Centos 7.3搭建git服务器

简介:

服务器端:Centos 7.3环境搭建git服务器

客户端:git客户端可以是windows、linux和mac


1、git服务器和客户端都安装Git

1
[root@localhost ~] # yum install git


2、git服务器上创建一个git用户组和用户,用来运行git服务

1
2
[root@localhost ~] # groupadd git
[root@localhost ~] # useradd git -g git


3、创建证书登录(如果用ssh key操作,要操作这步。如果用密码登录不需要操作这步)
收集所有需要登录的客户端的公钥,公钥位于id_rsa.pub文件中。ssh key可以让客户端与git服务器安全加密连接,而且不需要输入密码。

(1)客户端生成公钥和私钥。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@localhost ~] # ssh-keygen -t rsa -C "442102293@qq.com"
Generating public /private  rsa key pair.
Enter  file  in  which  to save the key ( /root/ . ssh /id_rsa ): 
Created directory  '/root/.ssh' .
Enter passphrase (empty  for  no passphrase): 
Enter same passphrase again: 
Your identification has been saved  in  /root/ . ssh /id_rsa .
Your public key has been saved  in  /root/ . ssh /id_rsa .pub.
The key fingerprint is:
64:78:e9:5d:72:d0:d5:0c:51:f9: dc :25:ff:b5:5b:d9 442102293@qq.com
The key's randomart image is:
+--[ RSA 2048]----+
|          .. .+*o|
|       . . .. ..+|
|      . = . o  ++|
|       = . +    *|
|        S .     *|
|               oE|
|                o|
|               . |
|                 |
+-----------------+


(2)查看客户端生成的公钥。

1
[root@localhost ~] # cat ~/.ssh/id_rsa.pub


(3)git服务器上创建/home/git/.ssh/authorized_keys文件,并设置权限。

1
2
3
4
5
6
[root@localhost ~] # cd /home/git/
[root@localhost git] # mkdir .ssh
[root@localhost git] # chmod 700 .ssh
[root@localhost git] # chown -R git.git .ssh
[root@localhost git] # touch .ssh/authorized_keys
[root@localhost git] # chmod 600 .ssh/authorized_keys   (网上还有说法最好644)


(4)把客户端公钥内容复制到/home/git/.ssh/authorized_keys文件

(5)git服务器上修改ssh配置文件,将密码验证关掉,开启ssh key验证。

1
2
3
4
5
6
vi  /etc/ssh/sshd_config
找到PasswordAuthentication节点并设置为no;
开启RSA认证,将前面的 #去掉,并确保如下配置:
RSAAuthentication  yes
PubkeyAuthentication  yes
AuthorizedKeysFile . ssh /authorized_keys


(6)git服务器上重启SSH服务使配置生效:

1
2
[root@localhost git] # systemctl restart sshd
[root@localhost git] # service sshd restart


4、git服务器上初始化Git仓库
首先我们选定一个目录作为Git仓库,比如是/home/gitrepo/runoob.git(叫这个名字,是因为参考完善别的文章):

1
2
3
4
5
6
7
8
[root@localhost git] # cd /home
[root@localhost home] # mkdir gitrepo
[root@localhost home] # chown git:git gitrepo/
[root@localhost home] # cd gitrepo
[root@localhost gitrepo] # git init --bare runoob.git
初始化空的 Git 版本库于  /home/gitrepo/runoob .git/
[root@localhost gitrepo] # chown -R git:git runoob.git
备注:服务器上的Git仓库名一般都以.git结尾。然后,把仓库所属用户改为git:


5、客户端操作,克隆仓库

1
2
3
4
[root@localhost ~] # mkdir testdata
[root@localhost testdata] # git clone git@192.168.1.109:/home/gitrepo/runoob.git
Initialized empty Git repository  in  /root/testdata/runoob/ .git/
warning: You appear to have cloned an empty repository.


6、客户端操作,提交文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost testdata] # cd runoob/
[root@localhost runoob] # vi test.sh
[root@localhost runoob] # git add test.sh 
[root@localhost runoob] # git commit -m "测试"
[master (root-commit) ee961b2] 测试
  1 files changed, 1 insertions(+), 0 deletions(-)
  create mode 100644  test .sh
[root@localhost runoob] # git status
# On branch master
nothing to commit (working directory clean)
[root@localhost runoob] # git log
commit ee961b270d4541ff7440765a4c32d9ea722e3611
Author: gxm <gxm@ test .com>
Date:   Sun May 22 09:02:40 2016 +0800
     测试
[root@localhost runoob] # git remote -v
origin    git@192.168.1.109: /home/gitrepo/runoob .git (fetch)
origin    git@192.168.1.109: /home/gitrepo/runoob .git (push)
[root@localhost runoob] # git push origin master
Counting objects: 3,  done .
Writing objects: 100% (3 /3 ), 216 bytes,  done .
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.1.109: /home/gitrepo/runoob .git
  * [new branch]      master -> master


7、git服务器上,可以查看objects这个时间知道是否提交了

1
2
3
4
5
6
7
8
9
10
[root@localhost runoob.git] # ll
总用量 12
drwxr-xr-x.  2 git git   6 9月  14 00:12 branches
-rw-r--r--.  1 git git  66 9月  14 00:12 config
-rw-r--r--.  1 git git  73 9月  14 00:12 description
-rw-r--r--.  1 git git  23 9月  14 00:12 HEAD
drwxr-xr-x.  2 git git 242 9月  14 00:12 hooks
drwxr-xr-x.  2 git git  21 9月  14 00:12 info
drwxr-xr-x. 10 git git  90 9月  14 00:55 objects
drwxr-xr-x.  4 git git  31 9月  14 00:12 refs




本文转自 sailikung 51CTO博客,原文链接:http://blog.51cto.com/net881004/2068517,如需转载请自行联系原作者
相关文章
|
5月前
|
安全 Linux Shell
使用SCP命令在CentOS 7上向目标服务器传输文件
以上步骤是在CentOS 7系统上使用SCP命令进行文件传输的基础,操作简洁,易于理解。务必在执行命令前确认好各项参数,尤其是目录路径和文件名,以避免不必要的传输错误。
538 17
|
4月前
|
Ubuntu 安全 小程序
服务器版本的CentOS和Ubuntu哪个更适合你?
但是以上的比较并不说明Ubuntu是不稳定的或者是不安全的,只是以上比较过程中,在稳定性方面Ubuntu稍微逊色了一点。由于Ubuntu在个人桌面电脑的使用率远远高于CentOS,用Ubuntu搭建服务器,如果遇到什么问题,寻找解决方案相对比较容易,这让Ubuntu在选择方面更优于CentOS。如果你是一个初学者,那么毫无疑问Ubuntu是更适合的选择。如果你正在经营自己的公司,在这两者之间,CentOS会更好一些。
|
5月前
|
安全 关系型数据库 网络安全
安全加固:启动PostgreSQL 14服务器SSL加密的方法指南在CentOS 7环境中
通过上述步骤,你可以为PostgreSQL数据库服务器设置SSL加密,从而增加数据在传输中的安全性。确保维持证书的有效性,并且定期更新和管理密钥,以防止未授权访问。
269 0
|
2月前
|
弹性计算 运维 安全
阿里云轻量应用服务器与云服务器ECS啥区别?新手帮助教程
阿里云轻量应用服务器适合个人开发者搭建博客、测试环境等低流量场景,操作简单、成本低;ECS适用于企业级高负载业务,功能强大、灵活可扩展。二者在性能、网络、镜像及运维管理上差异显著,用户应根据实际需求选择。
273 10
|
2月前
|
运维 安全 Ubuntu
阿里云渠道商:服务器操作系统怎么选?
阿里云提供丰富操作系统镜像,涵盖Windows与主流Linux发行版。选型需综合技术兼容性、运维成本、安全稳定等因素。推荐Alibaba Cloud Linux、Ubuntu等用于Web与容器场景,Windows Server支撑.NET应用。建议优先选用LTS版本并进行测试验证,通过标准化镜像管理提升部署效率与一致性。
|
2月前
|
弹性计算 ice
阿里云4核8g服务器多少钱一年?1个月和1小时价格,省钱购买方法分享
阿里云4核8G服务器价格因实例类型而异,经济型e实例约159元/月,计算型c9i约371元/月,按小时计费最低0.45元。实际购买享折扣,1年最高可省至1578元,附主流ECS实例及CPU型号参考。
415 8
|
2月前
|
存储 监控 安全
阿里云渠道商:云服务器价格有什么变动?
阿里云带宽与存储费用呈基础资源降价、增值服务差异化趋势。企业应结合业务特点,通过阶梯计价、智能分层、弹性带宽等策略优化成本,借助云监控与预算预警机制,实现高效、可控的云资源管理。
|
2月前
|
弹性计算 运维 安全
区别及选择指南:阿里云轻量应用服务器与ECS云服务器有什么区别?
阿里云轻量应用服务器适合个人开发者、学生搭建博客、测试环境,易用且性价比高;ECS功能更强大,适合企业级应用如大数据、高流量网站。根据需求选择:轻量入门首选,ECS专业之选。
261 2
|
2月前
|
弹性计算 ice
阿里云4核8G云服务器配置价格:热门ECS实例及CPU处理器型号说明
阿里云2025年4核8G服务器配置价格汇总,涵盖经济型e实例、计算型c9i等热门ECS实例,CPU含Intel Xeon及AMD EPYC系列,月费159元起,年付低至1578元,按小时计费0.45元起,实际购买享折扣优惠。
525 1