Configure GitLab LDAP authentication and user management with Active Directory integration

Intermediate 45 min Apr 02, 2026 379 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Set up GitLab LDAP authentication with Active Directory to centralize user management, enable automatic user provisioning, and implement group-based access control for your GitLab instance.

Prerequisites

  • GitLab CE or EE installed and running
  • Active Directory domain controller accessible
  • Service account with LDAP read permissions
  • Network connectivity between GitLab server and domain controller

What this solves

GitLab LDAP authentication with Active Directory integration centralizes user management by allowing users to log into GitLab using their existing AD credentials. This eliminates the need to maintain separate user accounts in GitLab, enables automatic user provisioning, and provides group-based access control that synchronizes with your existing organizational structure.

Step-by-step configuration

Install LDAP client tools

Install the necessary LDAP utilities to test connectivity and debug authentication issues before configuring GitLab.

sudo apt update
sudo apt install -y ldap-utils
sudo dnf install -y openldap-clients

Test LDAP connectivity

Verify that your GitLab server can connect to the Active Directory domain controller before configuring GitLab LDAP settings.

ldapsearch -x -H ldap://dc.example.com:389 -D "CN=ldap-reader,OU=Service Accounts,DC=example,DC=com" -W -b "DC=example,DC=com" "(sAMAccountName=testuser)"

Replace dc.example.com with your domain controller, and ldap-reader with a service account that has read access to Active Directory.

Create LDAP service account

Create a dedicated service account in Active Directory for GitLab LDAP authentication. This account needs read access to user and group information.

Note: Perform this step on your Active Directory domain controller or using Active Directory Users and Computers tool.

The service account should have:

  • Read permissions on the Users and Groups organizational units
  • Password set to never expire
  • User cannot change password option enabled
  • Minimum required privileges (no administrative rights)

Backup GitLab configuration

Create a backup of your current GitLab configuration before making changes to ensure you can restore if needed.

sudo cp /etc/gitlab/gitlab.rb /etc/gitlab/gitlab.rb.backup.$(date +%Y%m%d)

Configure GitLab LDAP settings

Edit the GitLab configuration file to add LDAP authentication settings that connect to your Active Directory server.

gitlab_rails['ldap_enabled'] = true
gitlab_rails['ldap_servers'] = {
  'main' => {
    'label' => 'Active Directory',
    'host' => 'dc.example.com',
    'port' => 389,
    'uid' => 'sAMAccountName',
    'bind_dn' => 'CN=gitlab-ldap,OU=Service Accounts,DC=example,DC=com',
    'password' => 'SecureServiceAccountPassword123!',
    'encryption' => 'plain',
    'verify_certificates' => false,
    'active_directory' => true,
    'allow_username_or_email_login' => true,
    'lowercase_usernames' => true,
    'block_auto_created_users' => false,
    'base' => 'DC=example,DC=com',
    'user_filter' => '(&(objectCategory=person)(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))',
    'attributes' => {
      'username' => ['uid', 'userid', 'sAMAccountName'],
      'email' => ['mail', 'email', 'userPrincipalName'],
      'name' => 'displayName',
      'first_name' => 'givenName',
      'last_name' => 'sn'
    },
    'group_base' => 'OU=Groups,DC=example,DC=com',
    'admin_group' => 'CN=GitLab-Admins,OU=Groups,DC=example,DC=com',
    'external_groups' => ['CN=External-Users,OU=Groups,DC=example,DC=com'],
    'sync_ssh_keys' => false
  }
}
Security: Replace the password with your actual service account password and store it securely. Consider using GitLab secrets management for production environments.

Configure LDAP over SSL (recommended)

For production environments, configure LDAPS (LDAP over SSL) to encrypt authentication traffic between GitLab and Active Directory.

gitlab_rails['ldap_servers'] = {
  'main' => {
    'label' => 'Active Directory',
    'host' => 'dc.example.com',
    'port' => 636,
    'uid' => 'sAMAccountName',
    'bind_dn' => 'CN=gitlab-ldap,OU=Service Accounts,DC=example,DC=com',
    'password' => 'SecureServiceAccountPassword123!',
    'encryption' => 'simple_tls',
    'verify_certificates' => true,
    'ca_file' => '/etc/ssl/certs/ad-ca-certificate.pem',
    'ssl_version' => 'TLSv1_2',
    # ... rest of configuration remains the same
  }
}

Copy your Active Directory CA certificate to /etc/ssl/certs/ad-ca-certificate.pem and set appropriate permissions.

sudo chmod 644 /etc/ssl/certs/ad-ca-certificate.pem
sudo chown root:root /etc/ssl/certs/ad-ca-certificate.pem

Configure group synchronization

Set up automatic group synchronization to map Active Directory groups to GitLab groups for access control.

gitlab_rails['ldap_group_sync_worker_cron'] = "0 /12   "
gitlab_rails['ldap_sync_worker_cron'] = "0 /6   "
gitlab_rails['ldap_group_sync_enabled'] = true

This configuration enables group synchronization every 12 hours and user synchronization every 6 hours.

Apply configuration changes

Reconfigure GitLab to apply the LDAP settings and restart all necessary services.

sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart

The reconfigure process may take several minutes to complete as GitLab updates its configuration and restarts services.

Test LDAP authentication

Use GitLab's built-in LDAP check command to verify that the LDAP configuration is working correctly.

sudo gitlab-rake gitlab:ldap:check

This command will test the LDAP connection, authentication, and user search functionality.

Configure automatic user provisioning

Set up automatic user provisioning to create GitLab accounts when users first authenticate via LDAP.

gitlab_rails['omniauth_auto_sign_in_with_provider'] = 'ldapmain'
gitlab_rails['omniauth_block_auto_created_users'] = false
gitlab_rails['omniauth_auto_link_ldap_user'] = true
gitlab_rails['omniauth_auto_link_saml_user'] = false

Apply the changes and restart GitLab.

sudo gitlab-ctl reconfigure

Create GitLab groups from AD groups

Map Active Directory groups to GitLab groups using the GitLab interface or API for organized access control.

sudo gitlab-rails console

In the Rails console, create group mappings:

group = Group.find_by_full_path('developers')
ldap_group_link = group.ldap_group_links.new(
  cn: 'CN=Developers,OU=Groups,DC=example,DC=com',
  group_access: Gitlab::Access::DEVELOPER,
  provider: 'ldapmain'
)
ldap_group_link.save!
exit

Verify your setup

Test the LDAP configuration with multiple verification methods to ensure authentication and user provisioning work correctly.

sudo gitlab-rake gitlab:ldap:check
sudo gitlab-rake gitlab:ldap:group_sync
sudo gitlab-ctl tail gitlab-rails/production.log

Navigate to your GitLab instance and verify:

  • LDAP login option appears on the sign-in page
  • Users can authenticate with AD credentials
  • New users are automatically created with correct attributes
  • Group membership synchronizes properly

Configure user attribute mapping

Customize user attribute synchronization

Fine-tune how Active Directory user attributes map to GitLab user profiles for better user experience.

gitlab_rails['ldap_servers']['main']['attributes'] = {
  'username' => ['sAMAccountName'],
  'email' => ['mail', 'userPrincipalName'],
  'name' => 'displayName',
  'first_name' => 'givenName',
  'last_name' => 'sn',
  'nickname' => 'sAMAccountName'
}

Apply the configuration changes:

sudo gitlab-ctl reconfigure

Troubleshoot LDAP issues

Enable detailed LDAP logging

Configure verbose logging to troubleshoot authentication and synchronization issues.

gitlab_rails['ldap_servers']['main']['debug_level'] = 1
gitlab_rails['log_level'] = 'debug'

Apply changes and monitor logs:

sudo gitlab-ctl reconfigure
sudo gitlab-ctl tail gitlab-rails/production.log

Test specific user authentication

Test LDAP authentication for a specific user to diagnose login issues.

sudo gitlab-rake gitlab:ldap:check RAILS_ENV=production

For testing a specific user:

ldapsearch -x -H ldap://dc.example.com:389 -D "CN=gitlab-ldap,OU=Service Accounts,DC=example,DC=com" -W -b "DC=example,DC=com" "(sAMAccountName=username)" displayName mail sAMAccountName

Verify your setup

sudo gitlab-rake gitlab:ldap:check
sudo gitlab-ctl status
curl -I https://gitlab.example.com
sudo gitlab-rails console -e production

In the Rails console, verify LDAP configuration:

Gitlab::Auth::Ldap::Config.providers
Gitlab::Auth::Ldap::Config.new('ldapmain').options
exit

Common issues

SymptomCauseFix
LDAP tab missing on loginLDAP not enabled or misconfiguredCheck gitlab_rails['ldap_enabled'] = true and run gitlab-ctl reconfigure
Authentication fails with correct passwordWrong bind DN or service account permissionsTest bind DN with ldapsearch and verify service account has read permissions
Users not auto-createdAuto-creation disabled or user filter too restrictiveSet block_auto_created_users to false and check user_filter
SSL certificate errorsMissing CA certificate or wrong SSL configurationInstall AD CA certificate and set correct ca_file path
Group synchronization not workingWrong group base DN or insufficient permissionsVerify group_base path and ensure service account can read group membership
Timeout errors during authenticationNetwork connectivity or DNS issuesTest connectivity with telnet dc.example.com 389 and verify DNS resolution

Next steps

Automated install script

Run this to automate the entire setup

Need help?

Don't want to manage this yourself?

We handle infrastructure security hardening for businesses that depend on uptime. From initial setup to ongoing operations.