天天看點

搭建git伺服器搭建git伺服器

搭建git伺服器

環境:win7 x64作為用戶端192.168.2.102,centos7作為服務端192.168.2.32

備注:centos7預設安裝了git-1.8.3.1版本,是以無需再安裝

win7 git用戶端下載下傳位址:https://git-for-windows.github.io/

win7 git用戶端安裝可以參考以下文檔:【Git的安裝步驟博文http://www.cnblogs.com/wj-1314/p/7993819.html】

服務端:

#檢視git版本
[[email protected] ~]$git --version
git version 1.8.3.1
#切換為root使用者,不切換root使用者後邊無法進入建立入的git使用者的家檔案
[[email protected] home]$ sudo su - root
[sudo] password for admin: 
 #檢視git使用者是否存在
[[email protected] home]# id git
id: git: no such user #提示git使用者不存在
#建立git使用者并設定密碼
[[email protected] home]#useradd git
[[email protected] home]#passwd git
Changing password for user git.
New password: #設定密碼
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: #确認密碼
passwd: all authentication tokens updated successfully.

#建立git倉庫
[[email protected] home]$ sudo mkdir -p /home/git/repository/gittest.git
[[email protected] home]# ls
admin git
[[email protected] home]# cd git
[[email protected] git]# ls
repository
[[email protected] git]# cd /home/git/gitrepository/
[[email protected] repository]# ls
gittest.git
#初始化項目git倉庫目錄
[[email protected] repository]# git init --bare /home/git/repository/gittest.git 
Initialized empty Git repository in /home/git/repository/gittest.git/

#更改git倉庫所有歸屬使用者
[[email protected] repository]# cd ..
[[email protected] git]# ll
total 0
drwxr-xr-x 3 root root 25 May 22 04:19 repository
[[email protected] git]# chown -R git:git /home/git/repository
[[email protected] git]# ll
total 0
drwxr-xr-x 3 git git 25 May 22 04:19 repository
           

用戶端:

#建立項目目錄
cd /f
mkdir gittest
#在用戶端clone遠端倉庫
cd /f/gittest
git clone [email protected]:/home/git/repository/gittest.git
首次clone倉庫會出現以下提示,需要輸入一次yes和一次git使用者密碼
Cloning into 'gittest'...
The authenticity of host '192.168.2.32 (192.168.2.32)' can't be established.
ECDSA key fingerprint is SHA256:D9Eoa+0UNCcPIPF4LKhr0nDE52fwbeuaDld8/4Rkie8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.32' (ECDSA) to the list of known hosts.
[email protected]'s password:
warning: You appear to have cloned an empty repository.

#設定公鑰和私鑰
ssh-keygen -t rsa -C "[email protected]"
#一路回車,公鑰和私鑰預設儲存在C:/user/使用者名/.ssh檔案下,分别預設命名為id_rsa 和 id_rsa.pub
           

服務端:

#建立公鑰存放目錄
cd /home/git
mkdir .ssh
#更改.shh目錄所有歸屬使用者
chown -R git:git .ssh
           

用戶端:

#将用戶端公鑰檔案導入服務端.shh目錄中
cd /f/gittest
ssh [email protected] 'cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub
#需要輸入一次git使用者密碼
           

服務端:

#修改公鑰目錄權限和公鑰檔案權限
chmod 700 /home/git/.ssh
chmod 600 /home/git/.ssh/authorized_keys
#再次用用戶端clone遠端倉庫就可以不用密碼直接連接配接了
           

用戶端:

#配置本地git的名稱和郵箱
cd /f/gittest
git config --global user.name "zaw"
git config --global user.email "[email protected]"
           
#禁止git使用者ssh直接登入服務端系統
#編輯/etc/passwd檔案
sudo vi /etc/passwd
找到:
git:x:1001:1001::/home/git:/bin/bash
修改為:
git:x:1001:1001::/home/git:/bin/git-shell

#删除遠端倉庫連接配接(用戶端操作)
rm -rf .git

           

繼續閱讀