CentOS/RHEL#Ubuntu/Debian配置ldap账户登录

在CentOS/RHEL#Ubuntu/Debian上配置 LDAP 账户登录需要完成以下步骤:

​​1. 安装必要的软件包

# CentOS/RHEL ​​

sudo yum install -y openldap-clients nss-pam-ldapd authconfig
 # Ubuntu/Debian
sudo apt-get install -y ldap-utils libnss-ldap libpam-ldap nscd
 
 ​​2. 配置 LDAP 客户端​​

使用 authconfig工具配置 LDAP 认证:

 
authconfig --enableldap --enableldapauth --ldapserver=ldap://your_ldap_server --ldapbasedn="dc=example,dc=com" --enablemkhomedir --update
 
--ldapserver:替换为你的 LDAP 服务器地址(如 ldap://ldap.example.com
--ldapbasedn:替换为你的 LDAP 基础 DN(如 dc=example,dc=com
--enablemkhomedir:自动创建用户家目录

​​3. 配置 /etc/nslcd.conf​​

手动编辑 /etc/nslcd.conf以细化 LDAP 配置:

vim /etc/nslcd.conf
 示例配置:
uri ldap://your_ldap_server
base dc=example,dc=com
binddn cn=admin,dc=example,dc=com
bindpw your_ldap_admin_password
ssl no
tls_cacertdir /etc/openldap/cacerts
如果 LDAP 服务器支持匿名查询,可省略 binddn和 bindpw
如需 TLS/SSL,调整 ssl和 tls_cacertdir参数

​​4. 启动并启用 nslcd服务​​

# CentOS/RHEL

sudo systemctl restart nslcd
sudo systemctl enable nslcd
 
# Ubuntu/Debian 
sudo systemctl restart nscd
sudo systemctl enable nscd

 

​​5. 配置 PAM 和 NSS​​

确保 /etc/nsswitch.conf包含以下内容:

passwd: files ldap
shadow: files ldap
group: files ldap
 
 /etc/pam.d/sshd
/etc/pam.d/system-auth
文件需添加auth sufficient pam_ldap.so

​​6. 测试 LDAP 用户登录​

使用 getent passwd检查 LDAP 用户是否可见: 

getent passwd

切换到 LDAP 用户:

su - username

如果启用了 --enablemkhomedir,家目录会自动创建在 /home/username

​​7. 调试问题​​

检查日志:

tail -f /var/log/secure journalctl -u nslcd

 

验证 LDAP 查询:

ldapsearch -x -H ldap://your_ldap_server -b "dc=example,dc=com"

 
posted @ 2025-09-05 13:29  aaahwh  阅读(48)  评论(0)    收藏  举报