Linux CentOS搭建Git服务器

简介

Git是目前最为流行的版本控制工具和分布式开发的最好用的工具。Github虽然非常方便,但是由于普通用户只能建立public的repository,只有付费用户才能创建private repository。因此,在平时工作中一些无法公开项目只能自己搭建一个Git服务器了。

环境配置

服务器:Linux CentOS-64
虚拟机:VMWare 11.1.0

服务器端Git版本:version 1.9.4

客户端Git版本: Git bash

测试客户端:Window10-64

服务器端的Git安装

方法一:直接安装

yum install git

通过which git可以查看git为否安装成功

方法二:下载安装包安装,地址http://distfiles.macports.org/git-core/

sudo yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel

下载安装包后,移到/usr/local/src/,输入以下命令安装git

cd /usr/local/src
sudo tar -zvxf git-1.9.4.tar.gz
cd git-1.9.4
sudo make prefix=/usr/local/git all
sudo make prefix=/usr/local/git install
sudo ln -s /usr/local/git/bin/* /usr/bin/

输入git --version查看是否安装成功

sudo yum install python python-setuptools

cd /usr/local/src

git clone git://github.com/res0nat0r/gitosis.git

cd gitosis

python setup.py install  

#显示Finished processing dependencies for gitosis==0.2即表示成功

至此,Git在服务器上安装已经完成。可以新建一个project作为测试。

#在/root/git下新建一个名为project的Git仓库
git init --bare project.git

客户端Git安装

 下载Git Bash安装包,在Windows下安装



#打开Git Bash,输入下面命令,会出现保存ssh的文件路径等信息,可以直接回车
ssh-keygen -t rsa 

会出现下面两个文件,



这样就已经生成ssh秘钥。打开bash,首先确保客户端和服务器端的ssh链接是好的。在服务器端通过service sshd status来查看ssh服务是否启动,在客户端通过git clone命令来下载刚才建立的project的仓库内容



可以看到已经成功。


如果不希望连接root用户,可以创建新的用户来连接,方法如下,来自 http://blog.chinaunix.net/uid-27105712-id-4511124.html

git server 安装配置

1、切换到root用户
stevenrao@server:~$  su - root 
2、创建git组
root@server:~$  groupadd git
root@server:~$  groupadd user 
3、添加git账户
root@server:~$ useradd git -g git -m -s /bin/bash
root@server:~$ usermod -G git,user git #把git同时加入git组和user组 
root@server:~$ passwd git #设置密码 
4、添加到sudoers用户中去,尽量不要用root操作
root@server:~$ vim /etc/sudoers #加上下面一句 git ALL=(ALL:ALL) ALL 
root@server:~$ su - git #切换到git用户下工作 
5、安装3个软件包
git@server:~$ sudo apt-get install git git-core ssh 
6、建立一个空白仓库
git@server:~$ mkdir project.git
git@server:~$ cd project.git
git@server:~/project.git$ git --bare 
init Initialized empty Git repository in /home/git/project.git/ 
7、再添加几个账户, 本样例账户为stevenrao
git@server:~/project.git$ cd ~
git@server:~$ sudo useradd stevenrao -g user -m -s /bin/bash
git@server:~$ sudo usermod -G git,user stevenrao #把stevenrao同时加入git组和user组 
git@server:~$ sudo passwd stevenrao #设置密码 
8、配置stevenrao用户秘钥(秘钥生成参考后面git client 配置中的2,3步骤)
git@server:~$  mkdir /home/git/.ssh
git@server:~$ vim /home/git/.ssh/authorized_keys
sh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAp1s5dDRyMeY7q6mRlQz5DbfKC/Ebtl5/gdanOaASjnNtJPULuTyVIj3UmYzZFVaVO+3yC31+cQTGWp2bXzKEuXqIUxhygvOCr+vwfRK8FrpXOLLF5WWQYd6UgpV6/6mgRPuVVDa39zqGwKEVBgoIpiVYRBSIywbO0Qf9SGQV4CevEny8aGTZCE/UHq19ec5w9jrsvij5zt xyFlvfjIaDOb8F/b3IqZ1v5PB5UHiJaZqE0xjxFiA6wtlprkrNOQKyBI44MCjw8P964+ED+w2I4n8TisCyok5FGBn66HzdBV59166NZfP+4CFScO8hDXpUnWPvpgjhtWUQeYCMLUp5eQ== stevenrao@ubuntu 
9、配置git config
git@server:~/project.git$ vim /home/git/project.git/config 
[core]
        repositoryformatversion = 0 filemode = true bare = true (添加下面两行)
[receive]
        denyCurrentBranch = ignore 
10、重启ssh服务
git@server:~$  /etc/init.d/ssh restart 

git client 配置 (在另一台电脑上)

1、安装3个软件包
root@client:~$ sudo apt-get install git git-core ssh 
1、添加用户
root@client:~$  useradd stevenrao -g user -m -s /bin/bash
root@client:~$  passwd stevenrao #设置密码 
root@client:~$  su -u stevenrao #切换当前用户 
2、添加git秘钥
stevenrao@client:~$ ssh-keygen -t rsa 
Generating public/private rsa key pair. Enter file in which to save the key (/home/stevenrao/.ssh/id_rsa): (直接回车)
Created directory '/home/stevenrao/.ssh'
Enter passphrase (empty for no passphrase): (输入密码)
Enter same passphrase again: (再次确认密码)
Your identification has been saved in /home/stevenrao/.ssh/id_rsa. 
Your public key has been saved in /home/stevenrao/.ssh/id_rsa.pub. 
The key fingerprint is: e8:6a:dd:2d:34:57:44:a5:61:65:75:c4:89:f2:f6:e4 stevenrao@client 
The key's randomart image is:
+--[ RSA 2048]----+
|           .+o=+=|
|           o.= .o|
|           .+    |
|       .    .o . |
|      . S  .. +  |
|     .  o .    E |
|     ..o +       |
|    ... o .      |
|   ..    .       |
+-----------------+
stevenrao@client:~$ ls -al .ssh/
total 16
drwx------ 2 stevenrao user 4096 2014-09-28 06:51 .
drwxr-xr-x 3 stevenrao user 4096 2014-09-28 06:50 ..
-rw------- 1 stevenrao user 1751 2014-09-28 06:51 id_rsa
-rw-r--r-- 1 stevenrao user  398 2014-09-28 06:51 id_rsa.pub 
2、查看公钥
stevenrao@client:~$ cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAp1s5dDRyMeY7q6mRlQz5DbfKC/Ebtl5/gdanOaASjnNtJPULuTyVIj3UmYzZFVaVO+3yC31+cQTGWp2bXzKEuXqIUxhygvOCr+vwfRK8FrpXOLLF5WWQYd6UgpV6/6mgRPuVVDa39zqGwKEVBgoIpiVYRBSIywbO0Qf9SGQV4CevEny8aGTZCE/UHq19ec5w9jrsvij5ztxyFlvfjIaDOb8F/b3IqZ1v5PB5UHiJaZqE0xjxFiA6wtlprkrNOQKyBI44MCjw8P964+ED+w2I4n8TisCyok5FGBn66HzdBV59166NZfP+4CFScO8hDXpUnWPvpgjhtWUQeYCMLUp5eQ== stevenrao@client 
3、初始化,添加test.txt文件到git仓库中去
stevenrao@client:~$ mkdir /home/stevenrao/test_proj
stevenrao@client:~$ cd /home/stevenrao/test_proj
stevenrao@client:~/test_proj$ echo "test git by stevenrao v1.0" > test.txt
stevenrao@client:~/test_proj$ git init Initialized empty Git repository in /home/stevenrao/test_proj/.git/
stevenrao@client:~/test_proj$ git add .
stevenrao@client:~/test_proj$ git commit -m 'initial commit' 
stevenrao@client:~/test_proj$ git remote add origin stevenrao@172.19.44.7:/home/git/project.git
stevenrao@client:~/test_proj$ git push origin master
stevenrao@client:~/test_proj$ git push origin master 
4、配置客户端操作账户
stevenrao@client:~$ git config --global user.name "stevenrao" 
stevenrao@client:~$ git config --global user.email "[email protected]" 
5、修改继续提交
stevenrao@client:~/test_proj$ vim test.txt 
test git by stevenrao v1.1 
stevenrao@client:~/test_proj$ git commit -m "1.1" test.txt
stevenrao@client:~/test_proj$ git push

你可能感兴趣的:(Git,Linux,git,虚拟机,分布式,版本控制工具)