设为首页 收藏本站
查看: 596|回复: 0

[经验分享] Centos 7.3搭建git服务器

[复制链接]

尚未签到

发表于 2018-4-19 16:14:50 | 显示全部楼层 |阅读模式
  服务器端:Centos 7.3环境搭建git服务器
  客户端:git客户端可以是windows、linux和mac
  
  1、git服务器和客户端都安装Git

[root@localhost ~]# yum install git  
2、git服务器上创建一个git用户组和用户,用来运行git服务

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

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

[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)查看客户端生成的公钥。

[root@localhost ~]# cat ~/.ssh/id_rsa.pub  
(3)git服务器上创建/home/git/.ssh/authorized_keys文件,并设置权限。

[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验证。

vi /etc/ssh/sshd_config
找到PasswordAuthentication节点并设置为no;
开启RSA认证,将前面的#去掉,并确保如下配置:
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys  
(6)git服务器上重启SSH服务使配置生效:

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

[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、客户端操作,克隆仓库

[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、客户端操作,提交文件

[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这个时间知道是否提交了

[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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-449185-1-1.html 上篇帖子: Centos7下的Openssl和CA 下篇帖子: centOS 下 Linux 中的 JDK 配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表