OpenLDAP Directory Service
Overview
- What you will learn: The Lightweight Directory Access Protocol (LDAP) and its data model (entries, attributes, objectClasses, the Directory Information Tree), how to install and configure OpenLDAP (slapd) on Ubuntu, manage directory content with LDIF files and ldap utilities (ldapsearch, ldapadd, ldapmodify, ldapdelete), understand and extend schemas, configure Access Control Lists for fine-grained authorization, and enable TLS encryption.
- Prerequisites: Lesson 49 (Kerberos Authentication), familiarity with DNS and X.509 certificates
- Estimated reading time: 30 minutes
Introduction
A directory service is a specialized database optimized for reading, searching, and browsing hierarchical data. While relational databases excel at transactional workloads, directories are designed for storing identity information — user accounts, groups, organizational units, hosts, and certificates — that is read far more often than it is written.
LDAP (Lightweight Directory Access Protocol) is the standard protocol for accessing directory services. It defines how clients query and modify directory entries, how the data is structured in a tree hierarchy, and how entries are identified by Distinguished Names (DNs). OpenLDAP is the most widely deployed open-source LDAP server on Linux.
In this lesson you will learn the LDAP data model, install and configure the OpenLDAP server (slapd) on Ubuntu, populate the directory with user and group entries, apply schemas, and configure access control lists to secure directory data.
LDAP Data Model
The Directory Information Tree (DIT)
LDAP organizes data in a tree structure called the Directory Information Tree. Each node in the tree is an entry identified by a Distinguished Name (DN) that describes the path from the entry to the root of the tree.
# Example DIT structure
dc=example,dc=com # Root (domain component)
├── ou=People,dc=example,dc=com # Organizational Unit
│ ├── uid=alice,ou=People,dc=example,dc=com
│ └── uid=bob,ou=People,dc=example,dc=com
├── ou=Groups,dc=example,dc=com
│ ├── cn=developers,ou=Groups,dc=example,dc=com
│ └── cn=admins,ou=Groups,dc=example,dc=com
└── ou=Services,dc=example,dc=com
Entries, Attributes, and ObjectClasses
Each entry consists of a set of attributes. Attributes have a type (e.g., cn, uid, mail) and one or more values. The set of allowed and required attributes is determined by the entry’s objectClass values.
- Structural objectClass: Defines the core attribute set (every entry must have exactly one). Examples:
inetOrgPerson,organizationalUnit,posixAccount. - Auxiliary objectClass: Adds optional attributes to an entry. Example:
posixAccountaddsuidNumber,gidNumber,homeDirectory.
Installing and Configuring OpenLDAP
# Install slapd and LDAP utilities
$ sudo apt install slapd ldap-utils
# During installation you will be prompted to set the admin password
# The default base DN is derived from the system domain name
# Reconfigure if needed
$ sudo dpkg-reconfigure slapd
# - Omit OpenLDAP config? No
# - DNS domain name: example.com (creates dc=example,dc=com)
# - Organization name: Example Inc
# - Admin password: (strong password)
# - Database backend: MDB
# - Remove database when slapd is purged? No
# - Move old database? Yes
Verify the Installation
# Check slapd is running
$ sudo systemctl status slapd
# Search the base DN
$ ldapsearch -x -LLL -H ldap:/// -b "dc=example,dc=com" "(objectClass=organization)"
dn: dc=example,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
o: Example Inc
dc: example
# The -x flag means simple authentication (as opposed to SASL)
# -LLL suppresses comments and version information
Managing Directory Content with LDIF
LDIF (LDAP Data Interchange Format) is the standard text format for representing LDAP entries and modifications. All directory changes are expressed as LDIF files.
Adding Organizational Units
# base-ous.ldif
dn: ou=People,dc=example,dc=com
objectClass: organizationalUnit
ou: People
dn: ou=Groups,dc=example,dc=com
objectClass: organizationalUnit
ou: Groups
# Add to the directory
$ ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f base-ous.ldif
Enter LDAP Password:
adding new entry "ou=People,dc=example,dc=com"
adding new entry "ou=Groups,dc=example,dc=com"
Adding User Entries
# user-alice.ldif
dn: uid=alice,ou=People,dc=example,dc=com
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: alice
cn: Alice Chen
sn: Chen
givenName: Alice
mail: alice@example.com
uidNumber: 10001
gidNumber: 10001
homeDirectory: /home/alice
loginShell: /bin/bash
userPassword: {SSHA}hashed_password_here
# Generate a password hash
$ slappasswd -s "securepassword"
{SSHA}W3ipFbMLsxKtF...
# Add the user
$ ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f user-alice.ldif
Modifying and Deleting Entries
# modify-alice.ldif — change email and add a phone number
dn: uid=alice,ou=People,dc=example,dc=com
changetype: modify
replace: mail
mail: alice.chen@example.com
-
add: telephoneNumber
telephoneNumber: +886-2-1234-5678
# Apply the modification
$ ldapmodify -x -D "cn=admin,dc=example,dc=com" -W -f modify-alice.ldif
# Delete an entry
$ ldapdelete -x -D "cn=admin,dc=example,dc=com" -W
"uid=bob,ou=People,dc=example,dc=com"
Searching the Directory
# Search for all users
$ ldapsearch -x -LLL -b "ou=People,dc=example,dc=com" "(objectClass=posixAccount)"
uid cn mail uidNumber
# Search by attribute
$ ldapsearch -x -LLL -b "dc=example,dc=com" "(uid=alice)"
# Search with a filter
$ ldapsearch -x -LLL -b "ou=People,dc=example,dc=com"
"(&(objectClass=posixAccount)(uidNumber>=10000))" uid cn
# Count results
$ ldapsearch -x -b "dc=example,dc=com" "(objectClass=posixAccount)" dn |
grep "^dn:" | wc -l
Schemas
Schemas define the attribute types and objectClasses available in the directory. OpenLDAP loads schemas from /etc/ldap/schema/ using the cn=config backend.
# List loaded schemas
$ ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:///
-b "cn=schema,cn=config" "(objectClass=olcSchemaConfig)" dn
dn: cn={0}core,cn=schema,cn=config
dn: cn={1}cosine,cn=schema,cn=config
dn: cn={2}nis,cn=schema,cn=config
dn: cn={3}inetorgperson,cn=schema,cn=config
# Load an additional schema (e.g., RADIUS)
$ sudo ldapadd -Q -Y EXTERNAL -H ldapi:///
-f /etc/ldap/schema/misc.ldif
Access Control Lists (ACLs)
OpenLDAP ACLs control who can read, write, and search directory entries. ACLs are configured in the cn=config database using the olcAccess attribute.
# View current ACLs
$ sudo ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:///
-b "olcDatabase={1}mdb,cn=config" olcAccess
# Example ACL configuration (acl-update.ldif)
dn: olcDatabase={1}mdb,cn=config
changetype: modify
replace: olcAccess
olcAccess: {0}to attrs=userPassword
by self write
by anonymous auth
by * none
olcAccess: {1}to attrs=shadowLastChange
by self write
by * read
olcAccess: {2}to dn.subtree="ou=People,dc=example,dc=com"
by self read
by users read
by * none
olcAccess: {3}to *
by dn="cn=admin,dc=example,dc=com" write
by users read
by * none
# Apply ACL changes
$ sudo ldapmodify -Q -Y EXTERNAL -H ldapi:/// -f acl-update.ldif
Enabling TLS Encryption
# Configure TLS (tls-config.ldif)
dn: cn=config
changetype: modify
replace: olcTLSCACertificateFile
olcTLSCACertificateFile: /etc/ssl/certs/ca-certificates.crt
-
replace: olcTLSCertificateFile
olcTLSCertificateFile: /etc/ldap/ssl/ldap-server.crt
-
replace: olcTLSCertificateKeyFile
olcTLSCertificateKeyFile: /etc/ldap/ssl/ldap-server.key
# Apply TLS configuration
$ sudo ldapmodify -Q -Y EXTERNAL -H ldapi:/// -f tls-config.ldif
# Restart slapd
$ sudo systemctl restart slapd
# Test with STARTTLS
$ ldapsearch -x -ZZ -H ldap://ldap.example.com -b "dc=example,dc=com" "(objectClass=organization)"
Key Takeaways
- LDAP is a protocol for accessing hierarchical directory data; entries are identified by Distinguished Names (DNs) within a Directory Information Tree (DIT).
- Each entry’s allowed attributes are determined by its objectClasses, which are defined in schemas loaded into the server.
- OpenLDAP (slapd) on Ubuntu is configured via the cn=config backend; directory content is managed with LDIF files and the ldapadd, ldapmodify, ldapdelete, and ldapsearch commands.
- Access Control Lists define who can read, write, and authenticate against directory entries — always protect the
userPasswordattribute. - Enable TLS encryption for all LDAP connections to prevent credentials and directory data from being transmitted in cleartext.
What’s Next
In the next lesson you will learn about SSSD and Active Directory Integration — how to join Ubuntu servers to an Active Directory domain and provide centralized authentication using SSSD.
繁體中文
概述
- 學習目標:輕量級目錄存取協定(LDAP)及其資料模型(項目、屬性、objectClass、目錄資訊樹)、如何在 Ubuntu 上安裝和設定 OpenLDAP(slapd)、使用 LDIF 檔案和 ldap 工具管理目錄內容、了解和擴充綱要、設定存取控制清單以實現細粒度授權,以及啟用 TLS 加密。
- 先決條件:第 49 課(Kerberos 認證),熟悉 DNS 和 X.509 憑證
- 預計閱讀時間:30 分鐘
簡介
目錄服務是一種專門針對讀取、搜尋和瀏覽階層式資料進行最佳化的資料庫。LDAP(輕量級目錄存取協定)是存取目錄服務的標準協定。它定義客戶端如何查詢和修改目錄項目、資料如何在樹狀階層中結構化,以及項目如何通過辨別名稱(DN)識別。
OpenLDAP 是 Linux 上部署最廣泛的開源 LDAP 伺服器。本課程將學習 LDAP 資料模型、在 Ubuntu 上安裝和設定 slapd、使用 LDIF 填入目錄、套用綱要,以及設定存取控制清單以保護目錄資料。
LDAP 資料模型
目錄資訊樹(DIT)
LDAP 將資料組織在稱為目錄資訊樹的樹狀結構中。樹中的每個節點是一個項目,由辨別名稱(DN)識別,描述從項目到樹根的路徑。
# DIT 結構範例
dc=example,dc=com # 根(域元件)
├── ou=People,dc=example,dc=com # 組織單位
│ ├── uid=alice,ou=People,dc=example,dc=com
│ └── uid=bob,ou=People,dc=example,dc=com
└── ou=Groups,dc=example,dc=com
項目、屬性和 ObjectClass
每個項目由一組屬性組成。屬性有類型和一個或多個值。允許和必需的屬性集由項目的 objectClass 值決定。
安裝和設定 OpenLDAP
# 安裝 slapd 和 LDAP 工具
$ sudo apt install slapd ldap-utils
# 重新設定
$ sudo dpkg-reconfigure slapd
# 驗證安裝
$ ldapsearch -x -LLL -H ldap:/// -b "dc=example,dc=com" "(objectClass=organization)"
使用 LDIF 管理目錄內容
# 添加組織單位(base-ous.ldif)
dn: ou=People,dc=example,dc=com
objectClass: organizationalUnit
ou: People
# 添加到目錄
$ ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f base-ous.ldif
# 添加使用者項目
$ ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f user-alice.ldif
# 修改項目
$ ldapmodify -x -D "cn=admin,dc=example,dc=com" -W -f modify-alice.ldif
# 刪除項目
$ ldapdelete -x -D "cn=admin,dc=example,dc=com" -W "uid=bob,ou=People,dc=example,dc=com"
搜尋目錄
# 搜尋所有使用者
$ ldapsearch -x -LLL -b "ou=People,dc=example,dc=com" "(objectClass=posixAccount)" uid cn mail
# 按屬性搜尋
$ ldapsearch -x -LLL -b "dc=example,dc=com" "(uid=alice)"
綱要和存取控制
綱要定義目錄中可用的屬性類型和 objectClass。ACL 控制誰可以讀取、寫入和搜尋目錄項目。
# 列出已載入的綱要
$ ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:///
-b "cn=schema,cn=config" "(objectClass=olcSchemaConfig)" dn
# 查看當前 ACL
$ sudo ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:///
-b "olcDatabase={1}mdb,cn=config" olcAccess
重點摘要
- LDAP 是存取階層式目錄資料的協定;項目由目錄資訊樹(DIT)中的辨別名稱(DN)識別。
- 每個項目允許的屬性由其 objectClass 決定,objectClass 在載入到伺服器的綱要中定義。
- Ubuntu 上的 OpenLDAP(slapd)透過 cn=config 後端設定;使用 LDIF 檔案和 ldapadd、ldapmodify、ldapdelete、ldapsearch 命令管理目錄內容。
- 存取控制清單定義誰可以讀取、寫入和認證目錄項目——始終保護
userPassword屬性。 - 為所有 LDAP 連線啟用 TLS 加密,防止憑證和目錄資料以明文傳輸。
下一步
在下一課中,您將學習 SSSD 和 Active Directory 整合——如何將 Ubuntu 伺服器加入 Active Directory 網域,並使用 SSSD 提供集中式認證。
日本語
概要
- 学習内容:LDAP(Lightweight Directory Access Protocol)とそのデータモデル(エントリ、属性、objectClass、ディレクトリ情報ツリー)、Ubuntu での OpenLDAP(slapd)のインストールと設定、LDIF ファイルと ldap ユーティリティによるディレクトリコンテンツの管理、スキーマの理解と拡張、きめ細かな認可のためのアクセス制御リストの設定、TLS 暗号化の有効化。
- 前提条件:レッスン49(Kerberos 認証)、DNS と X.509 証明書の知識
- 推定読了時間:30分
はじめに
ディレクトリサービスは、階層的なデータの読み取り、検索、ブラウズに最適化された特殊なデータベースです。LDAP はディレクトリサービスにアクセスするための標準プロトコルです。クライアントがディレクトリエントリをどのようにクエリして変更するか、データがどのようにツリー階層で構造化されるか、エントリがどのように識別名(DN)で識別されるかを定義します。
OpenLDAP は Linux で最も広くデプロイされているオープンソースの LDAP サーバーです。このレッスンでは、LDAP データモデルを学び、Ubuntu で slapd をインストール・設定し、LDIF でディレクトリにデータを投入し、スキーマを適用し、ACL を設定してディレクトリデータを保護します。
LDAP データモデル
ディレクトリ情報ツリー(DIT)
LDAP はデータをディレクトリ情報ツリーと呼ばれるツリー構造で整理します。ツリー内の各ノードはエントリであり、エントリからツリーのルートまでのパスを示す識別名(DN)で識別されます。
# DIT 構造の例
dc=example,dc=com # ルート
├── ou=People,dc=example,dc=com # 組織単位
│ ├── uid=alice,ou=People,dc=example,dc=com
│ └── uid=bob,ou=People,dc=example,dc=com
└── ou=Groups,dc=example,dc=com
エントリ、属性、ObjectClass
各エントリは一連の属性で構成されます。許可される属性と必須属性のセットはエントリの objectClass 値によって決まります。
OpenLDAP のインストールと設定
# slapd と LDAP ユーティリティのインストール
$ sudo apt install slapd ldap-utils
# 再設定
$ sudo dpkg-reconfigure slapd
# インストールの検証
$ ldapsearch -x -LLL -H ldap:/// -b "dc=example,dc=com" "(objectClass=organization)"
LDIF によるディレクトリコンテンツの管理
# 組織単位の追加(base-ous.ldif)
dn: ou=People,dc=example,dc=com
objectClass: organizationalUnit
ou: People
# ディレクトリに追加
$ ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f base-ous.ldif
# ユーザーエントリの追加
$ ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f user-alice.ldif
# エントリの変更
$ ldapmodify -x -D "cn=admin,dc=example,dc=com" -W -f modify-alice.ldif
# エントリの削除
$ ldapdelete -x -D "cn=admin,dc=example,dc=com" -W "uid=bob,ou=People,dc=example,dc=com"
ディレクトリの検索
# すべてのユーザーを検索
$ ldapsearch -x -LLL -b "ou=People,dc=example,dc=com" "(objectClass=posixAccount)" uid cn mail
# 属性で検索
$ ldapsearch -x -LLL -b "dc=example,dc=com" "(uid=alice)"
スキーマとアクセス制御
スキーマはディレクトリで利用可能な属性タイプと objectClass を定義します。ACL は誰がディレクトリエントリを読み取り、書き込み、検索できるかを制御します。
# ロードされたスキーマの一覧
$ ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:///
-b "cn=schema,cn=config" "(objectClass=olcSchemaConfig)" dn
# 現在の ACL の表示
$ sudo ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:///
-b "olcDatabase={1}mdb,cn=config" olcAccess
重要ポイント
- LDAP は階層的なディレクトリデータにアクセスするためのプロトコル。エントリはディレクトリ情報ツリー(DIT)内の識別名(DN)で識別される。
- 各エントリの許可属性は objectClass によって決まり、objectClass はサーバーにロードされたスキーマで定義される。
- Ubuntu の OpenLDAP(slapd)は cn=config バックエンドで設定。LDIF ファイルと ldapadd、ldapmodify、ldapdelete、ldapsearch コマンドでディレクトリコンテンツを管理。
- アクセス制御リストはディレクトリエントリの読み取り、書き込み、認証の権限を定義する。
userPassword属性は常に保護すること。 - すべての LDAP 接続で TLS 暗号化を有効にし、資格情報とディレクトリデータがクリアテキストで送信されるのを防ぐ。
次のステップ
次のレッスンでは、SSSD と Active Directory 統合について学びます。Ubuntu サーバーを Active Directory ドメインに参加させ、SSSD を使用して集中認証を提供する方法を学びます。