前言:之前學習了如何使用 git 后,一直想搭建一個本機搭建一個 git server 的,一開始不知道走了彎路用了 gitosis,折騰了我好幾天都沒配置好。昨晚查資料發現 gitosis 早就過時了,更新很慢取而代之的是 gitolite。后來在查看 gitosis 和 gitolite 的時候發現了這篇文章,其實如果對權限要求不高的話,都不需要安裝 gitosis, gitolite,gitlab 之類的管理軟件,所以今天一大早起來就開始研究了,最終成功了。
一、創建一個 Standard 新用戶名為 git。
$ sudo chmod u+w /etc/sudoers
$ sudo vi /etc/sudoers
# 找到這一行 "root ALL=(ALL) ALL",在下面添加 "AccountName ALL=(ALL) ALL" (這里是 git ALL=(ALL) ALL)並保存。
二、client 端,設置為下圖
三、驗證一下是否能連接上 git 用戶
$ ssh git@yourComputerName.local
# Are you sure you want to continue connecting (yes/no)? yes
# Password:
# Last login: Fri Jan 3 09:00:55 2014
# exit
四、使用 ssh rsa 公鑰
1) 如果 client 端已經創建 ssh rsa 公鑰, 則執行 2),否則先創建:
$ cd ~
$ ssh-keygen -t rsa # 默認存放路徑 ~/.ssh/id_rsa.pub
2) 把 ssh rsa 公鑰拷貝到 server 端 (id_rsa.pub 文件在創建的時候,是可以改名的。這里也可以先把 ~/.ssh/id_rsa.pub 拷貝到別的地方並重新命名 $ cp ~/.ssh/id_rsa.pub /tmp/yourName.pub)
$ ssh git@yourComputerName.local mkdir .ssh # 登陸 server 並創建 .ssh 文件夾
$ scp ~/.ssh/id_rsa.pub git@yourComputerName.local:.ssh/authorized_keys
# id_rsa.pub 100% 405 0.4KB/s 00:00
3) 修改 server 端的 sshd_config
$ ssh git@yourComputerName.local $ cd /etc $ sudo chmod 666 sshd_config $ vi sshd_config
#PermitRootLogin yes 改為 PermitRootLogin no
# 下面幾項把前面的 #注釋 去掉
#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys
#PasswordAuthentication no
#PermitEmptyPasswords no
#UsePAM yes 改為 UsePAM no
# exit
經過上面幾步已經配置好了,下面來測試一下了:
1) 在 server 端創建空的 repository
$ cd path/to
$ mkdir newrepo.git
$ cd newrepo.git
$ git init --bare
# --bare flag 只是用來存儲 pushes,不會當做本地 repository 來使用的。
2) 在 client 端,創建 local repository 並 push
$ cd path/to $ mkdir newrepo $ cd newrepo $ git init $ touch README $ git add . $ git commit -m "initial commit" $ git remote add origin git@yourComputerName:/path/to/newrepo.git # 如果直接之前在 server 端創建的 newrepo.git 是在 home 目錄,則這里地址為:git@yourComputerName:newrepo.git
$ git push origin master