这里我们选择一台Linux服务器用来搭建Git服务器。
$ sudo apt-get install -y git
- $ git config --global user.name "deparks"
- $ git config --global user.email deparks@126.com
- $ mkdir -p /home/deparks/gitserver.git
- $ git init --bare /home/deparks/gitserver.git
创建服务器仓库时通常在git init中加上`--bare`选项,即建立一个裸仓库,一个不包含当前工作目录的仓库,按照惯例裸仓库目录名以 .git 结尾。
有了Git服务器仓库后最简单的Git服务器就搭建好了,接下来我们就对这个Git服务器做连接测试。
$ git clone deparks@192.168.40.20:/home/deparks/gitserver.git
Git 支持多种数据传输协议,这里使用的是SSH 传输协议,格式 user@serverip:path。
clone仓库后就可以通过把要通过Git管理的代码拷贝到仓库的当前目录,然后通过git add, git commit, git push origin master提交到Git服务器。
ssh服务器端有两个配置文件: /etc/ssh/ssh_config和~/.ssh/config,前者是对所有用户,后者是针对指定用户。为了方便对所有Git用户进行管理,可以打开ssh服务器 的RSA 认证:
1) 创建~/.ssh/config
- $ mkdir .ssh && chmod 700 .ssh
- $ touch .ssh/config
2) 创建~/.ssh/authorized_keys
$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
3) 配置 ~/.ssh/config, 添加以下内容:
- RSAAuthentication yes
- PubkeyAuthentication yes
- AuthorizedKeysFile .ssh/authorized_keys
4) 将用户pub_key导入~/.ssh/authorized_keys
$ cat id_rsa.deparks.pub >> ~/.ssh/authorized_keys
5) 重启sshd服务
$ /etc/rc.d/init.d/sshd restart