1. 程式人生 > >linux搭建Git服務器

linux搭建Git服務器

height tin ssh tor clas ... private chown 創建證書

安裝環境:CentOS 7

  剛接觸git,興致也比較大,雖說直接用github作為遠端庫是非常不錯的選擇,不過趁著自己的阿裏雲沒過期也試試搭建一個自己的git服務器。

  第一步,安裝git:

# yum install git

  第二步,創建登錄用戶git,用來運行git服務:

# adduser git

  第三步,創建證書登陸:

  這裏需要有訪問端的ssh用戶公鑰,就是id_rsa.pub文件,把內容導入到/home/git/.ssh/authorized_keys文件裏,一行一個。.ssh文件夾默認是沒有的需要自己進行創建。

# mkdir .ssh
# cd .ssh/
# cat id_rsa.pub >> authorized_keys

  其中公鑰的生成方式很簡單,在客戶機上運行:

# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:  

  生成過程中一路回車,最後會在用戶目錄下會生成.ssh文件夾,其中就有該用戶在這臺機器上的公鑰id_rsa.pub。

  第四步,初始化git倉庫:

  選定某目錄作為git倉庫存放路徑,本機是/srv/gitlocal/sample.git在/srv/gitlocal/目錄下執行:

# git init --bare sample.git
# chown -R git:git sample.git

  第五步,禁用shell登陸:

  更改git用戶登錄方式,防止出現使用git用戶登陸系統的問題。修改/etc/passwd文件,將git登錄方式由/bin/bash改為/usr/bin/git-shell。這樣,git用戶可以正常通過ssh使用git,但無法登陸shell。

  第六步,克隆遠端倉庫:

  從客戶端執行git clone命令克隆遠端倉庫:

# git clone [email protected]:/srv/gitlocal/sample.git //server是git服務器地址
Cloning into ‘sample‘...
warning: You appear to have cloned an empty repository.

  

linux搭建Git服務器