自建服务器

参考:

搭建 Git 服务器

4.4 服务器上的 Git - 配置服务器

在云服务器上自建git服务器,最简单的方式就是创建裸仓库然后用ssh的方式进行代码的拉取和推送,参考创建裸仓库

下面实现一个规范的git服务器,类似第三方服务器(github、coding、gitlab等),其支持sshhttp协议的仓库操作,但不允许进行远程登录

第三方仓库名如下:

  • https://<domain-name>/<user-name>/<repo-name>.git
  • git@<domain-name>:<user-name>/<repo-name>.git

操作步骤如下:

  1. 创建用户git
  2. ssh设置
  3. 下载仓库

创建用户git

$ sudo adduser git
正在添加用户"git"...
正在添加新组"git" (1000)...
正在添加新用户"git" (1000) 到组"git"...
创建主目录"/home/git"...
正在从"/etc/skel"复制文件...
输入新的 UNIX 密码: 
重新输入新的 UNIX 密码: 
passwd: password updated successfully
Changing the user information for git
Enter the new value, or press ENTER for the default
    Full Name []: 
    Room Number []: 
    Work Phone []: 
    Home Phone []: 
    Other []: 
这些信息是否正确? [Y/n]

/data目录下创建目录repositories,然后在里面创建一个裸仓库

# 创建目录
$ sudo mkdir -p /data/repositories
# 设置用户和组
$ sudo chown -R git:git /data/repositories
# 设置可访问权限
$ sudo chmod 755 /data/repositories/
# 创建裸仓库
$ git init --bare hello.git
Initialized empty Git repository in /data/repositories/hello.git/

perl: warning: Falling back to a fallback locale ("en_US.utf8").

$ sudo adduser zhujian
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_MEASUREMENT = "zh_CN.UTF-8",
    LC_PAPER = "zh_CN.UTF-8",
    LC_MONETARY = "zh_CN.UTF-8",
    LC_NAME = "zh_CN.UTF-8",
    LC_ADDRESS = "zh_CN.UTF-8",
    LC_NUMERIC = "zh_CN.UTF-8",
    LC_TELEPHONE = "zh_CN.UTF-8",
    LC_IDENTIFICATION = "zh_CN.UTF-8",
    LC_TIME = "zh_CN.UTF-8",
    LANG = "en_US.utf8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.utf8").
Adding user `zhujian'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+VVpw5kOZBmzJYz/vngYpAAV61Fq9oChSflQFkfzr1sKHRqq2/sqeZD3gzPQZbrKWcHbuGCWyQOvm1gH+67gW+TpUO9DWeeHqo3h5rlCW+ElJcL/q4b+ZBVEmGDjzE+Sg+6wM+izBl5xzHDFeLhN3Yw1OVc2rwQFQ/CD6FSKdL4b5bt0/5rpu65sv7haXjfDMSEsIVgPY5behLzZzoXy81iN4/tPF3cjDsn/x5Yywc60LdslJ5hW5wlozhq1LibUXk9JQu/+5DDZKi8ytMEoe1S7yROvaC/ofJQR22hINnFoLNBC8gSFM2YR+t9oBF0eiAaVwfgddA0+ScYrWA5Yr zj@zj-ThinkPad-T470p ...
Adding new group `zhujian' (1000) ...
Adding new user `zhujian' (1000) with group `zhujian' ...
Creating home directory `/home/zhujian' ...
...
...

提示语言包问题,参考perl: warning: Falling back to a fallback locale ("en_US.UTF-8")

$ sudo apt install locales-all

git is not in the sudoers file. This incident will be reported.

是因为git没有添加到sudo权限,参考xxx is not in the sudoers file.This incident will be reported.的解决方法

  1. 先切换到ubuntu用户

    $ su ubuntu
    
  2. 赋予sudo文件写权限

    $ sudo chmod u+w /etc/sudoers
    
  3. 修改文件/etc/sudoers

    $ sudo vim /etc/sudoers
    # 添加
    youuser ALL=(ALL) ALL
    

    表示允许用户youuser执行sudo命令(需要输入密码)

  4. 撤销sudo文件写权限

    $ sudo chmod u-w /etc/sudoers
    
  5. 重新切换回原来用户

    $ su git
    

设置ssh连接

将客户端的ssh公钥添加到用户git.ssh/authorized_keys文件中,即可实现ssh连接

$ mkdir .ssh && chmod 700 .ssh
$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys

设置git-shell连接

设置用户git仅能操作git相关内容,可以将用户git的登录shell设置为git-shell

#查看用户git登录shell
$ cat /etc/passwd | grep git
git:x:1000:1000:,,,:/home/git:/bin/bash

修改为git-shell

# 查找
$ which git-shell
/usr/bin/git-shell
# 修改
$ sudo vim /etc/passwd
# 修改后
$ cat /etc/passwd | grep git
git:x:1000:1000:,,,:/home/git:/usr/bin/git-shell

publickey denied

ssh连接失败,这个问题很迷,耗了1天时间,网上找了很多资料没有结果,有一个博客主也遇到了同样的问题,他最后解决是因为突然就可以了,我也一样

下载仓库

下载仓库hello.git到本地

$ git clone git@132.232.142.219:/data/repositories/hello.git
Cloning into 'hello'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
$ cd hello/
$ ls -al
total 12
drwxrwxr-x 3 zj zj 4096 3月   9 16:29 .
drwxr-xr-x 4 zj zj 4096 3月   9 16:29 ..
drwxrwxr-x 7 zj zj 4096 3月   9 16:29 .git

添加文件hello.txt,加入版本管理并上传

$ touch hello.txt
$ ls
hello.txt
$ git add hello.txt 
$ git commit -m "hello git"
[master (root-commit) 24265ed] hello git
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 hello.txt
$ git push -u origin master 
Counting objects: 3, done.
Writing objects: 100% (3/3), 207 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@132.232.142.219:/data/repositories/hello.git
* [new branch]      master -> master
Branch master set up to track remote branch master from origin.

配置config

参考:干货 | 简单几步搭建一个远程git服务器

使用config文件可以简易仓库下载地址,在本地~/.ssh文件夹下新建配置文件config,添加如下

$ cat config 
host git-server
    user server
    hostname 132.232.142.219
    port 22
    identityfile ~/.ssh/tencent_id_rsa
# 设置访问权限
$ sudo chmod 600 config

注意:用tab键保持文档格式

config文件指定了主机名git-server配置的用户名、ip地址、端口号和私钥地址

添加配置文件后下载仓库地址如下

$ git clone git-server:/data/repositories/hello.git
Cloning into 'hello'...
remote: Counting objects: 3, done.
Receiving objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Checking connectivity... done.