git 搭建
centos7下git服务器端搭建
git的安装:
yum 源仓库里的 Git 版本更新不及时,最新版本的 Git 是 1.8.3.1,但是官方最新版本已经到了 2.9.2。想要安装最新版本的的 Git,只能下载源码进行安装。
1. 查看 yum 源仓库的 Git 信息:
1
|
# yum info git
|
可以看出,截至目前,yum 源仓库中最新的 Git 版本才 1.8.3.1,而查看最新的 Git 发布版本,已经 2.9.2 了。
2. 依赖库安装
1
2
|
# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
# yum install gcc perl-ExtUtils-MakeMaker
|
3. 卸载低版本的 Git
通过命令:git –-version
查看系统带的版本,Git 版本是: 1.8.3.1
,所以先要卸载低版本的 Git,命令:
1
|
# yum remove git
|
4. 下载新版的 Git 源码包(我放的了 /usr/local/git 的目录下了,git是我自己mkdir的目录)
进入:/usr/local下,新建git目录: # mkdir git
# cd git
在线下载最新的源码包
1
|
# wget https://github.com/git/git/archive/v2.9.2.tar.gz
|
也可以离线下载,然后传到 CentOS 系统中指定的目录下。
5. 解压当前目录
1
|
# tar -xzvf v2.9.2.tar.gz
|
6. 安装 Git
分别执行以下命令进行编译安装,编译过程可能比较漫长,请耐心等待完成。
1
2
3
|
# cd git-2.9.2
# make prefix=/usr/local/git all
# make prefix=/usr/local/git install
|
7. 添加到环境变量
vim /etc/profile
#如果没有vim,则安装vim工具 yum install vim
添加这一条: export PATH="/usr/local/git/bin:$PATH"
source /etc/profile #是配置立即生效
8. 查看版本号
1
2
|
# git --version
git version 2.9.2
|
8. 将git设置为默认路径,不然后面克隆时会报错
[[email protected] code]$ ln -s /usr/local/git/bin/git-upload-pack /usr/bin/git-upload-pack
[[email protected] code]$ ln -s /usr/local/git/bin/git-receive-pack /usr/bin/git-receive-pack
至此,CentOS 就安装上了最新版本的 Git。
第二步,创建一个git用户组和
用户,用来运行git
服务:
-
$ groupadd git
-
$ useradd git -g git
-
$ passwd git #参数是用户名
最好切换到git用户 不然后面新建的git仓库都要改权限 烦烦烦!!
$ su - git
第三步,创建证书登录:
如何生成**:http://blog.****.net/permanent_2008/article/details/73839384
备注:下边虚线内容为多余内容,只是留着存档而已。于本教程没有关系
添加证书之前,还要做这么一步:
Git服务器打开RSA认证 。在Git服务器上首先需要将/etc/ssh/sshd_config中将RSA认证打开,
即:
1.RSAAuthentication yes
2.PubkeyAuthentication yes
3.AuthorizedKeysFile .ssh/authorized_keys
这里我们可以看到公钥存放在.ssh/authorized_keys文件中。
所以我们在/home/git下创建.ssh目录,然后创建authorized_keys文件,并将刚生成的公钥导入进去。
然后再次clone的时候,或者是之后push的时候,就不需要再输入密码了:
[email protected]/E/testgit/8.34 $ git clone [email protected]:/data/git/learngit.git Cloning into 'learngit'... warning: You appear to have cloned an empty repository. Checking connectivity... done.
===============================
收集所有需要登录的用户的公钥,就是他们自己的id_rsa.pub
文件,把所有公钥导入到/home/git/.ssh/authorized_keys
文件里,一行一个。
$ cd /home/git/
$ mkdir .ssh #新建文件夹
$ chmod 700 .ssh
$ touch .ssh/authorized_keys #新建文件
$ chmod 600 .ssh/authorized_keys
第四步,初始化Git仓库
-
$ cd /home/git
-
$ git init --bare test.git
-
Initialized empty Git repository in /home/git/test.git/
以上命令会创建一个空仓库,服务器上的Git仓库通常都以.git结尾。
第五步、本地克隆仓库
-
$ git clone [email protected]:test.git
-
Cloning into 'test'...
-
warning: You appear to have cloned an empty repository.
-
Checking connectivity... done.
your-ip 为您 Git 所在服务器 ip
用git clone 获取服务器上的代码
[[email protected] code]$ git clone [email protected]:/root/code.git
报错如下:
bash: git-upload-pack: command not found
fatal: The remote end hung up unexpectedly
什么原因呢?原来代码服务器【192.168.57.61】上的git安装路径是/usr/local/git,不是默认路径,根据提示,在git服务器192.168.57.61上, 建立链接文件:
[[email protected] code]# ln -s /usr/local/git/bin/git-upload-pack /usr/bin/git-upload-pack
再次,执行git clone ,果真可以了。
当然,如果无法修改git代码服务器上配置,可以在clone时,添加--upload-pack选项来指定git服务器上的git-upload-pack 路径,达到上面相同的目的,如下所示:
[[email protected] code]$ git clone --upload-pack "/usr/local/git/bin/git-upload-pack" [email protected]:/root/code.git
当然,也许你会遇到git-receive-pack 之类的错误,很有可能和这个原理是一样的,请采用类似的操作即可
5.禁止Shell登录
出于安全考虑,git用户不允许登录shell,这可以通过编辑/etc/passwd文件完成。
找到类似下面的一行:
git:x:502:502::/home/git:/bin/bash
改为
git:x:502:502::/home/git:/usr/local/git/bin/git-shell
这样,git用户可以正常通过ssh使用git,但无法登录shell,因为我们为git用户指定的git-shell每次一登录就自动退出
参考文档:
git安装部署 http://www.cnblogs.com/imayanlong/p/5617626.html
git/ssh捋不清的几个问题 http://www.cnblogs.com/hustskyking/p/problems-in-git-when-ssh.html
centos7下git服务器端搭建和验证 https://my.oschina.net/u/2343829/blog/644663
Git 服务器搭建 http://www.runoob.com/git/git-server.html
Windows下SSH连接git服务器 https://www.zybuluo.com/zhutoulwz/note/30495
搭建git服务器
服务端192.168.221.20,安装git,创建用户,创建文件authorized_keys,权限,属主属组
[[email protected]-002 yum.repos.d]# yum install git -y
[[email protected]-002 yum.repos.d]# useradd -s /usr/bin/git-shell git
[[email protected]-002 yum.repos.d]# cd /home/git/
[[email protected]-002 git]# mkdir .ssh
[[email protected]-002 git]# touch .ssh/authorized_keys
[[email protected]-002 git]# chmod 600 .ssh/authorized_keys
[[email protected]-002 git]# chown -R git:git .ssh/
客户端192.168.221.10,将公钥放到服务端.ssh/authorized_keys
[[email protected] apeng]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCZ4he1cZhRT7LHd5QXtdMqA2g0ub+tp9uau0gG6rN5cRcHZhJv4vyFhuyhFSR4dHNPzMg+j5KSfWtkhikW52NXZ/+sgYC2/aS7C+vTBZZx82fL2YiF6eDOB0VZR6yTZggWQjljne8sle1W6wQiHkshbRFeV+pg4sYTXI2LHlzFcsFuyOhF4L6Cykg/v9s8ZvotRGRMLWBHAzCj+iI0pzwCJrHMzN6yiV6JH9ZyN3IrQs2cEoy+dbV/UxztPIc5f6tns+5jGeQtkUL102c+NFQ19qmU1kU+lbZ91+/42pbndJrrdyCdpeAmMDQSqe+7/M+gOwDCKyjNZigeKfLv0tXH [email protected]
服务端保存客户端的公钥
vim .ssh/authorized_keys //放入客户端的公钥
客户端连接服务端
[[email protected] apeng]# ssh [email protected]
The authenticity of host '192.168.221.20 (192.168.221.20)' can't be established.
ECDSA key fingerprint is SHA256:UiIDDUfExrEZLxrI8+z6PWjWsNCO2GTDDfTKpEhQaWY.
ECDSA key fingerprint is MD5:4e:79:bd:c6:bb:8d:b7:ee:1a:a4:cb:25:03:22:10:5f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.221.20' (ECDSA) to the list of known hosts.
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to 192.168.221.20 closed.
服务端建立裸仓库,修改仓库的属主,属组
[[email protected]-002 git]# mkdir -p /data/gitroot
[[email protected]-002 git]# cd /data/gitroot
[[email protected]-002 gitroot]# git init --bare sample.git
初始化空的 Git 版本库于 /data/gitroot/sample.git/
[[email protected]-002 gitroot]# ls
sample.git
[[email protected]-002 gitroot]# chown -R git:git sample.git/
客户端克隆远程的仓库,建立一些文件推送到服务端
[[email protected] ~]# mkdir /copy
[[email protected] ~]# cd /copy
[[email protected] copy]# git clone [email protected]:/data/gitroot/sample.git
[[email protected] copy]# cd sample/
[[email protected] sample]# echo "client" > client.txt
[[email protected] sample]# git add client.txt
[[email protected] sample]# git commit -m "add client.txt"
[master(根提交) 61a074d] add client.txt
1 file changed, 1 insertion(+)
create mode 100644 client.txt
[[email protected] sample]# git push
[[email protected] sample]# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 217 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]192.168.221.20:/data/gitroot/sample.git
* [new branch] master -> master
在客户端上再次建立一些文件,看看情况
[[email protected] sample]# echo "client1" > client1.txt
[[email protected] sample]# git add client1.txt
[[email protected] sample]# git commit -m "add client1.txt"
[[email protected] sample]# git push
在客户端上将服务端上的新的变更克隆到另外一个目录下
[[email protected] sample]# mkdir /update
[[email protected] sample]# cd /update
[[email protected] update]# git clone [email protected]:/data/gitroot/sample.git
[[email protected] update]# ls sample/
client1.txt client.txt
修改/copy/sample/client1.txt文件,推送到服务端,到/update/sample获取更新的数据
[[email protected] sample]# git add client1.txt
[[email protected] sample]# git commit -m "modify client1.txt"
[master a89467d] modify client1.txt
1 file changed, 1 insertion(+)
[[email protected] sample]# git push
[[email protected] sample]# cd /update/sample/
[[email protected] sample]# git pull