0%

Git服务器搭建

1.使用ssh命令登录Linux服务器

1
ssh root@104.224.156.100 -p 2022

2.安装Git

在CentOS上的安装命令:

1
2
$ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
$ yum install git

在Debian上的安装命令:

1
2
sudo apt update
sudo apt install git

3.接下来我们 创建一个git用户组和用户,用来运行git服务:

1
2
$ groupadd git
$ useradd git -g git

4.客户端生成ssh公钥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
192:git poet$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/poet/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/poet/.ssh/id_rsa.
Your public key has been saved in /Users/poet/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Rky+UE83syyOMjaKYHeoGduGBaPqnU+rtrcyl7TMdL8 poet@192.168.0.101
The key's randomart image is:
+---[RSA 2048]----+
| o . + |
| = o o + |
| o . + o o |
|. o . o + . |
|oo + .= S . |
|o.X..= * |
|.=.o*.+ . |
|. o+oB. . |
| ..=O+. E. |
+----[SHA256]-----+
192:git poet$ cat /Users/poet/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQVX+tOytAUJq3pKNvIFX2HETKvalq4rlqIFJ04KtKIDykzbct5FxK1jR92oIP+ylEsQGz6dJ3FqIsXSn200UIbGvdCvSX5uSFFu8psV4eX1J7DsA6gcLMXElTEHDr82U6MwFmOR6Vb8RkSwsNzRqv8uDdxrmayvbUnQL3VK+/VNcfTdPl2O4YyrYp/DBtFe8b61QvlmU6x4UaX40Sg/r9/JyFMSEDduse9bvVkqg3xkTpaZqwquY7384Ou2iIr4bzlb5mAjW222foM6YBuBhxlovu5kVhLx4GpPsCcxPBJe8lgiGyAGG4yvJ1CH7Rv658eIFqVUyVeOJGP871pLkR poet@192.168.0.101

5.创建证书登录,如果没有该文件就创建它:

1
2
3
4
5
$ cd /home/git/
$ mkdir .ssh
$ chmod 755 .ssh
$ touch .ssh/authorized_keys
$ chmod 644 .ssh/authorized_keys

6.将生成客户端公钥并复制到服务器上

收集所有需要登录的用户的公钥,公钥位于id_rsa.pub文件中,把我们的公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。
下面我们复制id_rsa.pub里的公钥到服务器的authorized_keys文件中。
首先使用ssh连接远程的服务器,然后进入目录/home/git,然后执行如下命令:

1
[root@localhost git]# vim .ssh/authorized_keys

然后将上面生成的公钥复制到服务器的公钥文件中。复制万保存,查看公钥文件中的内容:

1
2
[root@localhost git]# cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQVX+tOytAUJq3pKNvIFX2HETKvalq4rlqIFJ04KtKIDykzbct5FxK1jR92oIP+ylEsQGz6dJ3FqIsXSn200UIbGvdCvSX5uSFFu8psV4eX1J7DsA6gcLMXElTEHDr82U6MwFmOR6Vb8RkSwsNzRqv8uDdxrmayvbUnQL3VK+/VNcfTdPl2O4YyrYp/DBtFe8b61QvlmU6x4UaX40Sg/r9/JyFMSEDduse9bvVkqg3xkTpaZqwquY7384Ou2iIr4bzlb5mAjW222foM6YBuBhxlovu5kVhLx4GpPsCcxPBJe8lgiGyAGG4yvJ1CH7Rv658eIFqVUyVeOJGP871pLkR poet@192.168.0.101

7.初始化Git仓库

首先我们选定一个目录作为Git仓库,假定是/home/gitrepo/helloword.git,在/home/gitrepo目录下输入命令:

1
2
3
4
5
6
7
$ cd /home
$ mkdir gitrepo
$ chown git:git gitrepo/
$ cd gitrepo

$ git init --bare helloword.git
Initialized empty Git repository in /home/gitrepo/helloword.git/

8.以上命令Git创建一个空仓库,服务器上的Git仓库通常都以.git结尾。然后,把仓库所属用户改为git:

1
$ chown -R git:git helloword.git

9.克隆仓库

1
2
3
4
$ git clone git@104.224.156.100:/home/gitrepo/helloword.git
Cloning into 'helloword'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

104.224.156.100 为 Git 所在服务器 ip ,你需要将其修改为你自己的 Git 服务 ip。

但是我克隆的时候却报错了:

1
2
3
4
5
6
7
8
192:git poet$ git clone git@104.224.156.100:2022:/home/gitrepo/helloword.git
Cloning into 'helloword'...
ssh: connect to host 104.224.156.100 port 22: Connection refused
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
192:git poet$

10.克隆仓库的时候出了点小差错,因为ssh的端口号被我改了,而Git是依赖于ssh,所以Git的连接请求也会失败。第9步的命令应该换一下:

1
$ git clone ssh://git@104.224.156.100:2022/home/gitrepo/helloword.git

如果你添加到服务器的公钥有效果,则上述命令会自动把项目克隆下来,但是如果公钥无效或者生成公钥的时候设置了密码,则克隆的步骤中会提示输入密码。

这样我们的 Git 服务器安装就完成。

11.如果需要继续添加新的仓库,请到服务器的/home/gitrepo目录下输入命令:

1
2
$ git init --bare second.git
Initialized empty Git repository in /home/gitrepo/second.git/

以上命令Git创建一个空仓库,服务器上的Git仓库通常都以.git结尾。然后,把仓库所属用户改为git:

1
$ chown -R git:git second.git

然后你就可以在客户端使用git clone正常的克隆项目了。具体操作命令如下:

1
$ git clone ssh://git@104.224.156.100:2022/home/gitrepo/second.git

12.克隆了空项目,想在本地试下git是否好用,可以试试下面的命令。

创建Readme.txt文件

1
$ touch Readme.txt

查看当前项目的Git仓库的状态

1
$ git status -s

将Readme.txt文件添加到本地的Git仓库

1
$ git add Readme.txt

继续查看本地的Git仓库的状态

1
$ git status -s

将操作提交到本地的Git仓库

1
$ git commit -m "add Readme.txt file"

如果你没有设置本地Git的全局用户名和邮箱,有时候会报错,设置方法如下

1
2
$ git config  --global user.name 你的目标用户名
$ git config --global user.email 你的目标邮箱名

将操作推送到远程的Git仓库的master分支

1
$ git push origin master

在另外一个客户端拉取Git远程仓库master分支的内容

1
$ git pull

在本地按时间倒序查看提交的log记录

1
$ git log

13.禁用shell登录

网上流传在搭建服务器的过程中创建的git用户可以免密登录远程的ssh,试了一把,果不其然登录了。

1
2
3
4
5
192:~ poet$ ssh git@104.224.156.100 -p 2022
[git@localhost ~]$ ls
[git@localhost ~]$ ls -a
. .. .bash_logout .bash_profile .bashrc .ssh
[git@localhost ~]$

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

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

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

改为:

1
2
3
[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每次一登录就自动退出。

修改完再试一把,发现已经连接不了远程ssh了:

1
2
3
4
5
6
7
8
9
192:~ poet$ ssh git@104.224.156.100 -p 2022
Last login: Fri Aug 23 07:56:10 2019 from 222.93.190.219
fatal: What do you think I am? A shell?
Connection to 104.224.156.100 closed.
192:~ poet$ ssh git@104.224.156.100 -p 2022
Last login: Fri Aug 23 07:56:39 2019 from 222.93.190.219
fatal: What do you think I am? A shell?
Connection to 104.224.156.100 closed.
192:~ poet$

14.Git报错收集

  • git clone does not appear to be a git repository
    错误示例:

    1
    2
    3
    4
    5
    6
    poetmacbook-pro:git kris$ git clone ssh://git@10.211.55.4:/home/gitrepo/IMBasic.git
    正克隆到 'IMBasic'...
    fatal: '/home/gitrepo/IMBasic.git' does not appear to be a git repository
    fatal: 无法读取远程仓库。

    请确认您有正确的访问权限并且仓库存在。

    问题产生的原因:git仓库的路径不对,请确认/home/gitrepo/IMBasic.git是否确实存在,还要注意此处应该使用服务器上的绝对路径,不能使用相对路径。

15.关联远程仓库

如果已经关联过远程仓库,可以先删除关联的远程Git仓库

1
$ git remote rm origin

再添加关联的远程仓库

1
$ git remote add origin git@github.com:xxx/xxx.git

或者直接一条命令:

1
$ git remote set-url origin git@github.com:xxx/xxx.git

16.显示remote信息

1
$ git remote show origin

参考链接:
Git 服务器搭建
Git 服务器搭建与客户端安装
git 从远程git服务上拉代码 git服务器非默认端口
Git常用命令总结及其用法说明
如何在Debian 10 Linux上安装Git