1.安装GIT
[root@localhost ~]# yum install -y git
2.创建git帐号
[root@localhost ~]# useradd git
[root@localhost ~]# passwd git
[root@localhost yjx2]# su git
3.创建git仓库
[git@localhost ~]$ git init --bare learngit.git
注意:在git用户下创建learngit.git 文件归属git
如果没有执行chown更改文件归属
[root@localhost git]# chown git:git learngit.git
4.客户端创建生成密钥ssh key
[yjx@localhost ~]$git config --global user.name "jingsia"
[yjx@localhost ~]$git config --global user.email "xxx@xx.com"
[yjx@localhost ~]$ssh-keygen -t rsa -C "xxx@xx.com"
这里会生成 id_rsa 和 id_rsa.pub 一个是私有密钥 一个是公有密钥
[yjx@localhost ~]$ ls .ssh/id_rsa*
.ssh/id_rsa .ssh/id_rsa.pub
5.git服务器打开RSA认证
打开文件
[root@localhost ~]# vim /etc/ssh/sshd_config
设置
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
6.git服务器添加客户端密钥
创建authorized_keys文件 并将客户端密钥输入到文件中 保存退出
[root@localhost ~]$ touch .ssh/authorized_keys
注意:authorized_keys归属root
[root@localhost ~]# ll /home/git/.ssh/
total 4
-rw-r--r--. 1 root root 398 Jan 14 17:07 authorized_keys
7.客户端使用
git clone git@192.190.71.99:/home/git/learngit.git
常见问题:
1.not a valid object name master
使用Git来管理代码,第一次远程分支上什么都没,也没有本地分支。想建新的分支也没用。提示:not a valid object name master,查了下资料需要先添加点东西。所以在本地git库中新增了一个文件,然后在git命令行中敲入git commit
2.已经添加客户端key但是还是让输入密码
1)authorized_keys文件归属不对,归属root用户下,即:
[root@localhost ~]# mkdir /home/git/.ssh
[root@localhost .ssh]# vim authorized_keys
[root@localhost .ssh]# cat ../id_rsa.pub >> authorized_keys
参考:
1.http://www.cnblogs.com/lovychen/p/5900688.html
2.http://blog.csdn.net/hustpzb/article/details/8230454/
3.http://blog.csdn.net/wave_1102/article/details/47779401
4.http://blog.csdn.net/spring123tt/article/details/49867359