This guide walks you through connecting Linux servers to an LDAP directory for centralized user authentication and authorization. You’ll configure the system to authenticate users against an LDAP server (like OpenLDAP or Active Directory), enabling single sign-on and centralized account management. Before starting, ensure you have root or sudo access on the Linux server, the LDAP server’s hostname or IP address, the base DN (distinguished name) of your directory, and credentials for a read-only bind account (or the admin DN if you need to configure TLS).
What You’ll Need
- A Linux server (Ubuntu/Debian or RHEL/CentOS/Fedora) with internet access or local repositories configured.
- The LDAP server address (e.g., ldap.example.com or 192.168.1.10) and port (default 389 for LDAP, 636 for LDAPS).
- The Base DN of your directory (e.g., dc=example,dc=com).
- Bind DN and password for a read-only account (e.g., cn=readonly,dc=example,dc=com).
- If using TLS, the CA certificate of your LDAP server (or the server’s certificate if self-signed).
- Root or sudo access on the Linux server.
Step-by-Step
- Update your system and install required packages.
- On Debian/Ubuntu:
sudo apt update && sudo apt install -y libnss-ldap libpam-ldap ldap-utils - On RHEL/CentOS/Fedora:
sudo dnf install -y openldap-clients nss-pam-ldapd(oryumon older versions).
- On Debian/Ubuntu:
- Configure LDAP client settings.
- Edit
/etc/ldap/ldap.conf(or/etc/openldap/ldap.confon RHEL) to set the base and URI. Add or uncomment lines:BASE dc=example,dc=com URI ldap://ldap.example.com
If using LDAPS, use
ldaps://ldap.example.comand optionally setTLS_CACERT /etc/ssl/certs/ca-certificates.crt(or the path to your CA).
- Edit
- Configure NSS (Name Service Switch) to use LDAP.
- Edit
/etc/nsswitch.conf. Look for lines starting withpasswd,group, andshadow. Addldapto the list. For example:passwd: files ldap group: files ldap shadow: files ldap
- Edit
- Configure PAM (Pluggable Authentication Modules) to authenticate against LDAP.
- On Debian/Ubuntu, run
sudo pam-auth-updateand check the box for “LDAP Authentication” (and optionally “LDAP Profile” for home directory creation). - On RHEL/CentOS, edit
/etc/pam.d/system-auth(orpassword-auth) and addpam_ldap.solines. Typically, add after thepam_unix.solines:auth sufficient pam_ldap.so use_first_pass account sufficient pam_ldap.so password sufficient pam_ldap.so use_authtok session optional pam_ldap.so
- On Debian/Ubuntu, run
- Set LDAP server details for NSS and PAM.
- On Debian/Ubuntu, the installer may ask for LDAP URI, base DN, and bind credentials. If not, edit
/etc/ldap/nss-ldap.conf(or/etc/ldap/ldap.conffor PAM). Set:uri ldap://ldap.example.com base dc=example,dc=com binddn cn=readonly,dc=example,dc=com bindpw your_password
- On RHEL/CentOS, edit
/etc/nslcd.conf(if using nslcd) or/etc/ldap.conf. Set similar parameters.
- On Debian/Ubuntu, the installer may ask for LDAP URI, base DN, and bind credentials. If not, edit
- Enable and start the nslcd service (if installed).
- On Debian/Ubuntu with nslcd:
sudo systemctl enable nslcd && sudo systemctl start nslcd - On RHEL/CentOS with nslcd:
sudo systemctl enable nslcd && sudo systemctl start nslcd
- On Debian/Ubuntu with nslcd:
- Test LDAP connectivity.
- Use
ldapsearchto verify you can query the directory:ldapsearch -x -H ldap://ldap.example.com -b dc=example,dc=com -D cn=readonly,dc=example,dc=com -w your_password. You should see entries returned.
- Use
- Test user authentication.
- Try to log in as an LDAP user via SSH or
su. For example:ssh ldapuser@localhost(if SSH is configured to allow password auth). You can also usegetent passwd ldapuserto see if the user is resolved.
- Try to log in as an LDAP user via SSH or
- Optional: Configure TLS for secure LDAP.
- Copy the CA certificate to
/etc/ssl/certs/and updateldap.confto useldaps://orldap://withTLS_REQCERT allow(ordemandfor strict verification).
- Copy the CA certificate to
- Optional: Enable automatic home directory creation.
- Install
libpam-mkhomedir(Debian/Ubuntu) oroddjob-mkhomedir(RHEL). Then addsession required pam_mkhomedir.so skel=/etc/skel umask=0022to the session section of your PAM configuration (e.g.,/etc/pam.d/common-sessionon Debian).
- Install
Common Mistakes
- Incorrect Base DN – Using the wrong base DN (e.g., missing the
dc=parts) will result in no users being found. - Wrong NSS order – If
filescomes beforeldapand a local user has the same name as an LDAP user, the local one takes precedence. Ensureldapis included and in the desired order. - Binding with a non-existent account – If the bind DN or password is wrong, authentication will fail silently. Double-check credentials.
- Firewall blocking port 389/636 – Ensure the LDAP port is open on both server and client.
- Ignoring TLS certificate errors – If you don’t configure TLS properly, some clients may fail to connect. Use
TLS_REQCERT allowfor testing, but switch todemandin production.
Tips
- Use a dedicated read-only bind account instead of the admin DN to minimize security risks.
- Test with a single user before rolling out to all servers.
- Keep your LDAP client configuration in a version-controlled file (e.g., using Ansible or Puppet) for consistency across servers.
- If you use Active Directory, consider using SSSD (System Security Services Daemon) instead of nslcd for better performance and features like caching.
- Enable caching (e.g., nscd) to reduce LDAP load and speed up lookups.
Troubleshooting
- No users found with
getent passwd– Checknsswitch.conffor theldapentry. Test withldapsearchto ensure the server is reachable and the base DN is correct. Check nslcd status:systemctl status nslcd. - Authentication fails even though ldapsearch works – Verify PAM configuration. Check logs in
/var/log/auth.log(Debian) or/var/log/secure(RHEL). Look for errors like “LDAP connection failed” or “invalid credentials”. Ensure the bind DN has permission to read user passwords (usually not needed if using PAM’s binddn). - SSH login hangs – This often indicates a network issue or LDAP server not responding. Check if the server is reachable and if DNS resolution works. Also, ensure that the SSH server allows password authentication (or keyboard-interactive).
- Home directory not created – If you enabled mkhomedir, ensure the PAM session line is in the correct file (e.g.,
common-sessionon Debian) and that the module is installed. - LDAP connection refused – Check if the LDAP server is listening on the expected port. Use
telnet ldap.example.com 389ornc -vz ldap.example.com 389. Also check firewall rules.
Frequently Asked Questions
Can I use Active Directory as the LDAP server?
Yes, Active Directory supports LDAP. You’ll need to set the base DN to your domain (e.g., dc=example,dc=com) and use a user account that can read directory information. For better integration, consider using SSSD with the AD provider.
Do I need to configure TLS?
It’s strongly recommended, especially if LDAP traffic traverses untrusted networks. Without TLS, credentials are sent in plaintext. You can use LDAPS (port 636) or STARTTLS on port 389.
How do I allow LDAP users to log in via SSH?
By default, SSH uses PAM, so if PAM is configured correctly, LDAP users should be able to log in. Ensure that PasswordAuthentication is set to yes in /etc/ssh/sshd_config (or use public key authentication).
What if a local user has the same username as an LDAP user?
The NSS order determines which one is used. If files comes first, the local user takes precedence. You can change the order to put ldap first, but be careful: this could lock you out if the LDAP server is down.
Can I cache LDAP credentials to allow offline logins?
Yes, using SSSD with the cache_credentials = True option allows users to log in even when the LDAP server is unreachable, as long as they have logged in before.
How do I troubleshoot LDAP authentication issues?
Check the system logs (/var/log/auth.log or /var/log/secure), run ldapsearch manually, and test with getent passwd. Use strace on the login process if needed, but usually logs are sufficient.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Fix Dropbox Error 0x80070780 Access Issues
- Fix iTunes Error 2 on Windows: Step-by-Step Guide
- How to Save Pictures from Google Docs on Phone and PC
- How to Pay with Apple Pay on iPhone, Apple Watch & Apps
- How to Shuffle Playlists on Spotify
- Spotify Not Working on Firestick? Fixes That Work