LDAP Authentication with OpenSSH
A complete, production-ready guide to centralize SSH authentication with LDAP using SSSD, PAM, and public key lookup from your directory.
Architecture Overview
How it worksWhen a user connects via SSH to a server configured with LDAP authentication, the following flow occurs:
Key components:
- SSSD — Handles LDAP communication, caching, and user/group resolution via NSS. Provides offline auth when the LDAP server is unreachable.
- PAM — Pluggable Authentication Modules chain that SSSD plugs into for password-based auth and access control.
- OpenSSH
AuthorizedKeysCommand— Lets sshd fetch SSH public keys directly from LDAP instead of~/.ssh/authorized_keys. - NSS — Name Service Switch maps LDAP users/groups into the system so
id,getent, and home directory creation work transparently.
Step 1 — Prerequisites
Before you beginBefore configuring LDAP authentication for SSH, verify the following:
dc=example,dc=com) and bind credentialsposixAccount objectClass (uidNumber, gidNumber, homeDirectory, loginShell)LDAP user entry example (LDIF format):
If you also want SSH public key lookup from LDAP (covered in Step 6), include the sshPublicKey attribute:
Step 2 — Install SSSD & Required Packages
Package installationInstall SSSD and its LDAP backend along with supporting utilities for PAM, NSS, and home directory creation.
Enable automatic home directory creation so LDAP users get a home directory on first login:
Step 3 — Configure SSSD for LDAP
Core configurationSSSD configuration lives in /etc/sssd/sssd.conf. This file must have 0600 permissions — SSSD refuses to start otherwise.
realm join corp.example.com (install realmd and adcli packages). SSSD's ad provider auto-discovers servers, configures Kerberos, and maps AD attributes to POSIX.
Set correct permissions and start SSSD:
Configure NSS to use SSSD: Edit /etc/nsswitch.conf to add sss as a source for user and group lookups.
Verify LDAP user resolution:
Step 4 — Configure PAM for SSH Authentication
Authentication chainPAM (Pluggable Authentication Modules) is the bridge between OpenSSH and SSSD. When UsePAM yes is set in sshd_config, OpenSSH delegates password authentication to the PAM stack, which in turn calls SSSD to verify credentials against LDAP.
On Ubuntu/Debian, PAM is typically auto-configured when installing libpam-sss. Verify the SSH PAM config includes SSSD:
The common-auth include should already reference pam_sss.so. Verify:
On RHEL/CentOS, use authselect to configure the PAM stack for SSSD:
The resulting /etc/pam.d/sshd should include:
Step 5 — Configure OpenSSH Server
sshd_configEdit /etc/ssh/sshd_config to enable PAM authentication and (optionally) LDAP public key lookup. This configuration enables both password auth via LDAP and public key auth with keys stored in your directory.
Validate and restart SSH:
AllowGroups in sshd_config combined with ldap_access_filter in sssd.conf for defense-in-depth. Even if SSSD allows a user, sshd will reject them if they're not in the permitted group.
Step 6 — SSH Public Key Lookup from LDAP
Centralized key managementInstead of managing ~/.ssh/authorized_keys files on every server, store SSH public keys in LDAP as attributes on user entries. SSSD's sss_ssh_authorizedkeys command fetches them automatically when sshd needs to verify a key.
Option A: Using SSSD (recommended)
SSSD natively supports SSH key retrieval via the [ssh] service. Ensure ssh is listed in the services line of sssd.conf and ldap_user_ssh_public_key is set to the correct LDAP attribute name.
Option B: Custom script (fallback)
If you cannot use SSSD's SSH service or need more control, use a custom script with ldapsearch:
Add SSH public key to an LDAP user:
Step 7 — Testing & Verification
Validate the setupRun through this checklist to verify every component is working correctly. Test from a separate machine to simulate real user access.
Step 8 — Hardening & Best Practices
Production-ready securityApply these hardening measures before deploying to production:
Step 9 — Troubleshooting
Common issues & fixesgetent passwd username returns nothingAuthorizedKeysCommand not working — public key auth failsMove beyond LDAP + SSH plumbing
OnePAM provides Zero Trust access to SSH, RDP, databases, and web apps. Identity-based authentication, session recording, RBAC policies, and compliance reports — without managing SSSD, PAM, or SSH keys.