前言:以前學習瞭如何使用 git 後,一直想搭建一個本機搭建一個 git server 的,一開始不知道走了彎路用了 gitosis,折騰了我好幾天都沒配置好。昨晚查資料發現 gitosis 早就過期了,更新很慢取而代之的是 gitolite。後來在查看 gitosis 和 gitolite 的時候發現了這篇文章,其實若是對權限要求不高的話,都不須要安裝 gitosis, gitolite,gitlab 之類的管理軟件,因此今天一大早起來就開始研究了,最終成功了。git
1、建立一個 Standard 新用戶名爲 git。gitlab
$ sudo chmod u+w /etc/sudoers
$ sudo vi /etc/sudoers
# 找到這一行 "root ALL=(ALL) ALL",在下面添加 "AccountName ALL=(ALL) ALL" (這裏是 git ALL=(ALL) ALL)並保存。
2、client 端,設置爲下圖post
3、驗證一下是否能鏈接上 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
4、使用 ssh rsa 公鑰測試
1) 若是 client 端已經建立 ssh rsa 公鑰, 則執行 2),不然先建立:spa
$ 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).net
$ 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_configcode
$ 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
通過上面幾步已經配置好了,下面來測試一下了: server
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