π οΈ 165. OpenLDAP Installation & Configuration Guide π
Table of Contents:
- π What is OpenLDAP?
- π οΈ How to Install and Configure OpenLDAP
- ποΈ OpenLDAP Service: Slapd
- π Start, Stop, and Enable OpenLDAP Service
- βοΈ Configuration Files
- π Important Configurations
- π Common Use Cases
- π Script for Common Usage and Configuration
- π Visual Representation
π What is OpenLDAP?
OpenLDAP is an open-source implementation of the Lightweight Directory Access Protocol (LDAP). It provides a robust directory service that can manage and organize information across networks, particularly for centralized user authentication, group management, and more.
π οΈ How to Install and Configure OpenLDAP
- Install OpenLDAP: Install the OpenLDAP server and client utilities:
- Configure OpenLDAP: During installation, configure the directory administrator password. You can reconfigure it later:
- Check Service Status: Verify the service is running:
ποΈ OpenLDAP Service: Slapd
The slapd service is the core daemon for OpenLDAP, responsible for handling LDAP requests.
π Managing Slapd:
- Start, stop, restart, and enable the slapd service using
systemctl
(details in the next section).
π Start, Stop, and Enable OpenLDAP Service
To manage the OpenLDAP service:
- Start the service:
- Enable the service (start automatically on boot):
- Stop the service:
- Restart the service:
βοΈ Configuration Files
The main configuration files for OpenLDAP are located in /etc/openldap/slapd.d/
. OpenLDAP uses a dynamic configuration model that allows changes without stopping the service.
π Important Configurations
-
/etc/openldap/slapd.d/cn=config
: -
Purpose: This directory contains the dynamic configuration settings for slapd. These configurations control everything from database management to user access.
-
Key files:
olcDatabase={1}mdb.ldif
: This file contains settings for the main database (MDB) used by OpenLDAP.olcRootDN
: The distinguished name (DN) for the root user, typicallycn=admin,dc=example,dc=com
.
-
/etc/openldap/ldap.conf
: -
Purpose: Client configuration file that sets default options for LDAP utilities like
ldapsearch
andldapadd
. -
Common options:
BASE dc=example,dc=com
: Specifies the base DN for client queries.URI ldap://localhost
: The URI of the LDAP server.
-
Schemas (
/etc/openldap/schema/
): - Purpose: Defines object classes and attributes for the directory. For example, the inetOrgPerson schema is commonly used for user management.
- Usage: Schemas are loaded dynamically and allow you to extend LDAPβs capabilities with custom attributes.
π Common Use Cases
- Adding a New Schema:
To add a schema (e.g., the
cosine
schema):
- Changing the RootDN Password: To change the administrator's password, generate an encrypted password and apply it to the configuration:
Then update the password:
ldapmodify -Y EXTERNAL -H ldapi:/// <<EOF
dn: olcDatabase={1}mdb,cn=config
replace: olcRootPW
olcRootPW: {SSHA}generated_password
EOF
- Configuring Access Controls: Set access controls to restrict who can read or write data. For example, restrict write access to the root user:
π Script for Common Usage and Configuration
Hereβs a script to manage OpenLDAP with common tasks like adding users, starting/stopping services, and more.
#!/bin/bash
# Function to add a user to OpenLDAP
add_user() {
ldapadd -x -D "cn=admin,dc=example,dc=com" -W <<EOF
dn: uid=$1,ou=users,dc=example,dc=com
objectClass: inetOrgPerson
sn: $1
cn: $1
uid: $1
userPassword: $2
EOF
}
# Start OpenLDAP service
start_service() {
sudo systemctl start slapd
echo "OpenLDAP service started."
}
# Stop OpenLDAP service
stop_service() {
sudo systemctl stop slapd
echo "OpenLDAP service stopped."
}
# Restart OpenLDAP service
restart_service() {
sudo systemctl restart slapd
echo "OpenLDAP service restarted."
}
# Main Menu
echo "OpenLDAP Management Script"
echo "1. Add User"
echo "2. Start Service"
echo "3. Stop Service"
echo "4. Restart Service"
read -p "Choose an option: " option
case $option in
1)
read -p "Enter username: " username
read -p "Enter password: " password
add_user $username $password
;;
2)
start_service
;;
3)
stop_service
;;
4)
restart_service
;;
*)
echo "Invalid option."
;;
esac
π Visual Representation
π³ LDAP Directory Structure
graph TD;
A[Root DN - dc=example,dc=com] --> B[Organizational Unit - ou=users];
B --> C[User - uid=user1];
B --> D[User - uid=user2];
A --> E[Organizational Unit - ou=groups];
E --> F[Group - cn=admins];
E --> G[Group - cn=users];
βοΈ Slapd Configuration Flow
graph TD;
A[slapd Configuration] --> B[cn=config];
B --> C[olcDatabase mdb.ldif];
B --> D[olcAccess Control];
C --> E[RootDN Configuration];
D --> F[Password Settings];