配置简单的Git服务器

服务端
1、安装GIT
[iong@bogon iong]# sudo apt-get install git

2、创建git用户,并设置密码
[iong@bogon iong]# sudo adduser git
[iong@bogon iong]# sudo -s
[root@bogon iong]# passwd git

3、创建GIt仓库目录

[root@bogon iong]# mkdir /git
[root@bogon iong]# cd /git/

4、初始化仓库,并将仓库所有者更改为git
[root@bogon git]# git init --bare sample.git
初始化空的 Git 版本库于 /git/sample.git/
[root@bogon git]# chown -R git:git /git

5、使用root用户在Git服务中添加客户端密钥

[iong@bogon iong]# sudo -s
[root@bogon git]# mkdir /home/git/.ssh
[root@bogon git]# touch /home/git/.ssh/authorized_keys
[root@bogon git]# vi /home/git/.ssh/authorized_keys

将所有的客户端的id_rsa.pub生成的公钥复制到 authorized_keys 文件里
6、禁用git用户sell权限
这可以通过编辑/etc/passwd文件完成。找到类似下面的一行:
git:x:1001:1001:,,,:/home/git:/bin/bash
改为:
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

客户端

1、使用账户生成ssh密钥
[iong@bogon git]$ su iong
[iong@bogon git]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/iong/.ssh/id_rsa):
Created directory '/home/iong/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/iong/.ssh/id_rsa.
Your public key has been saved in /home/iong/.ssh/id_rsa.pub.
The key fingerprint is:
6a:e9:f1:2f:86:47:76:ea:c3:b2:64:c9:35:7a:4c:ac iong@bogon
The key's randomart image is:

密钥生成在/home/iong/.ssh/id_rsa.pub文件中
2、第一次提交需要设置邮箱和用户
[iong@bogon abctime]$ git config --global user.email "[email protected]"
[iong@bogon abctime]$ git config --global user.name "li"

3、 在项目中初始化Git,并提交版本
[iong@bogon abctime]$ git init
[iong@bogon abctime]$ git add ./*
[iong@bogon abctime]$ git commit -m 'v1.0'

4、关联远程仓库
[iong@bogon abctime]$ sudo git remote add origin [email protected]:/git/sample.git
5、将本地项目提交到远程仓库
[iong@bogon abctime]$ git push -u origin master
6、其他客户端直接下载Git库至本地,可能需要提供git账号的密码
git clone git@*.*.*.*:/git/sample.git

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章