`

CentOS7安装git服务器

 
阅读更多

CentOS7安装git服务器

一、前言

 

二、搭建Git服务器

 

  • yum安装Git服务器

  • 创建一个git用户,用来运行git服务

  • 创建客户端登录证书

  • 初始化Git仓库

  • 禁用shell登录

  • 克隆远程仓库

 

三、安装客户端

 

  • Windows 客户端

  • Linux 客户端

二、搭建Git服务器

 

1.yum安装Git服务器

[root@git ~]# yum install -y git

 


2.创建一个git用户并修改git的密码,用来运行git服务

 

[root@git ~]# adduser git

[root@localhost home]# passwd git
Changing password for user git.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost home]#

 

 


3.创建客户端登录证书

 

注,收集所有需要登录的用户的公钥,就是他们自己生成的id_rsa.pub文件,把所有公钥复制到/home/git/.ssh/authorized_keys文件里,一行一个。嘿嘿!

 

1).客户端生成id_rsa.pub文件的命令

 

xuecheng@yuxuecheng MINGW64 /d/git_local
$ ssh-keygen.exe -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/xuecheng/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/xuecheng/.ssh/id_rsa.
Your public key has been saved in /c/Users/xuecheng/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:1zr/MqdjFjowL1FeSwmonyhVENfydmSEpBOzt2Q8u+s xuecheng@yuxuecheng
The key's randomart image is:
+---[RSA 2048]----+
|      oo++.o.    |
|       .+*+ o    |
|       o+o*+ .   |
|      o  =+==    |
|     . oS=++..   |
|    . . *..oo    |
|     .   =+. .   |
|        . ++* .  |
|         oE+oB.  |
+----[SHA256]-----+

xuecheng@yuxuecheng MINGW64 /d/git_local
xuecheng@yuxuecheng MINGW64 ~
$ cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8tg2eBatxK54P8RjpdSduK+E2WtO+A68Uu06WJUFCRO9u/C98MHK8N9vU4Hc85vO/gCpQ+t3ihgZ81IJ3mve2Zq2VGLq3uFo+FjGOBaIyK3ShXELizgWFKbaASbMR7Bm1iCM0C5Vuua1h5zXXbT8viXxNt43J1pOLTEOJmhDstvfPbLeU6Pme+wLMO5rV+iCJhRoqkd85SX7e7Sov6R63JMggEQaaN6BYQCv7qgM+ue08ElXj4cSSwA6KUtxgqfb3rT370QlC4QIr0aR4EKNfkLnZ58Yzym0hqUhnqv02zIPj60Zp/RbpZIC56Lur53Dq2G0gl+Thiqi8fVCngGcF xuecheng@yuxuecheng

注,一路回车即可,将生成的id_rsa.pub,复制给管理员,帮你在服务器上增加一下,下次你用git时就不需要输入用户名和密码了。

 

2).查看服务器上authorized_keys文件

 

[git@localhost .ssh]$ cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8tg2eBatxK54P8RjpdSduK+E2WtO+A68Uu06WJUFCRO9u/C98MHK8N9vU4Hc85vO/gCpQ+t3ihgZ81IJ3mve2Zq2VGLq3uFo+FjGOBaIyK3ShXELizgWFKbaASbMR7Bm1iCM0C5Vuua1h5zXXbT8viXxNt43J1pOLTEOJmhDstvfPbLeU6Pme+wLMO5rV+iCJhRoqkd85SX7e7Sov6R63JMggEQaaN6BYQCv7qgM+ue08ElXj4cSSwA6KUtxgqfb3rT370QlC4QIr0aR4EKNfkLnZ58Yzym0hqUhnqv02zIPj60Zp/RbpZIC56Lur53Dq2G0gl+Thiqi8fVCngGcF xuecheng@yuxuecheng
[git@localhost .ssh]$

 

 

4.初始化Git仓库

 

注,先选定一个目录作为Git仓库,这里是/home/git/repo/trunk。

 

[git@localhost trunk]$ cd /home/git/repo/trunk

 

[git@localhost trunk]$ git init --bare
Initialized empty Git repository in /home/git/repo/trunk/

 

[git@localhost trunk]$ ls
branches  config  description  HEAD  hooks  info  objects  refs

 


执行以上命令 Git命令,会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾。然后,把owner改为git:

 

[git@localhost repo]$ chown -R git:git trunk/
[git@localhost repo]$ ls
trunk
[git@localhost repo]$ cd trunk/
[git@localhost trunk]$ ll
total 16
drwxrwxr-x. 2 git git    6 Dec  2 09:14 branches
-rw-rw-r--. 1 git git   66 Dec  2 09:14 config
-rw-rw-r--. 1 git git   73 Dec  2 09:14 description
-rw-rw-r--. 1 git git   23 Dec  2 09:14 HEAD
drwxrwxr-x. 2 git git 4096 Dec  2 09:14 hooks
drwxrwxr-x. 2 git git   20 Dec  2 09:14 info
drwxrwxr-x. 4 git git   28 Dec  2 09:14 objects
drwxrwxr-x. 4 git git   29 Dec  2 09:14 refs

 


5.禁用shell登录

 

注,出于安全考虑,第二步创建的git用户不允许登录shell,这可以通过编辑/etc/passwd文件完成。找到类似下面的一行:

 

[root@git ~]# cat /etc/passwd | grep git

 

git:x:1001:1001:git version control:/home/git:/bin/bash

 

改为:

 

[root@git ~]# vim /etc/passwd

 

git:x:1001:1001:git version control:/home/git:/usr/bin/git-shell

 

这样,git用户可以正常通过ssh使用git,但无法登录shell,因为我们为git用户指定的git-shell每次一登录就自动退出。

 

6.克隆远程仓库

 

注,现在可以通过git clone命令克隆远程仓库了,在各自的电脑上运行:

 

xuecheng@yuxuecheng MINGW64 /d/git_local
$ git clone git@192.168.56.102:/home/git/repo/trunk/
Cloning into 'trunk'...
The authenticity of host '192.168.56.102 (192.168.56.102)' can't be established.
ECDSA key fingerprint is SHA256:7NWrLoG3/WUzKUPuUpZIOOrNG/w3bRZ5joxxCq+A0D0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.56.102' (ECDSA) to the list of known hosts.
git@192.168.56.102's password:
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

xuecheng@yuxuecheng MINGW64 /d/git_local

 

 

注,git clone git@192.168.56.102:/home/git/repo/trunk/,其中git是用户名,192.168.56.102是服务器IP地址,/home/git/repo/trunk/是仓库路径。好了,到这里服务器的搭建到这里就完成了。

 

git从已有文件初始化仓库

切换到用来创建仓库的根目录

  1. git init
  2. git add .
  3. git commit:提交
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics