环境准备与Git安装
环境配置如下:
服务器端运行 CentOS 6.6 系统,安装的 Git 版本为 1.7.1。
客户端使用 Windows 10 系统,安装的 Git 版本为 2.8.4.windows.1。
第一步是安装 Git 软件。
需要在 Linux 服务器端和 Windows 客户端分别完成 Git 的安装工作。
服务器端的操作如下:
#yum install -y git
安装完成后,检查 Git 的版本信息。
[root@localhost ~]# git --version
git version 1.7.1
客户端的操作如下:
访问 https://git-for-windows.github.io/ 下载 Git for Windows 安装包。
安装程序后,可以使用 Git Bash 作为命令行工具进行操作。
安装完毕后,同样需要查看 Git 的版本以确认安装成功。
$ git --version
git version 2.8.4.windows.1
第二步,在服务器端创建一个名为 git 的用户,专门用于管理 Git 服务,并为其设置密码。
[root@localhost home]# id git
id: git:无此用户
[root@localhost home]# useradd git
[root@localhost home]# passwd git
第三步,在服务器端建立 Git 仓库。
指定 /home/data/git/gittest.git 路径作为 Git 仓库的位置。
随后将 Git 仓库的所有者权限修改为 git 用户。
[root@localhost home]# mkdir -p data/git/gittest.git
[root@localhost home]# git init --bare data/git/gittest.git
Initialized empty Git repository in /home/data/git/gittest.git/
[root@localhost home]# cd data/git/
[root@localhost git]# chown -R git:git gittest.git/
第四步,客户端从远程仓库克隆项目。
在 Git Bash 中创建项目目录(例如 d:/wamp64/www/gittest_gitbash)并进入该目录。
dee@Lenovo-PC MINGW64 /d
$ cd wamp64/www
dee@Lenovo-PC MINGW64 /d/wamp64/www
$ mkdir gittest_gitbash
dee@Lenovo-PC MINGW64 /d/wamp64/www
$ cd gittest_gitbash
dee@Lenovo-PC MINGW64 /d/wamp64/www/gittest_gitbash
$
执行命令从 Linux Git 服务器克隆项目。
$ git clone git@192.168.56.101:/home/data/gittest.git
首次连接目标 Git 服务器时,系统会弹出验证提示。
提示信息显示:The authenticity of host '192.168.56.101 (192.168.56.101)' can't be established.RSA key fingerprint is SHA256:Ve6WV/SCA059EqoUOzbFoZdfmMh3B259nigfmvdadqQ.Are you sure you want to continue connecting (yes/no)?
输入 yes 确认连接。
系统警告:Permanently added '192.168.56.101' (RSA) to the list of known hosts.
此时,C:\Users\用户名\.ssh 目录下会生成 known_hosts 文件,后续连接同一服务器不再提示此信息。

接下来需输入密码,建议使用 SSH 公钥进行身份验证。
第五步,客户端生成 SSH 公钥和私钥。
$ ssh-keygen -t rsa -C

操作后,C:\Users\用户名\.ssh 目录下会出现 id_rsa 和 id_rsa.pub 两个文件。
其中 id_rsa 为私钥文件。
id_rsa.pub 为公钥文件。

第六步,在服务器端开启 Git 的 RSA 认证功能。
进入 /etc/ssh 目录,编辑 sshd_config 文件,取消以下三项配置的注释:
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
保存配置并重启 sshd 服务。
[root@localhost ssh]# /etc/rc.d/init.d/sshd restart
根据 AuthorizedKeysFile 配置,公钥存放路径默认为 .ssh/authorized_keys,即 $Home/.ssh/authorized_keys。由于 Git 服务由 git 用户管理,实际路径为 /home/git/.ssh/authorized_keys。
在 /home/git/ 目录下创建 .ssh 文件夹。
[root@localhost git]# pwd
/home/git
[root@localhost git]# mkdir .ssh
[root@localhost git]# ls -a
. .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla .ssh
将 .ssh 文件夹的所有者修改为 git 用户。
[root@localhost git]# chown -R git:git .ssh
[root@localhost git]# ll -a
总用量 32
drwx------. 5 git git 4096 8月 28 20:04 .
drwxr-xr-x. 8 root root 4096 8月 28 19:32 ..
-rw-r--r--. 1 git git 18 10月 16 2014 .bash_logout
-rw-r--r--. 1 git git 176 10月 16 2014 .bash_profile
-rw-r--r--. 1 git git 124 10月 16 2014 .bashrc
drwxr-xr-x. 2 git git 4096 11月 12 2010 .gnome2
drwxr-xr-x. 4 git git 4096 5月 8 12:22 .mozilla
drwxr-xr-x. 2 git git 4096 8月 28 20:08 .ssh
第七步,将客户端生成的公钥导入服务器端的 /home/git/.ssh/authorized_keys 文件中。
回到 Git Bash 界面,执行导入命令。
$ ssh git@192.168.56.101 'cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub
执行时需输入服务器端 git 用户的密码。

返回服务器端,检查 .ssh 目录下是否存在 authorized_keys 文件。
[root@localhost git]# cd .ssh
[root@localhost .ssh]# ll
总用量 4
-rw-rw-r--. 1 git git 398 8月 28 20:08 authorized_keys
可以查看文件内容以确认是否为客户端生成的公钥。
注意以下重要步骤:
将 .ssh 目录的权限设置为 700。
将 .ssh/authorized_keys 文件的权限设置为 600。
[root@localhost git]# chmod 700 .ssh
[root@localhost git]# cd .ssh
[root@localhost .ssh]# chmod 600 authorized_keys
第八步,客户端再次尝试克隆远程仓库。
$ git clone git@192.168.56.101:/home/data/git/gittest.git

检查客户端的项目目录。

项目已成功克隆。
也可以使用 TortoiseGit 客户端来管理项目。

选择 clone 选项。

按照提示完成克隆操作。
第九步,禁止 git 用户通过 SSH 登录服务器系统。
此前创建的 git 用户不应具备 SSH 登录系统的权限。
编辑 /etc/passwd 文件。
git:x:502:504::/home/git:/bin/bash
找到对应的 git 用户行。
git:x:502:504::/home/git:/bin/git-shell
将其修改为使用 /bin/git-shell。
此时,git 用户可以通过 SSH 正常使用 Git 服务,但无法通过 SSH 登录操作系统。