git伺服器配置(使用SSH協議)

weixin_34320159發表於2016-01-30

配置git伺服器(使用SSH協議)

1、下載安裝

git-2.7.0

wget https://www.kernel.org/pub/software/scm/git/git-2.7.0.tar.gz
yum install gcc 
tar xvf git-2.7.0.tar.gz;cd git-2.7.0
make configure
mkdir /usr/local/git
./configure --prefix=/usr/local/git
make install
make install
#設定環境變數
vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/git/bin/
export PATH
source ~/.bash_profile
#檢視
git --version

2、配置

稍微設定一下使用者資訊

git config --global user.name "xulis2012"
git config --global user.email xulis2012@gmail.com 

建立git使用者

useradd -m -r git

新建SSH-key

如果出於安全考慮,需要使用有密碼的key,那麼客戶端需要使用SSH金鑰密碼記憶功能,比如ssh-agent等方式,如果ssh-key不需要是無密碼的,則無需設定。

預設情況下會使用SSH的預設key(一般為id_rsa),自行指定,可使用~/.ssh/config

su git
ssh-keygen
#這裡以指定key例子
[root@@omd_host test]# ? ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /home/git/.ssh/id_git
Enter passphrase (empty for no passphrase):  #密碼設定為空,如果設定了密碼,客戶端使用,需有記住此Key密碼的功能
Enter same passphrase again:
Your identification has been saved in /tmp/id_test.
Your public key has been saved in /tmp/id_test.pub.
The key fingerprint is:
db:c8:37:24:56:08:b8:c5:d5:8e:f3:60:84:d6:fb:a0 root@i-25BDB9AF
The key's randomart image is:
+--[ RSA 2048]----+
|     o.+..       |
|    . =.o..      |
|     + ..+.      |
|    .   B..      |
|       oS*.      |
|      Eo *o      |
|        + +      |
|         . .     |
|                 |
+-----------------+
cat /home/git/.ssh/id_git.pub >> /home/git/.ssh/authorized_keys 
chmod 600 /home/git/.ssh/authorized_keys

測試SSH

#一般出於安全考慮,服務端ssh埠會被修改,這裡以2222為例,git預設使用22連線
ssh -p 2222 -i /home/git/.ssh/id_git git@localhost
#連線成功的話,就說明ssh配置是沒有問題的了

新建裸專案

mkdir /opt/git/myproject
cd /opt/git/myproject
git init --bare
chown -R git:git /opt/git/myproject 
#如果目錄沒有寫許可權, git push的時候會提示:"remote: error: insufficient permission for adding an object to repository database ./objects"錯誤。
#設定git的登入shell為git-shell
vim /etc/passwd
git:x:500:501::/home/git:/usr/local/git/bin/git-shell

3、客戶端配置

首先客戶端需要有私鑰(yue)

scp /home/git/.ssh/id_git username@remote_host:/keypath/

新增ssh配置

vim ~/.ssh/config
Host gitser
    HostName 192.168.1.1   #git伺服器的地址
    User git
    IdentityFile /keypath/id_git  #指定使用的私鑰
    IdentitiesOnly yes

測試

cd /tmp
git clone ssh://git@gitser:2211/opt/git/myproject ./test
cd ./test
echo '(define (doub x) (* x 2))' > te.scm
git add te.scm
git commit -m "first"
git push origin master

結束

其實過程比較簡單,官方文件也相當詳細。之所以要寫出來,是因為使用SSH的方式,SSH的問題我折騰了挺久,大體上來說是這樣的:

預設情況下,git clone 可以指定使用者和SSH埠(例如:git clone ssh://git@hostname:2222/path/),證照使用的是當前使用者的~/.ssh/id_rsa

而我的需求是:首先SSH埠不用22以及使用者不用ROOT都滿足了;然後是我需要的是一些其他需求和解決方案,希望對有這種需求的人有幫助:

指定Key:使用SSH的config配置

Key密碼驗證:使用ssh-agent

相關文章