2019年02月08日

达成目标:通过 git cmd 搭建 GIT 仓库服务 另附 web 图形界面方式参:https://github.com/gogs/gogs (a painless self-hosted Git service)

1. 增加 Git 用户,并增加 .ssh 目录。

sudo adduser git
su git
cd
mkdir .ssh && chmod 700 .ssh
touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys

2. 增加你的公钥到 authorized_keys 文件

cat .ssh/id_rsa.pub
粘贴到
vim .ssh/authorized_keys

3. 设置 git 仓库 (git init –bare)

在 喜欢的地方 git 目录作为仓库服务地址 (需要使用有权限的账户)

mkdir git-repository
cd git-repository
mkdir project.git
cd project.git
git init --bare

显示

Initialized empty Git repository in /home/git/git-repository/project.git/

git仓库建好

4. 从远端 clone OR push 到远端

从远端 clone

git clone git@yourGitServer:git-repository/project.git

push 到远端

cd project
git init
git commit -m 'initial commit'
git remote add origin git@yourGitServer:git-repository/project.git
git push --set-upstream origin master

5. 将 git 用户限定为只允许 git 相关操作 (git用户的shell更改为git-shell)

cat /etc/shells       # 看 git-shell 是否已存在. 如果不存在...`
which git-shell       # 确认系统已经安装 git-shell `
sudo -e /etc/shells   # 将 git-shell 的路径(/usr/bin/git-shell) 添加到最后一行`

(修改后 ctrl+x 退出,y 保存, enter 确认)

sudo chsh git -s $(which git-shell) # 更改 git 用户使用的 shell

现在如果尝试用 git 用户登陆服务器会返回以下结果

fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to yourGitServer closed.

6. 禁止通过 SSH port forwarding 访问git服务器上的其他服务。

vim /home/git/.ssh/authorized_keys

在 public_key 的前边增加

no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty

像这样

no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa   
AAAxxxx...

Done!