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,如需转载请自行联系原作者
相关文章
|
1月前
|
存储 开发工具 git
[Git] 深入理解 Git 的客户端与服务器角色
Git 的核心设计理念是分布式,每个仓库既可以是客户端也可以是服务器。通过 GitHub 远程仓库和本地仓库的协作,Git 实现了高效的版本管理和代码协作。GitHub 作为远程裸仓库,存储项目的完整版本历史并支持多客户端协作;本地仓库则通过 `.git` 文件夹独立管理版本历史,可在离线状态下进行提交、回滚等操作,并通过 `git pull` 和 `git push` 与远程仓库同步。这种分布式特性使得 Git 在代码协作中具备强大的灵活性和可靠性。
65 18
[Git] 深入理解 Git 的客户端与服务器角色
|
1月前
|
网络安全 Apache 开发工具
图解Git——服务器上的Git《Pro Git》
Git 远程仓库及通信协议简介:远程仓库为团队协作提供平台,支持共享代码。常见形式为裸仓库,仅保存 Git 元数据。Git 支持多种协议,包括本地协议(适合局域网)、HTTP/HTTPS(推荐智能 HTTP,安全易用)、SSH(企业内部协作首选)和 Git 协议(高效只读访问)。选择协议需根据协作需求、安全性和配置难度权衡。此外,搭建 Git 服务器涉及创建裸仓库、上传至服务器、初始化共享仓库等步骤。生成 SSH 公钥、配置服务器及使用 GitWeb 或 GitLab 等工具可进一步增强功能。第三方托管服务如 GitHub 提供便捷的托管选项,适合快速启动和开源项目。总结而言,自行运行服务器提
45 11
|
1月前
|
安全 大数据 Linux
云上体验最佳的服务器操作系统 - Alibaba Cloud Linux | 飞天技术沙龙-CentOS 迁移替换专场
本次方案的主题是云上体验最佳的服务器操作系统 - Alibaba Cloud Linux ,从 Alibaba Cloud Linux 的产生背景、产品优势以及云上用户使用它享受的技术红利等方面详细进行了介绍。同时,通过国内某社交平台、某快递企业、某手机客户大数据业务 3 大案例,成功助力客户实现弹性扩容能力提升、性能提升、降本增效。 1. 背景介绍 2. 产品介绍 3. 案例分享
|
4月前
|
存储 网络协议 Linux
AWS实操-EC2-创建购买linux(centos)EC2服务器
AWS实操-EC2-创建购买linux(centos)EC2服务器
|
4月前
|
弹性计算 关系型数据库 MySQL
CentOS 7.x操作系统的ECS云服务器上搭建WordPress网站
CentOS 7.x操作系统的ECS云服务器上搭建WordPress网站
|
4月前
|
Linux 数据安全/隐私保护 Windows
centos 7.2 搭建svn服务器
centos 7.2 搭建svn服务器
135 0
|
Linux Shell 开发工具
centos7源码包安装git傻瓜式安装步骤
centos7源码包安装git傻瓜式安装步骤
320 0
|
6月前
|
Linux 开发工具 git
CentOS安装git客户端
【8月更文挑战第22天】在 CentOS 上安装 Git 可通过两种方式:一是利用 yum 包管理器,只需在终端依次执行 `sudo yum update` 和 `sudo yum install git` 命令,安装时按提示输入 y 即可;二是从源码安装,适用于有特殊需求的场景。首先安装必要的依赖库,然后下载并解压 Git 的源码包,最后通过一系列 make 命令完成配置与编译安装。无论哪种方式,安装完毕后均可通过 `git --version` 验证安装情况。
282 6
|
6月前
|
存储 Linux 编译器
在 CentOS 6.4 VPS 上安装 Git 的方法
在 CentOS 6.4 VPS 上安装 Git 的方法
68 0
|
6月前
|
存储 Linux 网络安全
在CentOS 7上安装Git的方法
在CentOS 7上安装Git的方法
436 0