centos7下安装git服务端,并自动更新到web目录

1.服务器安装git

①yum安装
[root@localhost home]#yum install -y git
②查看版本
[root@localhost home]# git --version
git version 1.7.1

2.创建git用户并设置密码

[root@localhost home]# id git
id: git:无此用户
[root@localhost home]# useradd git
[root@localhost home]# passwd git

3.创建服务端git仓库

①创建git仓库
[root@localhost home]# mkdir -p  /home/data/git/gittest.git
[root@localhost home]# git init --bare /home/data/git/gittest.git
Initialized empty Git repository in /home/data/git/gittest.git/
②修改仓库所有者
[root@localhost home]# cd /home/data/git/
[root@localhost git]# chown -R git:git gittest.git/

4.window上clone服务器上的项目

$ git clone [email protected]:/home/data/git/gittest.git
如果SSH用的不是默认的22端口,则需要使用以下的命令(假设SSH端口号是7071):
$ git clone ssh://[email protected]:7071/home/data/git/gittest.git
遇到如下提示,选择yes然后一直回车
The authenticity of host '192.168.56.101 (192.168.56.101)' can't be established.
RSA key fingerprint is SHA256:Ve6WV/SCA059EqoUOzbFoZdfmMh3B259nigfmvdadqQ.
Are you sure you want to continue connecting (yes/no)? 

5. 客户端创建 SSH 公钥和私钥

$ ssh-keygen -t rsa -C "########@qq.com"
遇到提示一直回车,此时 C:\Users\用户名\.ssh 下会多出两个文件 id_rsa 和 id_rsa.pub私钥和公钥

6.服务器端 Git 打开 RSA 认证⑥ 服务器端 Git 打开 RSA 认证

①进入 /etc/ssh 目录,编辑 sshd_config,打开以下三个配置的注释:

RSAAuthentication yes

PubkeyAuthentication yes

AuthorizedKeysFile .ssh/authorized_keys

②重启ssh

systemctl restart sshd

③在/home/git下面新建.ssh文件夹

[root@localhost git]# mkdir .ssh
[root@localhost git]# ls -a 
. .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla .ssh
④然后把 .ssh 文件夹的 owner 修改为 git
[root@localhost git]# chown -R git:git .ssh

7.将客户端公钥导入服务器端 /home/git/.ssh/authorized_keys 文件

$ ssh [email protected] 'cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub

按照提示输入服务端git用户的密码

回到服务器端,查看 .ssh 下是否存在 authorized_keys 文件:

[root@localhost git]# cd .ssh
[root@localhost .ssh]# ll
总用量 4
-rw-rw-r--. 1 git git 398 10月  26 10:18 authorized_keys

修改 .ssh 目录的权限为 700

修改 .ssh/authorized_keys 文件的权限为 600

[root@localhost git]# chmod 700 .ssh
[root@localhost git]# cd .ssh
[root@localhost .ssh]# chmod 600 authorized_keys 

然后到客户端就可以git克隆项目了。。。

$ git clone [email protected]:/home/data/git/gittest.git

8. 禁止 git 用户 ssh 登录服务器

之前在服务器端创建的 git 用户不允许 ssh 登录服务器

编辑 /etc/passwd

找到:git:x:502:504::/home/git:/bin/bash

修改为git:x:502:504::/home/git:/bin/git-shell

此时 git 用户可以正常通过 ssh 使用 git,但无法通过 ssh 登录系统。

9.将远程仓库项目自动同步到web目录

切换到web目录,克隆项目

[root@localhost gittest]# cd /opt/lampp/htdocs/

[root@localhost htdocs]# git clone /home/data/git/gittest.git

10.修改web目录的权限为git用户

[root@localhost htdocs]chown -R git.git gittest

11.编写自动拉取脚本

 [root@localhost htdocs] cd /home/data/git/gittest.git/hooks

 [root@localhost hooks]  vi post-receive 编写如下脚本保存退出

保存后赋予可执行权限:

# chmod +x /home/data/git/gittest.git/hooks/post-receive

配置完成,这样在本地执行git push 代码时,服务器代码仓库更新的同时

猜你喜欢

转载自blog.csdn.net/Jsonxiang/article/details/83410238