[Ubuntu] 在 Ubuntu 上安裝 OpenLDAP 的詳細教學
===
###### tags: `LDAP`
###### tags: `LDAP`, `OpenLDAP`, `Ubuntu`
<br>
[TOC]
<br>
## [選用] 建立測試環境
### 在主機(host)上
> 請直接跳過本節
---
### 在 docker 上
```bash
docker run -it --rm --name=slapd ubuntu:24.04 bash
# 或:預留特定 port 供連線測試
docker run -it --rm -p 389:389 -p 636:636 --name=slapd ubuntu:24.04 bash
apt update
# 安裝 OpenLDAP Server
apt install slapd -y
# 安裝 ldap commands:
# - ldapadd
# - ldapcompare
# - ldapdelete
# - ldapexop
# - ldapmodify
# - ldapmodrdn
# - ldappasswd
# - ldapsearch
# - ldapurl
# - ldapwhoami
apt install ldap-utils -y
```
---
### 在 Kubernetes (K8s) 上
- ### 有 root 的 pod
```bash
kubectl run ububtu-2404 -it --rm --image=ubuntu:24.04 -- bash
apt update
# 安裝 OpenLDAP Server
apt install slapd -y
# 安裝 ldap commands:
# - ldapadd
# - ldapcompare
# - ldapdelete
# - ldapexop
# - ldapmodify
# - ldapmodrdn
# - ldappasswd
# - ldapsearch
# - ldapurl
# - ldapwhoami
apt install ldap-utils -y
```
- ### 沒有 root 的 pod
> 只提供 ldap commands
```
kubectl run ldap-test --rm -it --image=bitnami/openldap:latest -- bash
```
<br>
---
---
<br>
> by gpt-4o (2025/09/21)
## 1️⃣ 安裝必要套件
```bash
sudo apt update
sudo apt install slapd ldap-utils -y
```
- **slapd**
- Standalone LDAP Daemon
(獨立式 LDAP 常駐服務程式)
- OpenLDAP 的伺服器行程,負責對外提供 `ldap://`, `ldaps://`, `ldapi:///` 端點的查詢與寫入服務。
- `ldaps://`: LDAP over SSL/TLS
- `ldapi:///`: LDAP over IPC
- **過程需設定 password**
> 後面重新設定 slapd 時,會再重新設定
```
$ sudo apt install slapd ldap-utils -y
...
Please enter the password for the admin entry in
your LDAP directory.
Administrator password: ******
Please enter the admin password for your LDAP
directory again to verify that you have typed
it correctly.
Confirm password: *******
...
```
---
## 2️⃣ 重新設定 slapd
執行:
```bash
sudo dpkg-reconfigure slapd
```
### 設定過程建議回答:
* **Omit OpenLDAP server configuration?** → `No` (選 `Yes` 就直接結束)
* **DNS domain name:** `www.google.com`
* **Organization name:** `Google`
* **Administrator password:** `YourSecurePassword` (自訂一組密碼)
* ~~**Database backend:** `MDB`~~
* **Remove database when slapd is purged?** → `No`
* **Move old database?** → `Yes`
設定完成後,會自動建立根節點:
```
dc=www,dc=google,dc=com
```
---
## 3️⃣ 驗證 slapd 服務
```bash
sudo systemctl status slapd
```
- 應該會看到 **active (running)**。
- **錯誤訊息:`bash: systemctl: command not found` (容器/Pod 內沒有 systemd)**:
- 不能用 `systemctl` 很正常。可直接前景啟動看 log:
```bash
sudo slapd -h "ldap:/// ldapi:///" \
-u openldap -g openldap -d 256
# slapd options:
# -h URLs List of URLs to serve
# -u user User (id or name) to run as
# -g group Group (id or name) to run as
# -d level Debug level
```
下面逐一解釋每個參數:
- ### `sudo`
先用 root 啟動,才能綁定特權埠(389),之後 slapd 會再降權到指定的使用者/群組。
- ### `slapd`
OpenLDAP 的伺服器行程本體。
- ### `-h "ldap:/// ldapi:///"`
> (多個 listener 用空白分隔,整段要加引號讓它被視為同一個參數)
* `ldap:///`:在所有介面(IPv4/IPv6)上用 TCP 監聽 **389**。
想改埠號可寫成 `ldap://0.0.0.0:1389` 或 `ldap://[::]:1389`。
* `ldapi:///`:開啟 **本機 Unix Domain Socket**(通常在 **/run/slapd/ldapi**)。
這個端點給本機工具用(例如用 `-Y EXTERNAL` 做免密鑰的管理操作)。
* 補充:要純 TLS 連線才開 `ldaps:///`(636),但一般建議在 `ldap:///` 上設定 **StartTLS**。
- ### `-u openldap`、`-g openldap`
綁好埠之後,**降權**成使用者 `openldap`、群組 `openldap` 來執行,提升安全性。
- **查看當前 Ubuntu 有無:使用者 `openldap` & 群組 `openldap`**
```
$ getent passwd openldap
openldap:x:100:101:OpenLDAP Server Account,,,:/var/lib/ldap:/bin/false
$ getent group openldap
openldap:x:101:
```
- **若你的容器沒有這個系統帳號,先建立**:
`adduser --system --group --no-create-home openldap`
並確保資料夾擁有者正確:
`chown -R openldap:openldap /etc/ldap/slapd.d /var/lib/ldap /run/slapd`
- ### `-d 256`(debug level, 留在**前景**)
把 slapd 留在**前景**並輸出除錯訊息;`256` 是 **stats** 級別,會列出連線與操作摘要,夠用又不至於太吵。
常用等級(可相加):
* `64`:config(啟動時看設定解析)
* `128`:acl(存取控制判斷)
* `256`:stats(操作摘要,**常用預設**)
* `512`:stats2(更細的操作統計)
例:要同時看設定與統計 → `-d $((64+256))` 或某些版本支援 `-d "config,stats"`。
---
## 4️⃣ 測試查詢
### 用 `ldapsearch` 指令
用 `ldapsearch` 測試 root base:
```bash
# 本機測試
ldapsearch -x \
-b dc=www,dc=google,dc=com -LLL
# 其他等效用法1
ldapsearch -x -H ldap:/// \
-b dc=www,dc=google,dc=com -LLL
ldapsearch -x -H ldap:// \
-b dc=www,dc=google,dc=com -LLL
# 其他等效用法2 (指定 local ip)
ldapsearch -x -H ldap://127.0.0.1 \
-b dc=www,dc=google,dc=com -LLL
ldapsearch -x -H ldap://127.0.0.1:389 \
-b dc=www,dc=google,dc=com -LLL
# 其他等效用法3 (使用 LDAP over IPC) (使用 socket)
ldapsearch -x -H ldapi:/// \
-b dc=www,dc=google,dc=com -LLL
ldapsearch -x -H ldapi:// \
-b dc=www,dc=google,dc=com -LLL
# or
# 帳密測試
ldapsearch -x -H ldap:// \
-D "cn=admin,dc=www,dc=google,dc=com" -w <前面輸入的帳密> \
-b dc=www,dc=google,dc=com -LLL
```
- 如果有回應 LDAP 條目,代表安裝成功。
- 執行範例
```
dn: dc=www,dc=google,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
o: Google
dc: www
```
- 改使用 `ldapi:///` (LDAP over IPC)
```
ldapsearch -Y EXTERNAL -H ldapi:/// \
-b dc=www,dc=google,dc=com -LLL
```
---
## 5️⃣ 新增組織單位 (OU)
> 補充資料:[[HackMD] tj_tsai.ldif (個人資料)](/NsEEE4PySgKDUDeQAHMMBA)
- ### 建立 `base.ldif`:
```ldif=
dn: ou=users,dc=www,dc=google,dc=com
objectClass: organizationalUnit
ou: users
dn: ou=groups,dc=www,dc=google,dc=com
objectClass: organizationalUnit
ou: groups
```
- ### 匯入:
```bash
sudo ldapadd -x -D "cn=admin,dc=www,dc=google,dc=com" -W -f base.ldif
```
- 需要用管理者身分匯入 (`-D` & `-W`),否則會有底下錯誤:
```
ldap_add: Strong(er) authentication required (8)
additional info: modifications require authentication
```
- ### 驗證
```bash
ldapsearch -x -b "dc=www,dc=google,dc=com" "(ou=*)" -LLL
```
- **執行結果**
```ldif
dn: ou=users,dc=www,dc=asus,dc=com
objectClass: organizationalUnit
ou: users
dn: ou=groups,dc=www,dc=asus,dc=com
objectClass: organizationalUnit
ou: groups
```
- ### 錯誤排除
- ### `ldap_add: Server is unwilling to perform (53)`
```
ldap_add: Server is unwilling to perform (53)
additional info: no global superior knowledge
```
- 這個錯誤是因為「你綁定的是 google 這棵樹的管理員,但你要新增的條目卻在 asus 這棵樹下」。
- 目前伺服器只有 `dc=www,dc=google,dc=com` 這個命名空間(naming context),對 `dc=www,dc=asus,dc=com` 沒有「上層知識」(no global superior knowledge),所以回 53。
- 你剛剛做了:
- 查:`-b dc=www,dc=google,dc=com`
- 綁:`-D "cn=admin,dc=www,dc=google,dc=com"`
- 卻加:`"ou=users,dc=www,dc=asus,dc=com"` ⛔️
---
## 6️⃣ 新增一個使用者
> 補充資料:[[HackMD] tj_tsai.ldif (個人資料)](/NsEEE4PySgKDUDeQAHMMBA)
- ### 建立 `user.ldif`:
```ldif
dn: cn=tj_tsai,ou=users,dc=www,dc=google,dc=com
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
cn: tj_tsai
sn: Tsai
givenName: TJ
mail: tj_tsai@gmail.com
uid: tj_tsai
uidNumber: 10001
gidNumber: 10001
homeDirectory: /home/tj_tsai
userPassword: {SSHA}your_password_hash
```
👉 密碼可以先產生:
```bash
$ slappasswd
New password:
Re-enter new password:
{SSHA}lxSVIBsvLERceWlz1gFj55zdIiGQO6ax
```
會輸出像 `{SSHA}lxSVIBsvLE....`,複製貼到 `userPassword`。
- ### 匯入
```bash
sudo ldapadd -x -D "cn=admin,dc=www,dc=google,dc=com" -W -f user.ldif
```
---
## 7️⃣ 查詢確認
```bash
ldapsearch -x -H ldap:/// \
-b dc=www,dc=google,dc=com cn=tj_tsai \
-LLL
```
輸出中應該能看到剛建立的使用者。
---
> ✅ 到這裡,你的 **OpenLDAP 在 Ubuntu 上已經完整安裝 + 初始化完成**,並且有一個 `tj_tsai` 使用者可用。
<br>
{%hackmd vaaMgNRPS4KGJDSFG0ZE0w %}