1.安装git
sudo apt-get install git
2.创建git用户,运行git服务
sudo adduser git
3.创建证书登录
创建/home/git/.ssh/authorized_keys文件
$ cd /home/git/
$ mkdir .ssh
$ chmod 700 .ssh
$ touch .ssh/authorized_keys
$ chmod 600 .ssh/authorized_keys
收集所有需要登陆的用户公钥,公钥位于id_rsa.pub文件中,把我们的公钥导入的/home/git/.ssh/authorized_keys文件里,一行一个。
4.初始化git仓库,创建目录,切换目录拥有着。
$ cd /home
$ mkdir depot ##创建目录depot
$ chown git:git depot/ ##将depot的拥有者修改为git
$ cd depot
$ git init --bare test.git ##初始化test.git仓库
$ chown -R git:git test.git ##把仓库所属用户改为git
5.上面已经搭建好了git服务器,现在在windows上,将代码clone到本地。
$ git clone git@172.16.1.130:/home/depot/test.git
Cloning into 'test'...
The authenticity of host '39.106.197.158 (39.106.197.158)' can't be established.
ECDSA key fingerprint is SHA256:GVUeRnxfWunh7Vv18IpL2UoW/zOXWBlWKfgklpruQ48.
Are you sure you want to continue connecting (yes/no)? yes ## 这里选择yes
Warning: Permanently added '39.106.197.158' (ECDSA) to the list of known hosts.
git@172.16.1.130's password: ##输入你创建的git用户的密码
warning: You appear to have cloned an empty repository.
6.在clone下来的本地仓库里,创建文件,并上传到远端仓库。
在clone下来的test文件中创建test.txt文件,将文件添加至暂存区,然后再提交到远端仓库
git add test.txt ##将文件添加的暂存区
git commit -m '测试提交' ##将文件提交至远端仓库 -m后面是本次提交说明
7.到这里我们发现自己的私钥并没有让自己免登陆,这时需要配置sshd_config文件并重启sshd
1. su root ;sshd_config位于/etc/ssh/ 文件夹下,需要使用root权限,同时如果出现 could not load host key: rsa_key dsa_key ecdsa_key, 也是由于这个原因,因为这三个文件只有root用户拥有读权限。
2. vim sshd_config
3. 取消AuthorizedKeysFile的注释,并修改为 AuthorizedKeysFile /home/git/.ssh/authorized_keys
4.重启sshd : /usr/sbin/sshd
如果重启sshd时,出现Could not load host key: /etc/ssh/ssh_host_ed25519_key则执行下面的命令
ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key
生成ssh_host_ed25519_key文件
再次执行sshd重启成功
参考博客:https://www.cnblogs.com/ToDoToTry/p/3956687.html
http://www.xitongzhijia.net/xtjc/20150615/50836.html