阿里云Centos7.4搭建git服务器

前言

阿里云Centos7.4搭建git服务器。

安装git及初始化用户(服务器)

1
2
3
4
5
6
7
# 安装Git
[root@iZwz94664y88uf68wjzri0Z ~]# yum install git
# 创建一个git用户组和用户,用来运行git服务
[root@iZwz94664y88uf68wjzri0Z ~]# groupadd git
[root@iZwz94664y88uf68wjzri0Z ~]# adduser git -g git
# 设置git用户密码
[root@iZwz94664y88uf68wjzri0Z /]# passwd git

进制git用户登录(服务器)

1
2
3
4
5
[root@iZwz94664y88uf68wjzri0Z ~]# vim /etc/passwd
# 找到这句:
git:x:503:503::/home/git:/bin/bash
# 改为:
git:x:503:503::/home/git:/bin/git-shell

创建证书登录(服务器)

1
2
3
4
5
6
[root@iZwz94664y88uf68wjzri0Z git]# mkdir /home/git/.ssh
[root@iZwz94664y88uf68wjzri0Z git]# chmod 700 /home/git/.ssh
[root@iZwz94664y88uf68wjzri0Z git]# touch 700 /home/git/.ssh/authorized_keys
[root@iZwz94664y88uf68wjzri0Z git]# chmod 600 /home/git/.ssh/authorized_keys
# 注意,如果是采用的sudo方式来创建git和相应的文件的,需要设置/home/git/.ssh/的owner为git,否则还是每次要输入密码的。
[root@iZwz94664y88uf68wjzri0Z git]# sudo chown -R git:git /home/git/.ssh/

配置git免密码(服务器)

1
2
3
4
5
6
7
8
9
10
11
# 添加如下内容
[root@iZwz94664y88uf68wjzri0Z ~]# vim etc/ssh/sshd_config

# 开启RSA认证功能
RSAAuthentication yes
# 开启公匙认证
PubkeyAuthentication yes
# author公匙文件
AuthorizedKeysFile /home/git/.ssh/authorized_keys
# 如果是yes在某些情况下可能会强制要求登录用户和文件拥有者用户相同
StricModes no

初始化git仓库(服务器)

1
2
3
4
5
6
7
[root@iZwz94664y88uf68wjzri0Z git]# cd /srv
[root@iZwz94664y88uf68wjzri0Z srv]# mkdir gitrepo
[root@iZwz94664y88uf68wjzri0Z srv]# chown git:git gitrepo/
[root@iZwz94664y88uf68wjzri0Z srv]# cd gitrepo
[root@iZwz94664y88uf68wjzri0Z gitrepo]# git init --bare lishijia.git
Initialized empty Git repository in /srv/gitrepo/project.git/
[root@iZwz94664y88uf68wjzri0Z gitrepo]# chown -R git:git lishijia.git

生成git秘钥(客户端)

1
2
3
# 创建私钥,文件位于用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件
$ ssh-keygen -t rsa -C "5878842@qq.com"
$ cat ~/.ssh/id_rsa.pub

配置git秘钥(服务器)

1
2
# 把上一步在客户端生成的git秘钥配置到如下文件
[root@iZwz94664y88uf68wjzri0Z git]# vim /home/git/.ssh/authorized_keys

配置邮箱用户名(客户端)

1
2
3
4
5
6
7
8
9
10
11
12
[root@izwz94664y88uf68wjzri0z lishijia]# git pull
git@47.107.168.156's password:
Could not chdir to home directory /home/git: Permission denied
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'root@izwz94664y88uf68wjzri0z.(none)')
[root@izwz94664y88uf68wjzri0z lishijia]# git config --global user.email "5878842@qq.com"
[root@izwz94664y88uf68wjzri0z lishijia]# git config --global user.name "lishijia"

克隆仓库&验证(客户端)

1
2
3
$ git clone git@47.107.168.156:/srv/gitrepo/lishijia.git
# 提示输入密码,密码就是之前给git用户设置的密码
$ git push origin master
分享到 评论