快速搭建git私有服务器

目录
在 Linux 服务器上快速搭建 Git 私有仓库,实现安全的代码托管。包含 git 用户创建、禁用 shell 登录、裸仓库初始化及客户端 clone/push 操作。
为什么要搭建git私有服务器?GitHub不好用吗?GitHub当然是很好用的,但是私有仓库收费啊,我们有一些代码代码不想被别人看到,例如你研究出来的某个0day的exp,或者别的商业项目,是不可能放在github公开的,且不说私有仓库的费用问题,那也是在别人的服务器上啊。所以最好的建议还是搭建自己的git私有服务器。
1 快速安装git
sudo apt install git
2 创建一个git用户
adduser git
passwd git
3 git用户禁用shell登陆
为了安全考虑,建议禁用git用户shell登陆,通过编辑/etc/passwd完成。将类似下面这行:
git:x:1001:1001:,,,:/home/git:/bin/bash
改为
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
这样的好处是可以正常的通过ssh使用git,而不需要登陆shell,因为git-shell指定每次登陆就自动退出。
4 创建一个空仓库
mkdir -p /home/git/test.git
git init --bare /home/git/test.git
chown -R git:git /home/git/test.git // 修改仓库的owner为git
5 客户端操作仓库
- git的url:
ssh://git@xx.xx.xx.xx:22 - git的用户名:
git - git的密码:
上面passwd git修改的密码 - test仓库的url:
ssh://git@xx.xx.xx.xx:22/home/git/test.git
git config --global user.email "ssh://git@xx.xx.xx.xx:22"
git config --global user.name "git"
git clone ssh://git@xx.xx.xx.xx:22/home/git/test.git
touch readme
git add readme
git commit readme -m "readme"
git push