1.安裝 Git 依賴
[root@localhost ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
2.安裝 Git
[root@localhost ~]# yum install git
3. 創建 git 用戶和用戶組
創建一個 git 用戶組和用戶,用來運行 Git 服務
[root@localhost ~]# groupadd git
[root@localhost ~]# adduser git -g git
4.創建 ssh 登錄證書
收集所有需要登錄的用戶的公鑰,公鑰位於 ~/.ssh/id_rsa.pub 文件中
把每個開發者的公鑰導入到 /home/git/.ssh/authorized_keys 文件裏,一行一個
如果沒有 /home/git/.ssh/authorized_keys 文件則使用下面的命令創建
[root@localhost ~]# cd /home/git/
[root@localhost git]# mkdir .ssh
[root@localhost git]# chmod 700 .ssh
[root@localhost git]# touch .ssh/authorized_keys
[root@localhost git]# chmod 600 .ssh/authorized_keys
5.初始化 Git 倉庫
首先我們選定一個目錄作爲 Git 倉庫的根目錄,一般情況下我們會在 git 用戶目錄下創建 repo 目錄作爲 Git 服務的根目錄
首先切換到 git 用戶
[root@localhost git]# su git
[git@localhost ~]$ mkdir /home/git/repo
[git@localhost ~]$ chown /home/git/repo git:git
然後在 repo 目錄下創建我們的 Git 項目目錄,比如 git-demo.git
目錄名最好以 .git 結尾,這樣在 git clone 或者 push 的時候 URL 路徑更直觀
[git@localhost ~]$ cd /home/home/git/repo
[git@localhost ~]$ mkdir git-demo.git
[git@localhost ~]$ chown git:git git-demo.git
[git@localhost ~]$ cd git-demo.git
[git@localhost ~]$ git init --bare .
Initialized empty Git repository in /home/git/repo/git-demo.git/
6. 禁用 git 用戶登錄
編輯 /etc/passwd 文件完成,找到有 git 那行
git:x:503:503::/home/git:/bin/bash
改爲:
git:x:503:503::/home/git:/sbin/nologin
7. 克隆倉庫
現在我們就可以克隆這個遠程倉庫了,假設我們服務器的 IP 是 192.168.1.8
$ git clone [email protected]:~/repo/git-demo.git
Cloning into ‘git-demo’…
warning: You appear to have cloned an empty repository.
Checking connectivity… done.
如果出現類似的 Clone into 語句,則說明我們的 遠程 Git 服務配置完成了