GitLab的权限管理需结合系统级目录权限、GitLab内置角色权限及项目/实例级访问控制,以下是具体步骤:
确保GitLab运行用户(默认git)对相关目录有正确权限,避免服务异常。
git用户运行GitLab服务,提升安全性:sudo adduser git --system --shell /bin/bash --gecos 'Git Version Control' --disabled-login --home /home/git
/var/opt/gitlab)需归属git用户及组,权限设为755(允许所有者读写执行,其他用户仅读执行):sudo chown -R git:git /var/opt/gitlab
sudo chmod -R 755 /var/opt/gitlab
sudo setenforce 0 # 临时关闭SELinux(测试用)
# 或参考GitLab官方文档配置永久策略:https://docs.gitlab.com/ee/installation/linux/centos/#configure-selinux
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
GitLab通过角色定义用户对项目/实例的访问权限,核心角色包括:
sudo gitlab-rails console
user = User.new(username: 'new_user', email: 'new_user@example.com', password: 'password123', password_confirmation: 'password123')
user.admin = false # 是否设为管理员
user.save
exit
Owner)。通过项目Members功能为用户分配项目角色,步骤如下:
Developer),点击Invite即可。若企业使用LDAP(如Active Directory),可通过同步LDAP组实现权限管理:
/etc/gitlab/gitlab.rb),启用LDAP同步:gitlab_rails['ldap_enabled'] = true
gitlab_rails['ldap_servers'] = YAML.load <<-'EOS'
main:
label: 'LDAP'
host: 'ldap.example.com'
port: 389
uid: 'uid' # LDAP用户标识字段(如uid、sAMAccountName)
method: 'plain' # 认证方式(plain、ssl、start_tls)
bind_dn: 'cn=admin,dc=example,dc=com' # LDAP管理员DN
password: 'ldap_password' # LDAP管理员密码
user_search_base: 'ou=users,dc=example,dc=com' # LDAP用户搜索基础DN
user_search_filter: '(uid=%{username})' # LDAP用户搜索过滤器
EOS
sudo gitlab-ctl reconfigure
cn=developers,ou=groups,dc=example,dc=com),选择对应的GitLab角色(如Developer),保存后LDAP组用户将自动获得项目权限。除角色权限外,可通过以下功能细化项目访问控制:
main)的修改,防止误推送:
main),设置Allowed to push(如Maintainer)、Allowed to merge(如Maintainer),保存后普通开发者无法直接推送至该分支。/var/opt/gitlab)及配置文件(/etc/gitlab/gitlab.rb),确保权限设置在恢复后保持一致:sudo gitlab-rake gitlab:backup:create CRON=1d # 每日备份
通过以上步骤,可实现CentOS系统下GitLab的全面权限管理,兼顾系统安全与团队协作需求。