centos7下安裝git服務端,並自動更新到web目錄
1.伺服器安裝git
①yum安裝
[[email protected] home]#yum install -y git
②檢視版本
[[email protected] home]# git --version
git version 1.7.1
2.建立git使用者並設定密碼
[[email protected] home]# id git
id: git:無此使用者
[[email protected] home]# useradd git
[[email protected] home]# passwd git
3.建立服務端git倉庫
①建立git倉庫 [[email protected] home]# mkdir -p /home/data/git/gittest.git [[email protected] home]# git init --bare /home/data/git/gittest.git Initialized empty Git repository in /home/data/git/gittest.git/ ②修改倉庫所有者 [[email protected] home]# cd /home/data/git/ [[email protected] 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資料夾
[[email protected] git]# mkdir .ssh
[[email protected] git]# ls -a
. .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla .ssh
④然後把 .ssh 資料夾的 owner 修改為 git
[[email protected] 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 檔案:
[[email protected] git]# cd .ssh
[[email protected] .ssh]# ll
總用量 4
-rw-rw-r--. 1 git git 398 10月 26 10:18 authorized_keys
修改 .ssh 目錄的許可權為 700
修改 .ssh/authorized_keys 檔案的許可權為 600
[[email protected] git]# chmod 700 .ssh
[[email protected] git]# cd .ssh
[[email protected] .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目錄,克隆專案
[[email protected] gittest]# cd /opt/lampp/htdocs/
[[email protected] htdocs]# git clone /home/data/git/gittest.git
10.修改web目錄的許可權為git使用者
[[email protected] htdocs]chown -R git.git gittest
11.編寫自動拉取指令碼
[[email protected] htdocs] cd /home/data/git/gittest.git/hooks
[[email protected] hooks] vi post-receive 編寫如下指令碼儲存退出
儲存後賦予可執行許可權:
# chmod +x /home/data/git/gittest.git/hooks/post-receive
配置完成,這樣在本地執行git push 程式碼時,伺服器程式碼倉庫更新的同時