38

Git服务器搭建,多用户组权限控制配置,添加新用户密码登陆

 5 years ago
source link: http://ourjs.com/detail/5b503ea37ad90c6e47f34c63?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

因为git是通过ssh协议实现的,所以权限控制与Linux的权限控制一致,即可以通过ssh key文件登录,安全性更高。也可以设置用户名密码登录,这样无需分发与管理key文件。这里主要介绍如何搭建git服务器并通过用户组设置户用户统一Git权限。

使用管理模式,这样下面的命令不用每次都输入 sudo

sudo -s

服务器端安装 git server

apt-get install git-core

创建 /git/ourjs 目录,-p是深度创建目录

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://[email protected]/git/ourjs

结果

Cloning into 'ourjs'...
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
warning: You appear to have cloned an empty repository.

不过提交时可能会报错

$ git push
[email protected]'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://[email protected]/git/ourjs'

这是由于git默认拒绝了push操作,需要进行设置,修改.git/config

nano /git/ourjs/.git/config

添加如下代码即可:

    [receive]
        denyNonFastforwards = true
        denyCurrentBranch = ignore

您可以添加新的用户 test2 到 git_ourjs 组

adduser --ingroup git_ourjs test2

然后在另外一台电脑上用 test2 clone即可:

git clone ssh://[email protected]/git/ourjs

然后test2就可以也提交代码到 ourjs 了

相关阅读:

Debian/Ubuntu Linux搭建SVN服务器,并设置开机默认启动

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK