1. 编辑GitLab主配置文件

sudo vi /etc/gitlab/gitlab.rb
  • 1.

2.添加LDAP配置模板(根据实际环境修改)

格式一:

gitlab_rails['ldap_enabled'] = true
gitlab_rails['ldap_servers'] = {
  'main' => {
    'label' => 'Company LDAP',  # 登录页显示的名称
    'host' => 'ldap.example.com',
    'port' => 389,              # 636为LDAPS端口
    'uid' => 'sAMAccountName',  # AD使用'sAMAccountName',OpenLDAP通常用'uid'
    'encryption' => 'plain',    # 'start_tls'或'simple_tls'(对应LDAPS)
    'verify_certificates' => true,
    'bind_dn' => 'CN=GitLab Sync,OU=Service Accounts,DC=example,DC=com',
    'password' => 'your_service_account_password',
    'active_directory' => true, # AD设为true,OpenLDAP设为false
    'base' => 'OU=Users,DC=example,DC=com',
    'user_filter' => '(memberOf=CN=GitLab Users,OU=Groups,DC=example,DC=com)',
    
    # 属性映射(根据LDAP结构调整)
    'attributes' => {
      'username' => ['sAMAccountName'],
      'email' => ['mail'],
      'name' => ['displayName'],
      'first_name' => ['givenName'],
      'last_name' => ['sn']
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

格式二:

gitlab_rails['ldap_enabled'] = true
gitlab_rails['ldap_servers'] = YAML.load <<-'EOS'
  'main' :
    label: 'LDAP'
    host: 'ldap1.example.com'
    port: 389             # 636为LDAPS端口
    uid: 'uid'  # AD使用'sAMAccountName',OpenLDAP通常用'uid'
    bind_dn: 'CN=GitLab Sync,OU=Service Accounts,DC=example,DC=com',
    password: 'your_service_account_password'
    encryption: 'plain'    # 'start_tls'或'simple_tls'(对应LDAPS)
    verify_certificates: true
    active_directory: false  # AD设为true,OpenLDAP设为false
    allow_username_or_email_login: true
    base: 'OU=Users,DC=example,DC=com'
   
   'secondary' :
    label: 'LDAP'
    host: 'ldap2.example.com'
    port: 389             # 636为LDAPS端口
    uid: 'uid'  # AD使用'sAMAccountName',OpenLDAP通常用'uid'
    bind_dn: 'CN=GitLab Sync,OU=Service Accounts,DC=example,DC=com',
    password: 'your_service_account_password',
    encryption: 'plain'    # 'start_tls'或'simple_tls'(对应LDAPS)
    verify_certificates: true
    active_directory: false  # AD设为true,OpenLDAP设为false
    allow_username_or_email_login: true+
    base: 'OU=Users,DC=example,DC=com'
EOS
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

参考:

在GitLab中配置LDAP认证指南_用户名

在GitLab中配置LDAP认证指南_用户名_02


3. 配置项详解

参数

说明

示例值

host

LDAP服务器地址

ldap.ad.example.com

port

端口号

389 (明文) / 636 (SSL)

encryption

加密方式

plain/start_tls/simple_tls

bind_dn

只读服务账号DN

CN=gitlab-svc,DC=example,DC=com

base

用户搜索基准DN

OU=Employees,DC=example,DC=com

user_filter

过滤允许登录的用户

(memberOf=CN=gitlab_users,OU=Groups,DC=example,DC=com)

uid

用户名对应字段

AD: sAMAccountNameOpenLDAP: uid


4. 应用配置

sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
  • 1.
  • 2.

5. 验证配置

方法1:通过控制台测试
sudo gitlab-rake gitlab:ldap:check
  • 1.
方法2:手动测试连接
sudo gitlab-rails runner '
  result = Gitlab::Auth::Ldap::Adapter.new("main").search("(sAMAccountName=testuser)")
  puts result.inspect
'
  • 1.
  • 2.
  • 3.
  • 4.

6. 高级配置技巧

多LDAP服务器支持
gitlab_rails['ldap_servers'] = {
  'ad_server' => { ... },  # 第一个LDAP配置
  'openldap' => { ... }    # 第二个LDAP配置
}
  • 1.
  • 2.
  • 3.
  • 4.
证书配置(LDAPS)
  1. 将CA证书放入指定目录:
sudo mkdir -p /etc/gitlab/ssl
sudo cp ldap_ca.crt /etc/gitlab/ssl/
  • 1.
  • 2.
  1. 修改配置:
'encryption' => 'simple_tls',
'verify_certificates' => true,
'ca_file' => '/etc/gitlab/ssl/ldap_ca.crt'
  • 1.
  • 2.
  • 3.