因为git是通过ssh协议实现的,所以权限控制与Linux的权限控制一致,即可以通过ssh key文件登录,安全性更高。也可以设置用户名密码登录,这样无需分发与管理key文件。这里主要介绍如何搭建git服务器并通过用户组设置户用户统一Git权限。
服务器端安装 git server
apt-get install git-core
服务器创建 /git/ourjs 目录,-p是深度创建目录,此目录所在的文件系统需要支持组权限设置,ntfs文件系统则不支持。
mkdir -p /git/ourjs
切换到 ourjs 目录
cd /git/ourjs
初始化 git 仓库,指定通过用户组进行权限控制
git init --shared=group
创建 git_ourjs 用户组,用于统一权限控制
addgroup git_ourjs
添加一个 test 用户,指定用户组为 git_ourjs,按提示设置密码即可
adduser --ingroup git_ourjs test
将 ourjs 仓库用户组改为 git_ourjs
chgrp -R git_ourjs /git/ourjs
给用户组添加读写权限
chmod -R g+swX /git/ourjs
然后本地可以通过test用户clone
$ git clone ssh://test@192.168.0.101/git/ourjs
结果
Cloning into 'ourjs'...
test@192.168.0.101's password:
Permission denied, please try again.
test@192.168.0.101's password:
warning: You appear to have cloned an empty repository.
不过提交时可能会报错
$ git push
test@192.168.0.101's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 207 bytes | 69.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To ssh://192.168.0.101/git/ourjs
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'ssh://test@192.168.0.101/git/ourjs'
这是由于git默认拒绝了push操作,需要进行设置,修改.git/config
nano /git/ourjs/.git/config
添加如下代码 denyCurrentBranch 即可:
[receive]
denyNonFastforwards = true
denyCurrentBranch = ignore
或者在服务器端运行
git config receive.denyCurrentBranch ignore您可以添加新的用户 test2 到 git_ourjs 组
adduser --ingroup git_ourjs test2
然后在另外一台电脑上用 test2 clone即可:
git clone ssh://test2@192.168.0.101/git/ourjs
然后test2就可以也提交代码到 ourjs 了
相关阅读:
Linux上为git无交互添加最低权限的使用用户
Debian/Ubuntu Linux搭建SVN服务器,并设置开机默认启动
回复 (0)
微信扫码 立即评论
