在linux服務(wù)器上搭建git服務(wù)器
寫在前面,使用 centos7 root賬戶執(zhí)行了以下所有操作。服務(wù)器上安裝openssh, git 客戶機(jī)上安裝 git。
自己搭建的時(shí)候是昨天,寫這篇文章是今天,所以,我自己沒有嚴(yán)格按照以下步驟執(zhí)行,但是反復(fù)瀏覽了幾遍,應(yīng)該沒什么問題
- 創(chuàng)建一個(gè)git用戶專門用來提交項(xiàng)目文件
#創(chuàng)建組
groupadd git
#指定用戶所屬組
useradd git -g git
- 禁止git用戶登錄shell
vi /etc/passwd
#找到類似下面的這一行
#git:x:1001:1001:,,,:/home/git:/bin/bash
#修改為
#git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
- 創(chuàng)建證書驗(yàn)證
#如果權(quán)限不對,就會讀取不到公鑰,git就會一直提示你輸入密碼....
#創(chuàng)建目錄,并且修改文件夾權(quán)限
mkdir /home/git/.ssh
chmod 755 /home/git/.ssh
#創(chuàng)建文件,用以保存公鑰, 并修改權(quán)限
touch /home/git/.ssh/authorized_keys
chmod 644 /home/git/.ssh/authorized_keys
- 初始化git裸倉庫
mkdir /home/gitrepo
chown git:git /home/gitrepo
#初始化倉庫
git init --bare /home/gitrepo/gitname.git
#修改所屬用戶組 -R 表示遞歸修改目錄下所有文件,文件夾
chown -R git:git /home/gitrepo/gitname.git
#以上, 在 /home/gitrepo 目錄下創(chuàng)建了一個(gè)名為 gitname 的 git倉庫
添加公鑰
公鑰交給管理員(其實(shí)就是你自己了),添加到 /home/git/.ssh/authorized_keys 文件中。直接操縱虛擬機(jī)無法復(fù)制公鑰,可能要使用文件共享才行。如果是通過遠(yuǎn)程工具 putty, xshell, cmder等, 可以復(fù)制上去(親測putty可以,其他不知道)
管理公鑰還可以使用 [Gitosis] 工具,我等下再去了解。clone 克隆
#git@主機(jī)IP:端口號 項(xiàng)目地址
git clone git@192.168.43.128:/home/gitrepo/gitname.git
#如果不是默認(rèn)端口,需要加上端口號,比如 20000
git clone git@192.168.43.128:20000/home/gitrepo/gitname.git
#我搭建成功之后...
$ git clone git@192.168.43.128:/home/gitrepo/ycrepo.git
Cloning into 'ycrepo'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.