使用 OpenLDAP 的集中式身份验证配置手册
作者:Full-Stack Innovator Pro(技术审查)目标:配置 OpenLDAP 服务器以允许 Ubuntu 客户端实现单点登录(SSO)...
作者:Full-Stack Innovator Pro(技术审查) 目标:配置 OpenLDAP 服务器以在 Ubuntu Desktop 客户端上启用 Single Sign-On (SSO),包括自动创建个人主目录和 SSL/TLS 安全性。
- 领域:
portelcore.local - 组织:
portelcore - 服务器(静态IP):
172.20.0.153 - 客户端:Ubuntu 桌面
- 基础DN:
dc=portelcore,dc=local
第一部分:服务器配置(Ubuntu Server)
Sección titulada «PARTE 1: Configuración del Servidor (Ubuntu Server)»首先,我们将安装 LDAP 守护进程 (slapd) 和管理工具。
sudo apt updatesudo apt install slapd ldap-utils -y现在,我们将重新配置该包以使用我们的特定域名创建数据库。
sudo dpkg-reconfigure slapd配置助手的回答:
- 跳过配置?
No - DNS域名名称?
portelcore.local - 组织名称?
portelcore - 管理员密码? (我们定义了一个安全密码并记住它)
- 数据库引擎?
MDB - 清除数据库?
No - 移动旧数据库?
Sí
完成之后,我们将通过验证域名上下文来检查数据库是否正确创建:
slapcat | grep "dc=portelcore"2. 创建映射文件(LDIF)
Sección titulada «2. Creación del archivo de mapeo (LDIF)»此步骤对于定义目录架构至关重要。我们将创建一个 .ldif 文件,该文件定义结构(组织单位)、组和用户。
我们创建了文件:
nano usuaris_grups.ldif将以下内容粘贴到文件中:
# Estructura Base: Unidades Organizativasdn: ou=people,dc=portelcore,dc=localobjectClass: organizationalUnitou: people
dn: ou=groups,dc=portelcore,dc=localobjectClass: organizationalUnitou: groups
# ---------------------------------------------------------# DEFINICIÓN DE GRUPOS# ---------------------------------------------------------
# Grupo 1: Administrador (GID 5000)dn: cn=Administrador,ou=groups,dc=portelcore,dc=localobjectClass: posixGroupcn: AdministradorgidNumber: 5000
# Grupo 2: Empleados (GID 5001)dn: cn=Empleats,ou=groups,dc=portelcore,dc=localobjectClass: posixGroupcn: EmpleatsgidNumber: 5001
# Grupo 3: Helpdesk (GID 5002)dn: cn=Helpdesk,ou=groups,dc=portelcore,dc=localobjectClass: posixGroupcn: HelpdeskgidNumber: 5002
# ---------------------------------------------------------# DEFINICIÓN DE USUARIOS# ---------------------------------------------------------
# Usuario 1: admin (Dentro de Administrador)dn: uid=admin,ou=people,dc=portelcore,dc=localobjectClass: inetOrgPersonobjectClass: posixAccountobjectClass: shadowAccountuid: adminsn: SystemgivenName: Administratorcn: Admin SystemdisplayName: Admin SystemuidNumber: 10000gidNumber: 5000userPassword: nolose123gecos: Admin SystemloginShell: /bin/bashhomeDirectory: /home/admin
# Usuario 2: user.portelcore (Dentro de Empleados)dn: uid=user.portelcore,ou=people,dc=portelcore,dc=localobjectClass: inetOrgPersonobjectClass: posixAccountobjectClass: shadowAccountuid: user.portelcoresn: UsergivenName: Portelcorecn: User PortelcoredisplayName: User PortelcoreuidNumber: 10001gidNumber: 5001userPassword: nolose123gecos: User PortelcoreloginShell: /bin/bashhomeDirectory: /home/user.portelcore
# Usuario 3: becario (Dentro de Helpdesk)dn: uid=becario,ou=people,dc=portelcore,dc=localobjectClass: inetOrgPersonobjectClass: posixAccountobjectClass: shadowAccountuid: becariosn: TechgivenName: Becariocn: Becario TechdisplayName: Becario TechuidNumber: 10002gidNumber: 5002userPassword: nolose123gecos: Becario TechloginShell: /bin/bashhomeDirectory: /home/becario我们将把映射文件注入到LDAP服务器中。
ldapadd -x -D "cn=admin,dc=portelcore,dc=local" -W -f usuaris_grups.ldif它会要求输入步骤1中配置的管理员密码。
验证:
如果一切正确,我们将看到 adding new entry 消息。我们验证用户“becario”是否存在:
aldapsearch -x -b "dc=portelcore,dc=local" "(uid=becario)"第二部分:Ubuntu桌面客户端配置
Sección titulada «PARTE 2: Configuración del Cliente (Ubuntu Desktop)»我们进入客户端机器。该设备没有本地创建的用户;需要从服务器读取用户。
我们将安装连接身份验证(PAM)和名称服务(NSS)与LDAP所需的软件包。
sudo apt updatesudo apt install libnss-ldapd libpam-ldapd nscd -y图形化配置助手:
- LDAP服务器URI:
ldap://172.20.0.153 - 搜索基础(Search Base):
dc=portelcore,dc=local - 服务: 确保使用空格键选中
passwd、group和shadow。
⚠️ 重要:验证 nsswitch.conf 如果安装后用户无法使用,我们手动验证系统是否在 LDAP 中查找:
cat /etc/nsswitch.conf | grep passwd如果在行尾没有出现ldap,请编辑文件sudo nano /etc/nsswitch.conf,并在行passwd、group和shadow的末尾添加ldap。
客户端必须能够“看到”服务器上的用户。
getent passwd user.portelcore预期结果: user.portelcore:x:10001:5001:User Portelcore:/home/user.portelcore:/bin/bash
我们将配置PAM,以便在第一次登录时自动创建文件夹/home。
编辑文件common-session:
sudo nano /etc/pam.d/common-session在文件的末尾添加此行:
session required pam_mkhomedir.so skel=/etc/skel umask=0077重新启动服务nscd以清除缓存:
sudo systemctl restart nscd使用用户admin(密码:nolose123)登录桌面终端。
id结果: 必须显示uid=10000(admin)和gid=5000(Administrador)。
我们注销并使用 becario / nolose123 登录。
第4部分:服务器和客户端安全 (SSL/TLS)
Sección titulada «PARTE 4: Seguridad del Servidor y Cliente (SSL/TLS)»为了遵守安全法规,我们将加密连接。
我们将安装OpenSSL并创建证书。 重要: 当要求“Common Name (CN)”时,我们将输入 portelcore.local。
sudo apt install openssl -ysudo mkdir /etc/ldap/certs
# 1. Crear clave privada CAsudo openssl genrsa -out /etc/ldap/certs/ca.key 2048
# 2. Crear certificado CA (Autoridad)sudo openssl req -x509 -new -nodes -key /etc/ldap/certs/ca.key -sha256 -days 3650 -out /etc/ldap/certs/ca.crt -subj "/C=ES/ST=Barcelona/L=Granollers/O=Portelcore/CN=portelcore.local"
# 3. Crear clave privada del Servidorsudo openssl genrsa -out /etc/ldap/certs/ldap_server.key 2048
# 4. Crear petición de firma (CSR)sudo openssl req -new -key /etc/ldap/certs/ldap_server.key -out /etc/ldap/certs/ldap_server.csr -subj "/C=ES/ST=Barcelona/L=Granollers/O=Portelcore/CN=portelcore.local"
# 5. Firmar el certificado del servidor con nuestra CAsudo openssl x509 -req -in /etc/ldap/certs/ldap_server.csr -CA /etc/ldap/certs/ca.crt -CAkey /etc/ldap/certs/ca.key -CAcreateserial -out /etc/ldap/certs/ldap_server.crt -days 365 -sha256我们将分配权限,以便LDAP可以读取密钥:
sudo chown -R openldap:openldap /etc/ldap/certs/sudo chmod 600 /etc/ldap/certs/*.key4.2 配置OpenLDAP以使用TLS(服务器)
Sección titulada «4.2 Configuración de OpenLDAP para usar TLS (Servidor)»我们将创建一个文件 tls_config.ldif 以指示证书的位置:
nano tls_config.ldif内容:
dn: cn=configchangetype: modifyreplace: olcTLSCACertificateFileolcTLSCACertificateFile: /etc/ldap/certs/ca.crt-replace: olcTLSCertificateFileolcTLSCertificateFile: /etc/ldap/certs/ldap_server.crt-replace: olcTLSCertificateKeyFileolcTLSCertificateKeyFile: /etc/ldap/certs/ldap_server.key应用配置:
sudo ldapmodify -Y EXTERNAL -H ldapi:/// -f tls_config.ldif我们将编辑 /etc/default/slapd 以确保它接受安全连接:
sudo nano /etc/default/slapd我们将查找 SLAPD_SERVICES 并将其保留如下:
SLAPD_SERVICES="ldap:/// ldapi:/// ldaps:///"
我们将重启:
sudo systemctl restart slapd4.3 客户端SSL配置(Ubuntu桌面)
Sección titulada «4.3 Configuración del Cliente para SSL (Ubuntu Desktop)»⚠️ 关键预备步骤:DNS 配置
由于证书是颁发给 portelcore.local 的,因此我们不能在 SSL 配置中直接使用 IP 地址。
-
我们将编辑客户端的hosts文件:
Ventana de terminal sudo nano /etc/hosts -
我们将在最后添加此行:
172.20.0.153 portelcore.local
SSL配置步骤:
-
传输CA:我们需要从服务器下载证书到客户端。
我们将在客户端上打开终端并执行: (我们将
usuari_remot替换为服务器上的用户,例如,alex)Ventana de terminal # 1. Descargar archivoscp user.portelcore@172.20.0.153:/etc/ldap/certs/ca.crt .# 2. Mover al lugar correctosudo mv ca.crt /etc/ssl/certs/# 3. Asignar permisos de lectura para todossudo chmod 644 /etc/ssl/certs/ca.crt -
配置 LDAP 守护进程: 我们将编辑配置文件:
Ventana de terminal sudo nano /etc/nslcd.conf我们将进行两次重要更改:
更改 A:我们将修改 URI 以使用域名而不是 IP:
uri ldap://portelcore.local变更 B: 我们将启用 TLS 和证书:
ssl start_tlstls_reqcert demandtls_cacertfile /etc/ssl/certs/ca.crt -
重启客户机服务:
Ventana de terminal sudo systemctl restart nslcd -
验证连接:
getent passwd admin
第5部分:专业审计(tcpdump + Wireshark)
Sección titulada «PARTE 5: Auditoría Profesional (tcpdump + Wireshark)»在生产环境(无GUI的服务器)中,最佳实践是在服务器上捕获流量,然后在桌面设备上分析。
5.1 流量捕获(在服务器上)
Sección titulada «5.1 Captura de Tráfico (En el Servidor)»-
我们将安装捕获工具:
Ventana de terminal sudo apt install tcpdump -y -
我们将启动捕获,过滤LDAP端口(389)并保存到文件
.pcap:Ventana de terminal sudo tcpdump -i any port 389 -w /tmp/ldap_capture.pcap(在进行下一步骤时,我们将保持终端打开。)
-
我们在客户端(Ubuntu Desktop)打开一个终端。
-
我们将进行查询以生成流量:
getent passwd user.portelcore -
回到服务器并使用
Ctrl + C停止捕获。
现在我们将捕获文件传输到客户端,使用Wireshark(具有图形界面)进行分析。
-
在客户端,我们将下载文件:
scp usuari@172.20.0.153:/tmp/ldap_capture.pcap . -
我们将安装并打开Wireshark:
Ventana de terminal sudo apt install wireshark -ywireshark ldap_capture.pcap
我们应该寻找什么?>
- 我们将搜索使用协议
LDAP的数据包。 - 我们将搜索包 “startTLSRequest”。
- 随后,应该会出现 TLSv1.2 或 TLSv1.3 数据包。
- 成功测试: 我们将尝试读取“Application Data”数据包的内容。如果只看到不可读的字符(加密的),则安全功能正常工作。
附录 1:服务器IP更改时的解决方案
Sección titulada «ANEXO 1: Solución si cambia la IP del Servidor»如果客户端已配置,且服务器IP更改(例如,从 192.168.1.100 到 172.20.0.153),则无需重新安装。
选项 A(推荐 - 向导):
我们将执行 nslcd 包的重新配置:
sudo dpkg-reconfigure nslcd当要求输入URI时(ldap://172.20.0.153),我们将输入新IP。
选项 B(手动):
-
我们将编辑nslcd守护进程的配置文件:
Ventana de terminal sudo nano /etc/nslcd.conf -
我们将修改 URI 行: 我们将查找以
uri开头的行,并将旧 IP 更改为新 IP。 之前:uri ldap://192.168.1.100现在:uri ldap://172.20.0.153(我们将使用 Ctrl+O 保存并使用 Ctrl+X 退出)
-
我们将重启服务以应用更改:
Ventana de terminal sudo systemctl restart nslcdsudo systemctl restart nscd -
我们将验证连接:
getent passwd user.portelcore
附录 2:问题解决(Troubleshooting)
Sección titulada «ANEXO 2: Resolución de Problemas (Troubleshooting)»如果 getent passwd 没有返回任何内容,但有网络连接,问题通常是 SSL。
步骤 1:实时诊断 我们将停止守护进程并以调试模式手动运行它,以查看为什么拒绝连接。
sudo systemctl stop nslcdsudo nslcd -d阅读最后几行。如果看到“hostname does not match CN”,则表示我们在配置文件中使用了 IP 而不是名称。
步骤 2:URI 验证
我们将确保在 /etc/nslcd.conf 中恰好有以下内容:
uri ldap://portelcore.local
(如果拥有 ldap://172.20.0.153,证书将失败)。

