服务器端建立 git 用户,用来管理 Git 服务,并为 git 用户设置密码git
[root@localhost home]# id git id: git:无此用户 [root@localhost home]# useradd git [root@localhost home]# passwd git
设置 /home/data/git/gittest.git 为 Git 仓库
而后把 Git 仓库的 owner 修改成 gitshell
[root@localhost home]# mkdir -p data/git/gittest.git [root@localhost home]# git init --bare data/git/gittest.git Initialized empty Git repository in /home/data/git/gittest.git/ [root@localhost home]# cd data/git/ [root@localhost git]# chown -R git:git gittest.git/
禁用shell登陆:
出于安全考虑,第二步建立的git用户不容许登陆shell,这能够经过编辑/etc/passwd文件完成。找到相似下面的一行:
git:x:1001:1001:,,,:/home/git:/bin/bash
改成:
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell缓存
git clone、push 提示输入密码但输入后登陆被拒绝
客户端建立 SSH 公钥和私钥
$ ssh-keygen -t rsa -C "447313241@qq.com"
由 AuthorizedKeysFile 得知公钥的存放路径是 .ssh/authorized_keys,其实是 $Home/.ssh/authorized_keys,因为管理 Git 服务的用户是 git,因此实际存放公钥的路径是 /home/git/.ssh/authorized_keys
在 /home/git/ 下建立目录 .ssh安全
把专用密钥(private and public keys)添加到 ssh-agent 的高速缓存中
ssh-add ~/.ssh/id_dsa
ssh-add -d ~/.ssh/id_xxx.pub
若是执行ssh-add时提示”Could not open a connection to your authentication agent”,能够现执行命令:
ssh-agent bash
而后再执行上述 ssh-add 操做
而后能够进行clone等操做了bash
push 出错的解决 (branch is currently checked out)
是因为git默认拒绝了push操做,须要进行设置,修改.git/config文件后面添加以下代码:
[receive]
denyCurrentBranch = ignore服务器