Linux服务器配置之Git服务器搭建

1、服务器:阿里云CentOS 7.2(64位) + Git (version 1.8.3.1)
2、客户端:Windows 10 Pro(64位) + Git(version 2.11.0.windows.1)
3、服务器连接软件:Xshell 5

二、配置步骤

1、安装git
Linux作为服务器端系统,Windows作为客户端系统,分别安装Git
<b>服务器端:</b> [admin@ceontOS ~]$ su root #切换到root用户名 Password: #输入root用户的密码 [root@ceontOS ~]# yum install -y git #执行该命令进行Git安装

安装完后,查看Git版本

[root@ceontOS ~]# git --version git version 1.8.3.1

<b>客户端:</b>
下载 Git for Windows,地址:https://git-for-windows.github.io/
安装完之后,可以使用 Git Bash 作为命令行客户端。
安装完之后,查看 Git 版本

$ git --version git version 2.11.0.windows.1

Git客户端安装具体可参考:Git安装及SSH Key管理之Windows篇

2、服务器端创建 git 用户,用来管理 Git 服务,并为 git 用户设置密码

[root@ceontOS ~]# cd /home #进入/home/目录 [root@ceontOS home]# id git #查看git用户是否存在 id: git: no such user #提示git用户不存在 [root@ceontOS home]# useradd git #创建git用户 [root@ceontOS home]# passwd git #为git用户创建密码 Changing password for user git. New password: #设置密码 BAD PASSWORD: The password is shorter than 8 characters Retype new password: #确认密码 passwd: all authentication tokens updated successfully.

3、服务器端创建 Git 仓库
设置 /home/git/repository/gittest.git 为 Git 仓库

 

[root@ceontOS home]# mkdir -p ./git/repository/gittest.git #在git用户目录下创建仓库目录repositroy,并且创建gittest.git项目测试目录
[root@ceontOS home]# ls #查看/home/目录下有哪些用户目录
admin git
[root@ceontOS home]# cd git #进入git用户目录
[root@ceontOS git]# ls #查看git用户目录下有哪些目录/文件
repository
[root@ceontOS git]# cd repository/ #进入repository仓库目录
[root@ceontOS repository]# ls #查看仓库目录下的项目目录
gittest.git
[root@ceontOS repository]# git init --bare ./gittest.git #这步很重要,初始化项目测试目录
Initialized empty Git repository in /home/git/repository/gittest.git/

然后把 Git 仓库的 owner 修改为 git

[root@ceontOS git]# ll #查看gittest.git项目文件夹的拥有者
total 4
drwxr-xr-x 3 root root 4096 Jan 13 13:08 repository #拥有者是root用户名
[root@ceontOS git]# chown -R git:git repository #将拥有者改为git用户
[root@ceontOS git]# ll #再次查看gittest.git项目文件夹的拥有者
total 4
drwxr-xr-x 3 git git 4096 Jan 13 13:08 repository #拥有者是git用户

* **4、客户端 clone 远程仓库** 先在本地Windows系统上创建一个存放git项目的文件夹,例如我的设置在:【D:\gittest】 此处通过Git Bash来创建的,当然也可以手动创建该目录,然后进入该目录,启动Git Bash

JayYang@YJ-PC MINGW64 ~/Desktop #在桌面打开的git bash
$ cd /d #进入D盘
JayYang@YJ-PC MINGW64 /d
$ mkdir gittest #创建gittest文件夹
JayYang@YJ-PC MINGW64 /d
$ cd gittest/ #进入gittest文件夹
JayYang@YJ-PC MINGW64 /d/gittest #显示当前在D:\gittest路径下
$ git clone git@服务器公网IP地址:/home/git/repository/gittest.git #IP地址后面跟冒号,冒号后面是刚才初始化的项目文件夹的绝对路径

![](?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 当第一次连接到目标 Git 服务器时会得到一个提示:

The authenticity of host '118.178.142.77 (118.178.142.77)' can't be established.
ECDSA key fingerprint is SHA256:JwC9NxLIjBGqtLC2NUk8MulSc3XH3mM5AWMcFz0a5/8.
Are you sure you want to continue connecting (yes/no)? yes

选择 yes:

Warning: Permanently added '118.178.142.77' (ECDSA) to the list of known hosts.

此时 C:\Users\用户名\.ssh 下会多出一个文件 known_hosts,以后在这台电脑上再次连接目标 Git 服务器时不会再提示上面的语句。 【说明】如果你的服务器没有配置SSH连接,那么按照正常情况会让你输入git用户的密码,输入正确后就能进行项目克隆了。 ![](?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 如果不采用 SSH 公钥来进行验证,则每次都要输入密码,很麻烦,下面就来配置SSH公钥验证的方式来clone项目 * **5、客户端创建 SSH 公钥和私钥**

ssh-keygen -t rsa -C "695834706@qq.com"

此处我是管理了多个ssh_key,所以这边给公私钥起了个名字:id_rsa_git,生成后需要进行**ssh-add**操作,如何管理多个ssh_key可以参考:[Git安装及SSH Key管理之Windows篇]() ![](?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 此时 C:\Users\用户名\.ssh 下会多出两个文件 id_rsa 和 id_rsa.pub id_rsa_git 是私钥 id_rsa_git.pub 是公钥 ![](?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) * **6、服务器端修改配置文件,打开 RSA 认证** 进入 /etc/ssh 目录,编辑 sshd_config,打开以下三个配置的注释:

[root@ceontOS ~]# sudo vi /etc/ssh/sshd_config #root用户下,编辑/etc/ssh/sshd_config文件

按如下设置这三个配置,如果注释掉了,则去掉前面的#号

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

保存并重启 sshd 服务:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/96a4df952735ac2235b706a2e5cb6546.html