温馨提示×

Linux GitLab如何集成LDAP

小樊
38
2026-07-28 14:35:51
栏目: 智能运维

在 Linux 上部署的 GitLab 集成 LDAP 主要用于统一账号登录(认证),常见场景是接入 OpenLDAP / Active Directory。下面按通用做法 + 示例配置 + 常见问题来说明。


一、GitLab 支持哪些 LDAP

GitLab 支持:

  • OpenLDAP
  • Microsoft Active Directory
  • 389 Directory Server
  • FreeIPA

GitLab CE / EE 都支持 LDAP
EE 还支持 LDAP 群组同步(Group Sync)


二、修改 GitLab LDAP 配置

GitLab 的 LDAP 配置在:

/etc/gitlab/gitlab.rb

1️⃣ 打开配置文件

sudo vim /etc/gitlab/gitlab.rb

三、基础 LDAP 集成示例(OpenLDAP)

示例环境

项目
LDAP 服务器 ldap://192.168.1.10
端口 389
Base DN dc=example,dc=com
Bind DN cn=admin,dc=example,dc=com
用户登录属性 uid
用户 DN ou=people,dc=example,dc=com

2️⃣ 配置 LDAP

gitlab_rails['ldap_enabled'] = true

gitlab_rails['ldap_servers'] = YAML.load <<-'EOS'
main:
  label: 'LDAP'
  host: '192.168.1.10'
  port: 389
  uid: 'uid'
  bind_dn: 'cn=admin,dc=example,dc=com'
  password: 'ldap_admin_password'
  encryption: 'plain'
  base: 'dc=example,dc=com'
  user_filter: ''
EOS

3️⃣ 重新配置 GitLab

sudo gitlab-ctl reconfigure

四、Active Directory(AD)示例

gitlab_rails['ldap_servers'] = YAML.load <<-'EOS'
main:
  label: 'AD'
  host: 'ad.example.com'
  port: 389
  uid: 'sAMAccountName'
  bind_dn: 'cn=gitlab,ou=service,dc=example,dc=com'
  password: 'password'
  encryption: 'plain'
  base: 'dc=example,dc=com'
  user_filter: ''
EOS

AD 常用 uid:

  • sAMAccountName(推荐)
  • userPrincipalName

五、启用 LDAPS(推荐)

使用 LDAPS(636)

  port: 636
  encryption: 'simple_tls'

或 StartTLS:

  port: 389
  encryption: 'start_tls'

六、验证 LDAP 是否可用

1️⃣ 测试 LDAP 连接

sudo gitlab-rake gitlab:ldap:check

输出示例:

Checking LDAP ...

Server: ldapmain
LDAP authentication... Success
LDAP users with access to your GitLab server (only showing first 100 results)

2️⃣ Web 登录测试

访问 GitLab:

http://gitlab.example.com

登录界面会出现 LDAP 登录选项
使用 LDAP 中的用户名和密码登录


七、用户登录后的行为

行为 说明
首次登录 自动创建 GitLab 用户
用户名 来自 LDAP uid
邮箱 来自 LDAP mail
密码 始终由 LDAP 验证

✅ GitLab 不会存储 LDAP 密码


八、常见可选配置

1️⃣ 限制登录用户(user_filter)

只允许某个组:

user_filter: '(memberOf=cn=gitlab-users,ou=groups,dc=example,dc=com)'

2️⃣ 禁止本地登录(可选)

gitlab_rails['gitlab_signin_enabled'] = false

3️⃣ 自动同步用户(EE)

ldap_servers:
  main:
    group_base: 'ou=groups,dc=example,dc=com'
    admin_group: 'gitlab-admins'

九、常见问题排查

❌ 登录失败

sudo gitlab-ctl tail | grep ldap

检查:

  • Bind DN 是否有权限
  • Base DN 是否正确
  • uid 是否匹配

❌ 中文用户乱码 / 属性缺失

确认 LDAP 中存在:

  • mail
  • cn
  • sn

十、总结流程

安装 GitLab
↓
配置 /etc/gitlab/gitlab.rb
↓
gitlab-ctl reconfigure
↓
gitlab-rake gitlab:ldap:check
↓
Web 登录测试

如果你愿意,我可以帮你:

✅ 针对 OpenLDAP / AD / FreeIPA 写完整配置
✅ 排查你当前的 gitlab.rb
✅ 配置 LDAP + 群组同步 + 权限控制

只要把你的 LDAP 类型 + 示例结构发我即可。

0