惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

SecWiki News
SecWiki News
I
InfoQ
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
博客园 - Franky
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
博客园_首页
罗磊的独立博客
V
V2EX
李成银的技术随笔
大猫的无限游戏
大猫的无限游戏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
True Tiger Recordings
Vercel News
Vercel News
Cyberwarzone
Cyberwarzone
Cisco Talos Blog
Cisco Talos Blog
F
Fox-IT International blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
M
Microsoft Research Blog - Microsoft Research
Know Your Adversary
Know Your Adversary
爱范儿
爱范儿
The Register - Security
The Register - Security
G
Google Developers Blog
The Hacker News
The Hacker News
Malwarebytes
Malwarebytes
S
Securelist
博客园 - 三生石上(FineUI控件)
Jina AI
Jina AI
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
S
SegmentFault 最新的问题
博客园 - 叶小钗
F
Fortinet All Blogs
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
博客园 - 聂微东
T
Threatpost
博客园 - 【当耐特】
D
Docker
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
V
Visual Studio Blog
C
Cisco Blogs
IT之家
IT之家
S
Security Archives - TechRepublic
Latest news
Latest news
阮一峰的网络日志
阮一峰的网络日志

披萨盒的赛博日志

使用策略模式重构复杂业务分支 像 systemd 一样管理 MacOS 后台常驻任务 以ORM看封装的边界 Git Merge VS Git Rebase: 如何优雅地合并分支? 修改Linux内核模块以支持WG 非特权模式容器 ssh 登录问题 在 Linux 开发环境中使用网络代理 白嫖 Aseprite 像素绘图软件 MongoDB 增删改查 Python数据分析工具包-Numpy 解决 CLion 中文乱码问题 搭建 RLCraft 服务器 SpringBoot读取配置文件 Centos 配置 LNMP 环境 部署项目时遇到的坑 浅谈 xhr 请求跨域问题 JavaScript 学习笔记 Eclipse配置Web开发环境 Vue2 基本知识 Ribbon 简单使用 Nacos 简单使用 Spring Cloud Alibaba 环境搭建 什么是RSS?什么是Feed?它们有什么关系? Docker基本使用 TensorFlow启用GPU加速 如何进行内网穿透 Hello World! Git基本使用 Butterfly常用标签外挂
OpenLDAP折腾日记
2024-12-12 · via 披萨盒的赛博日志

折腾了几天OpenLDAP,终于是把它给部署上了。对于软件部署这件事,我的评价是不要完全相信网上任何一篇几年前的教程(包括这篇),可能一些操作是对的,但另一些操作已经过时了,这会导致一些莫名奇妙的错误。配置文件中配置项上一般会有注释,看懂了再改。

Linux:RockyLinux9

安装相关软件

这步没啥说的,照着安装就行了

1
2
3
dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
dnf upgrade -y
dnf install -y openldap openldap-servers openldap-clients nginx phpldapadmin

安装后直接启动,不用顾虑

1
systemctl start slapd

初始化

首先查看自己的配置文件命令,这很重要。执行ls /etc/openldap/slapd.d/cn\=config/

image-20241214095534004

然后生成密码,执行命令 slappasswd -s your_password,这会在下面生成一个加密后的密码,如{SSHA}+TszwnXVhnhH5HFGK5Nf4A0xZWGHrmRN,把这个记录下来。

创建以下三个文件

init.ldif

1
2
3
4
5
6
7
8
9
10
dn: olcDatabase={2}mdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=admin,dc=pushihao,dc=com
-
replace: olcSuffix
olcSuffix: dc=pushihao,dc=com
-
replace: olcRootPW
olcRootPW: {SSHA}+TszwnXVhnhH5HFGK5Nf4A0xZWGHrmRN

access.ldif

1
2
3
4
dn: olcDatabase={1}monitor,cn=config
changetype: modify
replace: olcAccess
olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read by dn.base="cn=admin,dc=pushihao,dc=com" read by * none

base.ldif

1
2
3
4
5
6
7
8
9
10
11
dn: dc=pushihao,dc=com
o: PSH Organization
dc: pushihao
objectClass: top
objectClass: dcObject
objectclass: organization

dn: cn=admin,dc=pushihao,dc=com
cn: admin
objectClass: organizationalRole
description: Directory Manager

文件解读(前两个文件):第一行的dn指定了要修改的条目的唯一标识符,根据刚刚查看的名称来。changetype指定了对条目的修改类型。replace指定了要修改的字段名称,其接下来的一行表示修改后的值。记得把olcRootPW字段替换为刚刚生成的密码。

文件解读(第三个文件):第一段新建一个域,第二段新建了一个管理员。

文件创建好之后执行文件进行修改

1
2
3
ldapadd -Q -Y EXTERNAL -H ldapi:/// -f init.ldif
ldapadd -Q -Y EXTERNAL -H ldapi:/// -f access.ldif
ldapadd -x -D 'cn=admin,dc=pushihao,dc=com' -w 'your_password' -f base.ldif

导入基本的Schema

1
2
3
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif

使用PHPLDAPAdmin管理OpenLDAP

修改/etc/phpldapadmin/config.php

1
2
3
$servers->setValue('login','attr','dn');
$servers->setValue('login','anon_bind',false);
$servers->setValue('login','allowed_dns',array('cn=admin,dc=pushihao,dc=com'));

修改解读:第一行表示使用完整的dn作为登录凭据,第二行表示不允许匿名访问,第三行表示仅允许指定的dn进行登录

修改/etc/nginx/nginx.conf,添加一个server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
listen 8080;

root /usr/share/phpldapadmin/htdocs;
index index.php;

location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_param SCRIPT_FILENAME /usr/share/phpldapadmin/htdocs$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\.ht {
deny all;
}
}

注意fastcgi_pass这个配置项:查看/etc/php-fpm.d/www.conf这个文件,把fastcgi_pass这个配置项和/etc/php-fpm.d/www.conf文件中的listen配置项保持一致(它监听9000端口你也监听,它若用套接字你也用套接字)。

配置完成后重启nginxnginx -s reload即可在浏览器输入ip:8080访问PHPLDAPAdmin的web界面,用户名为cn=admin,dc=pushihao,dc=com,密码为最开始设置的密码(输入未加密的)。

OpenLDAP SSH客户端部署

服务端起来之后就可以根据需求配置各种各样支持ldap的客户端了,ssh是一个常见的需求。这里使用轻量级的nslcd工具。

安装相关软件

没啥说的,装就完了

1
2
3
dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
dnf upgrade -y
dnf install -y openldap-clients nss-pam-ldapd

配置nslcd

修改配置文件/etc/nslcd.conf,参考以下内容,如果之前的配置跟我不同(如密码),则灵活修改

1
2
3
4
5
6
7
8
9
uid root
gid root

uri ldap://ldap-node-01/
base dc=pushihao,dc=com

binddn cn=admin,dc=pushihao,dc=com
bindpw root
ssl no

修改配置文件/etc/nsswitch.conf,参考以下内容,灵活修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
passwd:     files ldap sss systemd
shadow: files ldap
group: files ldap sss systemd
hosts: files dns myhostname
services: files ldap sss
netgroup: files ldap sss
automount: files ldap sss

aliases: files
ethers: files
gshadow: files
networks: files dns
protocols: files
publickey: files
rpc: files

配置pam

修改/etc/pam.d/password-auth

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authselect is run.
auth required pam_env.so
auth sufficient pam_unix.so try_first_pass nullok
auth sufficient pam_ldap.so use_first_pass # 这里
auth required pam_deny.so

account required pam_unix.so
account [default=bad success=ok user_unknown=ignore] pam_ldap.so # 这里

password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type=
password sufficient pam_unix.so try_first_pass use_authtok nullok sha512 shadow
password sufficient pam_ldap.so use_authtok # 这里
password required pam_deny.so

session optional pam_keyinit.so revoke
session required pam_limits.so
-session optional pam_systemd.so
session [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid
session required pam_unix.so
session optional pam_ldap.so # 这里

修改/etc/pam.d/system-auth

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authselect is run.
auth required pam_env.so
auth sufficient pam_unix.so try_first_pass nullok
auth sufficient pam_ldap.so use_first_pass # 这里
auth required pam_deny.so

account required pam_unix.so
account [default=bad success=ok user_unknown=ignore] pam_ldap.so # 这里

password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type=
password sufficient pam_unix.so try_first_pass use_authtok nullok sha512 shadow
password sufficient pam_ldap.so use_authtok # 这里
password required pam_deny.so

session optional pam_keyinit.so revoke
session required pam_limits.so
-session optional pam_systemd.so
session [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid
session required pam_unix.so
session optional pam_ldap.so # 这里

修改/etc/ssh/sshd_config

1
2
PasswordAuthentication yes
UsePAM yes

重启相关软件

全部配置文件修改完成之后重启相关软件即可

1
systemctl restart sshd nslcd

然后就可以使用在ldap系统中新建的用户通过ssh连接到服务器了。

搞定!


2025.08.02 更新

使用 ldap 管理管理员用户

今天突然发现了一个问题,经过以上一通操作后。将用户添加到 wheel 组后仍然没有拥有管理员权限。

去看了一眼 sudoers 文件,发现 wheel 组确实是在里面的,并且没有被注释掉。

image-20250802105625430

于是查看 super 用户的 uid、gid 等信息,发现了一件有点令人匪夷所思的事情,明明使用 id 命令查看 super 用户是在 wheel 组的,但是 getent 命令却查不到。

image-20250802105207266

多次试验后我还是找到了问题所在:nsswitch.conf 的机制。

可以翻到上面看到我们配置的该文件有一行:group: files ldap sss systemd,也就是说如果在本地文件 (/etc/group) 找到了一个用户组,那就不会再往后面查找了。而 wheel 用户组又是 RockyLinux 系统默认就存在的组,所以 OpenLDAP 远程的 wheel 组会被覆盖掉。

问题找到了,那么解决方案就很清晰了。

  • 解决方案1: 注释掉 /etc/group 中 wheel:x:10: 这一行。
  • 解决方案2: 修改 /etc/nsswitch.conf 中 group 的配置,将 ldap 提到 files 前面,即 group: ldap files sss systemd

这下才是真的搞定!