mirror of
https://github.com/ZJDoc/GitGuide.git
synced 2026-07-29 15:20:25 +08:00
docs(git): add advanced and error
This commit is contained in:
208
docs/git/advanced/自建服务器.md
Normal file
208
docs/git/advanced/自建服务器.md
Normal file
@@ -0,0 +1,208 @@
|
||||
|
||||
# 自建服务器
|
||||
|
||||
在云服务器上自建`git`服务器,最简单的方式就是创建裸仓库然后用`ssh`的方式进行代码的拉取和推送,参考[创建裸仓库](./创建裸仓库.md)
|
||||
|
||||
下面实现一个规范的`git`服务器,类似第三方服务器(`github、coding、gitlab`等),其支持`ssh`和`http`协议的仓库操作,但不允许进行远程登录
|
||||
|
||||
第三方仓库名如下:
|
||||
|
||||
* `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/
|
||||
|
||||
## 设置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
|
||||
|
||||
## 下载仓库
|
||||
|
||||
下载仓库`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
|
||||
|
||||
使用`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.
|
||||
|
||||
## ERROR
|
||||
|
||||
* 问题一:`publickey denied`
|
||||
|
||||
`ssh`连接失败,这个问题很迷,耗了`1`天时间,网上找了很多资料没有结果,有一个博客主也遇到了同样的问题,他最后解决是因为突然就可以了,我也一样
|
||||
|
||||
* 问题二:`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.的解决方法](https://www.cnblogs.com/xiaochaoyxc/p/6206481.html)
|
||||
|
||||
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
|
||||
|
||||
* 问题三:`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")](https://blog.csdn.net/jmpjmpkiss/article/details/55098794)
|
||||
|
||||
$ sudo apt install locales-all
|
||||
|
||||
## 相关阅读
|
||||
|
||||
* [搭建 Git 服务器](https://cloud.tencent.com/developer/labs/lab/10045)
|
||||
|
||||
* [4.4 服务器上的 Git - 配置服务器](https://git-scm.com/book/zh/v2/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-%E9%85%8D%E7%BD%AE%E6%9C%8D%E5%8A%A1%E5%99%A8)
|
||||
|
||||
* [干货 | 简单几步搭建一个远程git服务器](https://www.onelib.biz/blog/a/581047797628141c0fbfb0ed)
|
||||
Reference in New Issue
Block a user