GitLab教程(3): 用户和组管理
GitLab的用户和组管理是权限控制的基础。本文将详细介绍如何管理用户、创建组和配置权限。
用户管理
创建用户
# 方式1: Web界面创建
# Admin Area (扳手图标) > Users > New user
填写信息:
- Name: 张三
- Username: zhangsan
- Email: zhangsan@example.com
- Access level: Regular 或 Admin
- 可选: Projects limit, Can create group
点击 "Create user"
# 用户会收到设置密码的邮件
# 方式2: 命令行创建
sudo gitlab-rails console
> user = User.new(
username: 'lisi',
email: 'lisi@example.com',
name: '李四',
password: 'SecurePass123!',
password_confirmation: 'SecurePass123!'
)
> user.skip_confirmation! # 跳过邮箱确认
> user.save!
=> true
> exit
# 方式3: 通过API创建
curl --request POST \
--header "PRIVATE-TOKEN: your-admin-token" \
--header "Content-Type: application/json" \
--data '{
"email": "wangwu@example.com",
"name": "王五",
"username": "wangwu",
"password": "SecurePass123!",
"skip_confirmation": true
}' \
"https://gitlab.example.com/api/v4/users"
# 响应
{
"id": 5,
"username": "wangwu",
"name": "王五",
"state": "active",
"email": "wangwu@example.com",
...
}
用户角色
# GitLab用户访问级别
级别 数值 权限说明
──────────────────────────────────────────────────
Guest 10 查看项目、创建Issue
Reporter 20 查看代码、拉取代码、创建Issue
Developer 30 推送代码、创建分支、创建MR
Maintainer 40 管理分支保护、合并MR、管理成员
Owner 50 完全控制,删除项目
# 权限对比表
功能 Guest Reporter Developer Maintainer Owner
─────────────────────────────────────────────────────────────────────
查看项目 ✓ ✓ ✓ ✓ ✓
创建Issue ✓ ✓ ✓ ✓ ✓
查看代码 ✗ ✓ ✓ ✓ ✓
克隆代码 ✗ ✓ ✓ ✓ ✓
创建分支 ✗ ✗ ✓ ✓ ✓
推送代码 ✗ ✗ ✓ ✓ ✓
创建合并请求 ✗ ✗ ✓ ✓ ✓
合并代码 ✗ ✗ ✗ ✓ ✓
管理分支保护 ✗ ✗ ✗ ✓ ✓
管理成员 ✗ ✗ ✗ ✓ ✓
项目设置 ✗ ✗ ✗ ✓ ✓
删除项目 ✗ ✗ ✗ ✗ ✓
用户管理操作
# 禁用用户
# Admin Area > Users > 选择用户 > Block
# 命令行禁用
sudo gitlab-rails console
> user = User.find_by(username: 'zhangsan')
> user.block!
=> true
# 删除用户
# Admin Area > Users > 选择用户 > Delete user
# 或
# Delete user and contributions (同时删除用户创建的内容)
# 重置用户密码
sudo gitlab-rake "gitlab:password:reset[zhangsan]"
Enter password:
Confirm password:
Password successfully updated for user with username zhangsan.
# 批量导入用户 (通过API)
#!/bin/bash
for user in user1 user2 user3; do
curl --request POST \
--header "PRIVATE-TOKEN: your-token" \
--header "Content-Type: application/json" \
--data "{
\"email\": \"${user}@example.com\",
\"name\": \"${user}\",
\"username\": \"${user}\",
\"password\": \"TempPass123!\",
\"skip_confirmation\": true
}" \
"https://gitlab.example.com/api/v4/users"
done
组管理
创建组
# 组织结构示例
公司/
├── 前端团队/
│ ├── web-app
│ ├── mobile-app
│ └── component-library
├── 后端团队/
│ ├── api-gateway
│ ├── user-service
│ └── order-service
├── 运维团队/
│ ├── infrastructure
│ ├── monitoring
│ └── ci-templates
└── 共享/
├── docs
└── tools
# 创建组
# Menu > Groups > Create group
填写信息:
- Group name: 前端团队
- Group URL: frontend
- Visibility: Private
- 可选: Description
点击 "Create group"
# 创建子组 (Subgroup)
# 进入父组 > New subgroup
或
# 创建组时选择父组
# 通过API创建组
curl --request POST \
--header "PRIVATE-TOKEN: your-token" \
--header "Content-Type: application/json" \
--data '{
"name": "后端团队",
"path": "backend",
"visibility": "private"
}' \
"https://gitlab.example.com/api/v4/groups"
# 响应
{
"id": 10,
"name": "后端团队",
"path": "backend",
"full_path": "backend",
"visibility": "private",
...
}
组成员管理
# 添加成员到组
# Group > Members > Invite members
选择用户: zhangsan
角色: Developer
访问到期日期: 可选
点击 "Invite"
# 通过API添加成员
curl --request POST \
--header "PRIVATE-TOKEN: your-token" \
--data "user_id=5&access_level=30" \
"https://gitlab.example.com/api/v4/groups/10/members"
# access_level 值:
# 10 = Guest
# 20 = Reporter
# 30 = Developer
# 40 = Maintainer
# 50 = Owner
# 列出组成员
curl --header "PRIVATE-TOKEN: your-token" \
"https://gitlab.example.com/api/v4/groups/10/members"
# 响应
[
{
"id": 5,
"username": "wangwu",
"name": "王五",
"access_level": 30,
"expires_at": null
},
...
]
# 修改成员权限
curl --request PUT \
--header "PRIVATE-TOKEN: your-token" \
--data "access_level=40" \
"https://gitlab.example.com/api/v4/groups/10/members/5"
# 移除成员
curl --request DELETE \
--header "PRIVATE-TOKEN: your-token" \
"https://gitlab.example.com/api/v4/groups/10/members/5"
项目权限管理
# 项目可见性级别
1. Private (私有)
- 只有项目成员可以访问
- 适合内部项目
2. Internal (内部)
- 所有登录用户可以访问
- 适合公司内部开源
3. Public (公开)
- 任何人可以访问
- 适合开源项目
# 设置项目可见性
# Project > Settings > General > Visibility
# 添加成员到项目
# Project > Members > Invite members
# 项目继承组权限
# 组成员自动继承组内所有项目的访问权限
# 可以在项目级别覆盖(提高权限,不能降低)
# 示例:组和项目权限关系
前端团队 (Group)
├── zhangsan: Developer
├── lisi: Maintainer
└── web-app (Project)
├── zhangsan: Developer (继承自组)
├── lisi: Maintainer (继承自组)
└── wangwu: Reporter (项目单独添加)
访问令牌
# 个人访问令牌 (Personal Access Token)
# 用于API调用和Git操作
# 创建个人访问令牌
# User Settings > Access Tokens
填写:
- Token name: ci-token
- Expiration date: 2026-12-31
- Select scopes:
- api: 完整API访问
- read_api: 只读API访问
- read_user: 读取用户信息
- read_repository: 读取仓库
- write_repository: 写入仓库
- read_registry: 读取容器镜像
- write_registry: 写入容器镜像
点击 "Create personal access token"
# 保存生成的token(只显示一次)
glpat-xxxxxxxxxxxxxxxxxxxx
# 使用Token进行Git操作
git clone https://oauth2:glpat-xxxxxxxxxxxx@gitlab.example.com/group/project.git
# 使用Token调用API
curl --header "PRIVATE-TOKEN: glpat-xxxxxxxxxxxx" \
"https://gitlab.example.com/api/v4/projects"
# 项目访问令牌
# Project > Settings > Access Tokens
# 用于CI/CD或自动化任务
# 组访问令牌
# Group > Settings > Access Tokens
# 用于组级别的自动化任务
SSH密钥管理
# 生成SSH密钥
ssh-keygen -t ed25519 -C "your-email@example.com"
# 输出
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/user/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_ed25519
Your public key has been saved in /home/user/.ssh/id_ed25519.pub
# 查看公钥
cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKxxxxxxxxxxxxxx your-email@example.com
# 添加SSH密钥到GitLab
# User Settings > SSH Keys
# 粘贴公钥内容
# 填写Title: My Laptop
# 设置过期日期 (可选)
# 点击 "Add key"
# 测试SSH连接
ssh -T git@gitlab.example.com
# 输出
Welcome to GitLab, @zhangsan!
# 使用SSH克隆
git clone git@gitlab.example.com:group/project.git
LDAP/AD集成
# /etc/gitlab/gitlab.rb
gitlab_rails['ldap_enabled'] = true
gitlab_rails['ldap_servers'] = {
'main' => {
'label' => 'LDAP',
'host' => 'ldap.example.com',
'port' => 389,
'uid' => 'sAMAccountName',
'bind_dn' => 'cn=admin,dc=example,dc=com',
'password' => 'admin-password',
'encryption' => 'plain',
'verify_certificates' => true,
'active_directory' => true,
'base' => 'dc=example,dc=com',
'user_filter' => '(memberOf=cn=gitlab-users,ou=groups,dc=example,dc=com)'
}
}
# 应用配置
sudo gitlab-ctl reconfigure
# 测试LDAP配置
sudo gitlab-rake gitlab:ldap:check
# 输出
Checking LDAP ...
Server: ldapmain
LDAP authentication... Success
LDAP users with access to your GitLab server (only showing the first 100 results):
DN: cn=zhangsan,ou=users,dc=example,dc=com uid: zhangsan
最佳实践
- 使用组管理权限:通过组分配权限,而非项目单独添加
- 最小权限原则:给予用户完成工作所需的最低权限
- 定期审查成员:定期检查并移除不活跃用户
- 使用访问令牌:CI/CD使用令牌而非个人密码
- 设置令牌过期:所有令牌应设置过期时间
- 启用双因素认证:对管理员账户启用2FA
总结
本文介绍了GitLab的用户和组管理,包括创建用户、配置权限、管理组和访问令牌等内容。良好的权限管理是团队协作的基础。
下一篇我们将学习如何创建和管理GitLab项目。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。







