【git】搭建git伺服器 在 Linux 下搭建 Git 伺服器
目錄
- ① 安裝 Git
- ② 伺服器端建立 git 使用者,用來管理 Git 服務,併為 git 使用者設定密碼
- ③ 伺服器端建立 Git 倉庫
- ④ 客戶端 clone 遠端倉庫
- ⑤ 客戶端建立 SSH 公鑰和私鑰
- ⑥ 伺服器端 Git 開啟 RSA 認證
- ⑦ 將客戶端公鑰匯入伺服器端 /home/git/.ssh/authorized_keys 檔案
- ⑧ 客戶端再次 clone 遠端倉庫
- ⑨ 禁止 git 使用者 ssh 登入伺服器
正文
環境:
伺服器 CentOS6.6 + git(version 1.7.1) 客戶端 Windows10 + git(version 2.8.4.windows.1)
回到頂部
① 安裝 Git
Linux 做為伺服器端系統,Windows 作為客戶端系統,分別安裝 Git
伺服器端:
#yum install -y git
安裝完後,檢視 Git 版本
[[email protected] ~]# 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 使用者設定密碼
[[email protected] home]# id git
id: git:無此使用者
[ [email protected] home]# useradd git
[[email protected] home]# passwd git
回到頂部
③ 伺服器端建立 Git 倉庫
設定 /home/data/git/gittest.git 為 Git 倉庫
然後把 Git 倉庫的 owner 修改為 git
[[email protected] home]# mkdir -p data/git/gittest.git [[email protected] home]# git init --bare data/git/gittest.git Initialized empty Git repository in /home/data/git/gittest.git/ [[email protected] home]# cd data/git/ [[email protected] git]# chown -R git:git gittest.git/
回到頂部
④ 客戶端 clone 遠端倉庫
進入 Git Bash 命令列客戶端,建立專案地址(設定在 d:/wamp64/www/gittest_gitbash)並進入:
[email protected] MINGW64 /d $ cd wamp64/www [email protected] MINGW64 /d/wamp64/www $ mkdir gittest_gitbash [email protected] MINGW64 /d/wamp64/www $ cd gittest_gitbash [email protected] MINGW64 /d/wamp64/www/gittest_gitbash $
然後從 Linux Git 伺服器上 clone 專案:
$ git clone [email protected]:/home/data/gittest.git
如果SSH用的不是預設的22埠,則需要使用以下的命令(假設SSH埠號是7700):
$ git clone ssh://[email protected]: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 "[email protected]"
此時 C:\Users\使用者名稱\.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 服務:
[[email protected] ssh]# /etc/rc.d/init.d/sshd restart
由 AuthorizedKeysFile 得知公鑰的存放路徑是 .ssh/authorized_keys,實際上是 $Home/.ssh/authorized_keys,由於管理 Git 服務的使用者是 git,所以實際存放公鑰的路徑是 /home/git/.ssh/authorized_keys
在 /home/git/ 下建立目錄 .ssh
[[email protected] git]# pwd
/home/git [[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 [[email protected] git]# ll -a 總用量 32 drwx------. 5 git git 4096 8月 28 20:04 . drwxr-xr-x. 8 root root 4096 8月 28 19:32 .. -rw-r--r--. 1 git git 18 10月 16 2014 .bash_logout -rw-r--r--. 1 git git 176 10月 16 2014 .bash_profile -rw-r--r--. 1 git git 124 10月 16 2014 .bashrc drwxr-xr-x. 2 git git 4096 11月 12 2010 .gnome2 drwxr-xr-x. 4 git git 4096 5月 8 12:22 .mozilla drwxr-xr-x. 2 git git 4096 8月 28 20:08 .ssh
回到頂部
⑦ 將客戶端公鑰匯入伺服器端 /home/git/.ssh/authorized_keys 檔案
回到 Git Bash 下,匯入檔案:
$ ssh [email protected]192.168.56.101 '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 8月 28 20:08 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
回到頂部
⑧ 客戶端再次 clone 遠端倉庫
$ git clone [email protected]192.168.56.101:/home/data/git/gittest.git
檢視客戶端專案目錄:
專案已經 clone 了。
也可以使用 tortoiseGit 客戶端來管理專案:
clone
回到頂部
⑨ 禁止 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 登入系統。
目錄
- ① 安裝 Git
- ② 伺服器端建立 git 使用者,用來管理 Git 服務,併為 git 使用者設定密碼
- ③ 伺服器端建立 Git 倉庫
- ④ 客戶端 clone 遠端倉庫
- ⑤ 客戶端建立 SSH 公鑰和私鑰
- ⑥ 伺服器端 Git 開啟 RSA 認證
- ⑦ 將客戶端公鑰匯入伺服器端 /home/git/.ssh/authorized_keys 檔案
- ⑧ 客戶端再次 clone 遠端倉庫
- ⑨ 禁止 git 使用者 ssh 登入伺服器
正文
環境:
伺服器 CentOS6.6 + git(version 1.7.1) 客戶端 Windows10 + git(version 2.8.4.windows.1)
回到頂部
① 安裝 Git
Linux 做為伺服器端系統,Windows 作為客戶端系統,分別安裝 Git
伺服器端:
#yum install -y git
安裝完後,檢視 Git 版本
[[email protected] ~]# 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 使用者設定密碼
[[email protected] home]# id git
id: git:無此使用者
[[email protected] home]# useradd git
[[email protected] home]# passwd git
回到頂部
③ 伺服器端建立 Git 倉庫
設定 /home/data/git/gittest.git 為 Git 倉庫
然後把 Git 倉庫的 owner 修改為 git
[[email protected] home]# mkdir -p data/git/gittest.git [[email protected] home]# git init --bare data/git/gittest.git Initialized empty Git repository in /home/data/git/gittest.git/ [[email protected] home]# cd data/git/ [[email protected] git]# chown -R git:git gittest.git/
回到頂部
④ 客戶端 clone 遠端倉庫
進入 Git Bash 命令列客戶端,建立專案地址(設定在 d:/wamp64/www/gittest_gitbash)並進入:
[email protected] MINGW64 /d $ cd wamp64/www [email protected] MINGW64 /d/wamp64/www $ mkdir gittest_gitbash [email protected] MINGW64 /d/wamp64/www $ cd gittest_gitbash [email protected] MINGW64 /d/wamp64/www/gittest_gitbash $
然後從 Linux Git 伺服器上 clone 專案:
$ git clone [email protected]:/home/data/gittest.git
如果SSH用的不是預設的22埠,則需要使用以下的命令(假設SSH埠號是7700):
$ git clone ssh://[email protected]: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 "[email protected]"
此時 C:\Users\使用者名稱\.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 服務:
[[email protected] ssh]# /etc/rc.d/init.d/sshd restart
由 AuthorizedKeysFile 得知公鑰的存放路徑是 .ssh/authorized_keys,實際上是 $Home/.ssh/authorized_keys,由於管理 Git 服務的使用者是 git,所以實際存放公鑰的路徑是 /home/git/.ssh/authorized_keys
在 /home/git/ 下建立目錄 .ssh
[[email protected] git]# pwd
/home/git [[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 [[email protected] git]# ll -a 總用量 32 drwx------. 5 git git 4096 8月 28 20:04 . drwxr-xr-x. 8 root root 4096 8月 28 19:32 .. -rw-r--r--. 1 git git 18 10月 16 2014 .bash_logout -rw-r--r--. 1 git git 176 10月 16 2014 .bash_profile -rw-r--r--. 1 git git 124 10月 16 2014 .bashrc drwxr-xr-x. 2 git git 4096 11月 12 2010 .gnome2 drwxr-xr-x. 4 git git 4096 5月 8 12:22 .mozilla drwxr-xr-x. 2 git git 4096 8月 28 20:08 .ssh
回到頂部
⑦ 將客戶端公鑰匯入伺服器端 /home/git/.ssh/authorized_keys 檔案
回到 Git Bash 下,匯入檔案:
$ ssh [email protected]192.168.56.101 '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 8月 28 20:08 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
回到頂部
⑧ 客戶端再次 clone 遠端倉庫
$ git clone [email protected]192.168.56.101:/home/data/git/gittest.git
檢視客戶端專案目錄:
專案已經 clone 了。
也可以使用 tortoiseGit 客戶端來管理專案:
clone
回到頂部
⑨ 禁止 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 登入系統。