① 安裝 Git
Linux 做為服務器端系統,Windows 作為客戶端系統,分別安裝 Git
服務器端:
#yum install -y git
安裝完后,查看 Git 版本
[root@localhost ~]# git --version
git version 1.7.1
客戶端:
下載 Git for Windows,地址:https://git-for-windows.github.io/
安裝完之后,可以使用 Git Bash 作為命令行客戶端。
安裝完之后,查看 Git 版本
$ git --version
git version 2.8.4.windows.1
② 服務器端創建 git 用戶,用來管理 Git 服務,並為 git 用戶設置密碼
[root@localhost home]# id git
id: git:無此用戶
[root@localhost home]# useradd git
[root@localhost home]# passwd git
③ 服務器端創建 Git 倉庫
設置 /home/data/git/gittest.git 為 Git 倉庫
然后把 Git 倉庫的 owner 修改為 git
[root@localhost home]# mkdir -p /home/git/gittest.git
[root@localhost home]# git init --bare /home/git/gittest.git
Initialized empty Git repository in /home/data/git/gittest.git/
[root@localhost home]# cd /home/git/
[root@localhost git]# chown -R git:git gittest.git/
④ 客戶端 clone 遠程倉庫
進入 Git Bash 命令行客戶端,創建項目地址(設置在 d:/wamp64/www/gittest_gitbash)並進入:
$ cd wamp64/www
$ mkdir gittest_gitbash
$ cd gittest_gitbash
然后從 Linux Git 服務器上 clone 項目:
$ git clone git@192.168.56.101:/home/data/gittest.git
如果SSH用的不是默認的22端口,則需要使用以下的命令(假設SSH端口號是7700):
$ git clone ssh://git@192.168.56.101:7700/home/data/gittest.git
當第一次連接到目標 Git 服務器時會得到一個提示:
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)?
選擇 yes:
Warning: Permanently added '192.168.56.101' (RSA) to the list of known hosts.
此時 C:\Users\用戶名\.ssh 下會多出一個文件 known_hosts,以后在這台電腦上再次連接目標 Git 服務器時不會再提示上面的語句。
后面提示要輸入密碼,可以采用 SSH 公鑰來進行驗證。
⑤ 客戶端創建 SSH 公鑰和私鑰
$ ssh-keygen -t rsa -C "630185513@qq.com"
此時 C:\Users\ Administrator \.ssh 下會多出兩個文件 id_rsa 和 id_rsa.pub
id_rsa 是私鑰
id_rsa.pub 是公鑰
⑥ 服務器端 Git 打開 RSA 認證
進入 /etc/ssh 目錄,編輯 sshd_config,打開以下三個配置的注釋:
RSAAuthentication yes(可能沒有需要加上)
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
保存並重啟 sshd 服務:
查看狀態:
systemctl status sshd.service
啟動服務:
systemctl start sshd.service
重啟服務:
systemctl restart sshd.service
開機自啟:
systemctl enable sshd.service
在 /home/git/ 下創建目錄 .ssh
[root@localhost git]# pwd/home/git
[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
[root@localhost git]# ll -a
⑦ 將客戶端公鑰導入服務器端 /home/git/.ssh/authorized_keys 文件
回到 Git Bash 下,導入文件:
$ cd C:/Users/ Administrator /.ssh
$ ssh git@192.168.56.101 'cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub
需要輸入服務器端 git 用戶的密碼
回到服務器端,查看 .ssh 下是否存在 authorized_keys 文件:
[root@localhost git]# cd .ssh
[root@localhost .ssh]# ll
重要:
修改 .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 用戶 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 登錄系統。
11.利用鈎子同步服務器代碼
如果在客服端提交代碼希望在服務器同時更新,這里我們可以使用git倉庫的hooks目錄下新建一個post-receive文件來進行更新,具體操作如下:
cd /var/git/gittest.git/hooks
vi post-receive
1
2
在post-receive里寫入以下代碼:
#!/bin/bash
cd /home/git/gittest.git
git --work-tree=/home/git/gittest checkout -f
1
2
接着鍵入ESC->eq 寫入保存。同時把文件擁有者改為git,並且給git賦有執行權限:
chown git:git post-receive
chmod 744 post-receive
1
2
/home/git/gittest這里可以替換為你想更新的文件夾,同時要在該文件夾給git寫入權限,這里我們可以把git用戶加入root用戶組,同時把要更新文件夾的用戶組添加可寫入權限:
usermod -aG root git
chmod -R 775 /home/git/gittest
12.上傳項目
客戶端:連接git服務器
git remote add origin git@172.16.10.201:/data/git/gittest.git
git push -u origin master -f