前言:以前学习了如何使用 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