首页 > 技术文章 > Linux下搭建git服务器

luhan 2020-04-17 16:56 原文

Linux环境为Centos7、Git版本为1.8.1、客户端是Mac
 
设置git服务专用的账户:
    添加git用户:useradd git
    设置git用户的密码:passwd git    之后输入需要设置的密码即可
 
配置git账户可以进行免密登录:这一步是重点
    打开RSA认证登录:vim /etc/ssh/sshd_config    设置如下
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
    重启ssh服务:systemctl restart sshd
  在/home/git下创建.ssh文件夹:mkdir .ssh
        注意:如果是root用户创建的需要设置所属用户为git
    设置.ssh文件夹所属的用户:chown -R git:git .ssh
    设置.ssh文件夹的权限:chmod -R 700 .ssh/
    在.ssh文件夹下创建authorized_keys文件:touch authorized_keys
    设置authorized_keys所属用户:chown -R git:git authorized_keys
    设置authorized_keys权限:chmod -R 600 authorized_keys
    将客户端生成的id_ras.pub公钥内容复制到authorized_keys中:每行一个公钥即可
 
下载安装git:yum -y install git
 
创建git仓库:随意到目录中进行创建,后续将目录的所属用户修改为git账户即可
    例如:指定/home/git/rep为仓库地址
    设置rep文件夹的所属用户:chown -R git:git rep/
    进入到rep文件夹创建仓库:git --bare init test.git/
    设置test.git所属用户:chown -R git:git test.git/
 
客户端clone仓库即可:git clone git@ip地址:/home/git/rep/test.git
 
push时出现'remote: error: refusing to update checked out branch: refs/heads/master'问题:
    问题如下:这是由于初始化仓库时使用的是git inin命令而没有添加--bare参数导致的
    
    解决方案:进入到远程的仓库执行'git config --bool core.bare true'即可
 
如果开启了RSA认证也添加了公钥还是一直需要输入密码的问题:可以尝试将git用户删除,重新进行配置一次即可
 

推荐阅读