1 | 环境: |
① 安装 Git
Linux 做为服务器端系统,Windows 作为客户端系统,分别安装 Git
服务器端:
1 | #yum install -y git |
安装完后,查看 Git 版本
1 | [root@localhost ~]# git --version |
客户端:
自行下载并安装,安装完之后,查看 Git 版本
1 | $ git --version |
② 服务器端创建 git 用户,用来管理 Git 服务,并为 git 用户设置密码
1 | [root@localhost home]# id git |
③ 服务器端创建 Git 仓库
设置 /home/git/test.git 为 Git 仓库
然后把 Git 仓库的 owner 修改为 git
1 | [root@localhost git]# mkdir -p test.git |
④ 服务器端 Git 打开 RSA 认证
进入 /etc/ssh 目录,编辑 sshd_config,打开以下三个配置的注释:
1 | RSAAuthentication yes |
保存并重启 sshd 服务:
1 | [root@localhost ssh]# server sshd restart |
由 AuthorizedKeysFile 得知公钥的存放路径是 .ssh/authorized_keys,实际上是 $Home/.ssh/authorized_keys,由于管理 Git 服务的用户是 git,所以实际存放公钥的路径是 /home/git/.ssh/authorized_keys
在 /home/git/ 下创建目录 .ssh
1 | [root@localhost git]# pwd |
然后把 .ssh 文件夹的 owner 修改为 git
1 | [root@localhost git]# chown -R git:git .ssh |
⑤ 将客户端公钥导入服务器端 /home/git/.ssh/authorized_keys 文件
1 | [root@localhost git]# cat 公钥 >> .ssh/authorized_keys |
也可以使用Vim编辑
⑥ 禁止 git 用户 ssh 登录服务器
之前在服务器端创建的 git 用户不允许 ssh 登录服务器
编辑 /etc/passwd
找到:
1 | git:x:502:504::/home/git:/bin/bash |
修改为
1 | git:x:502:504::/home/git:/bin/git-shell |
此时 git 用户可以正常通过 ssh 使用 git,但无法通过 ssh 登录系统。
完成
通过以上操作,就实现了使用git自建服务器存放单用户仓库的全部流程;其中,开启RSA不是必需的,使用秘钥一样可以的.
克隆示例
1 | git clone git@{ip/域名:端口}:/home/git/test.git |