Skip to content

Instantly share code, notes, and snippets.

@zfkun
Last active August 29, 2015 14:04
Show Gist options
  • Save zfkun/029567e7bd07c0fea958 to your computer and use it in GitHub Desktop.
Save zfkun/029567e7bd07c0fea958 to your computer and use it in GitHub Desktop.
git server with ssh

1. 安装 git server

省略

2. 创建 git 用户 及 .ssh 目录

useradd git --disabled-password
su git
mkdir ~/.ssh

3. 客户端生成公钥并上传到服务器

ssh-keygen -t rsa
ssh-add ~/.ssh/id_rsa // 这步很重要
scp ~/.ssh/id_rsa.pub xxx@xxx.xxx.xxx.xx:/tmp/id_rsa.pub

4. 服务器端追加 公钥 到 git 用户的 .ssh/authorized_keys 文件中

cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*

5. 用 git 用户创建仓库

mkdir -p /data/git_repos
cd /data/git_repos
mkdir test.git
cd test.git
git init --bare --shared

到此仓库创建完毕

6. 客户端直接 clone 项目

git clone git@xxx.xxx.xxx.xxx:/data/git_repos/test.git
cd test
git status

7. 禁用 git 用户登录 及 权限

passwd git -d

移除 git 用户的密码

vim /etc/passwd

修改 git 用户的 shell 为 /usr/bin/git-shell, 这样就只有 git 操作权限了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment