Install OpenLDAP on Ubuntu
LDAP is often used as part of the IAM landscape and while many organization may already have a directory infrastructure, the steps below describe how to install OpenLDAP on Ubuntu.
This document is not intended to serve as a comprehensive guide to installing and configuring OpenLDAP.
Install OpenLDAP
Install SLAP and other LDAP utilities
sudo apt install slapd ldap-utils
Enter the password when the installer prompts you.
Validate that your installation was successful by running the slapcat
command. You should see output similar to the example below:
dn: dc=nodomainobjectClass: topobjectClass: dcObjectobjectClass: organizationo: nodomaindc: nodomainstructuralObjectClass: organizationentryUUID: 2f3ff140-6a54-103c-99c6-8144871930cacreatorsName: cn=admin,dc=nodomaincreateTimestamp: 20220517174028ZentryCSN: 20220517174028.015525Z#000000#000#000000modifiersName: cn=admin,dc=nodomainmodifyTimestamp: 20220517174028Z
Update the configuration to use your domain. This can be done by using the package reconfiguration utility shown below.
sudo dpkg-reconfigure slapd
You will be prompted to determine if the OpenLDAP server configuration should be omitted or not. Select No
and proceed to configure your OpenLDAP settings. The utility will prompt you for the information below.
- DNS information for constructing the base DN of your LDAP directory; ie. ldap.local
- Enter the name of your organization to be used in the base DN; ie. test
- Re-enter the name of your administration password and confirm it.
- Choose to remove SLAPD database when slapd package is removed.
After completing the reconfiguration process, run the slapcat utility again and you should see output similar to the example below (your domain information will be different)
dn: dc=ldap,dc=localobjectClass: topobjectClass: dcObjectobjectClass: organizationo: openiamdc: ldapstructuralObjectClass: organizationentryUUID: bc830e48-6a54-103c-8324-c7e057e95c40creatorsName: cn=admin,dc=ldap,dc=localcreateTimestamp: 20220517174425ZentryCSN: 20220517174425.014134Z#000000#000#000000modifiersName: cn=admin,dc=ldap,dc=localmodifyTimestamp: 20220517174425Z
Validate that your directory is running
After the above steps have been completed, you can validate that your directory is operational by running the following command:
sudo systemctl status slapd
You should see output similar to the example below:
● slapd.service - LSB: OpenLDAP standalone server (Lightweight Directory Access Protocol)Loaded: loaded (/etc/init.d/slapd; generated)Drop-In: /usr/lib/systemd/system/slapd.service.d└─slapd-remain-after-exit.confActive: active (running) since Tue 2022-05-17 17:44:25 UTC; 3min 28s agoDocs: man:systemd-sysv-generator(8)Process: 645482 ExecStart=/etc/init.d/slapd start (code=exited, status=0/SUCCESS)Tasks: 3 (limit: 38496)Memory: 3.0MCPU: 35msCGroup: /system.slice/slapd.service└─645488 /usr/sbin/slapd -h ldap:/// ldapi:/// -g openldap -u openldap -F /etc/ldap/slapd.dMay 17 17:44:25 localhost systemd[1]: slapd.service: Succeeded.May 17 17:44:25 localhost systemd[1]: Stopped LSB: OpenLDAP standalone server (Lightweight Directory Access Protocol).May 17 17:44:25 localhost systemd[1]: Starting LSB: OpenLDAP standalone server (Lightweight Directory Access Protocol)...May 17 17:44:25 localhost slapd[645487]: @(#) $OpenLDAP: slapd 2.4.57+dfsg-3 (May 15 2021 23:03:34) $Debian OpenLDAP Maintainers <pkg-openldap-devel@lists.alioth.debian.org>May 17 17:44:25 localhost slapd[645488]: slapd startingMay 17 17:44:25 localhost slapd[645482]: Starting OpenLDAP: slapd.May 17 17:44:25 localhost systemd[1]: Started LSB: OpenLDAP standalone server (Lightweight Directory Access Protocol).
Populate your directory
The following steps describe how you can create the initial structure of your directory. Along the way, utilities to review your configuration will also be described.
To check the BaseDN, use the utility below:
ldapsearch -x -LLL -b "" -s base namingContexts
To view the RootDN, use the command below:
ldapsearch -H ldapi:/// -Y EXTERNAL -b "cn=config" -LLL -Q | grep olcRootDN:
You will see output similar to the example below:
olcRootDN: cn=admin,cn=configolcRootDN: cn=admin,dc=ldap,dc=local
Define the basic structure for your directory using the steps and examples shown below:
- Create an ldif file as shown
nano basedn.ldif
dn: ou=people,dc=ldap,dc=localobjectClass: organizationalUnitou: peopledn: ou=groups,dc=ldap,dc=localobjectClass: organizationalUnitou: groupsdn: ou=dept1,ou=people,dc=ldap,dc=localobjectClass: organizationalUnitou: dept1dn: ou=dept2,ou=people,dc=ldap,dc=localobjectClass: organizationalUnitou: dept2dn: ou=admins,ou=people,dc=ldap,dc=localobjectClass: organizationalUnitou: adminsdn: ou=disabledusers,dc=ldap,dc=localobjectClass: organizationalUnitou: disableduser
- Load the file using the
ldapadd
utility.
sudo ldapadd -x -D cn=admin,dc=ldap,dc=local -W -f basedn.ldif
Create test users
Create an ldif file as shown
nano testusers.ldif
dn: uid=james.brown,ou=people,dc=ldap,dc=localobjectclass: topobjectclass: personobjectclass: organizationalPersonobjectclass: inetOrgPersonuid: james.browncn: Jamessn: Brownmail: james.brown@test.localpostalCode: 12345userPassword: password123dn: uid=Mick.Jagger,ou=dept1,ou=people,dc=ldap,dc=localobjectclass: topobjectclass: personobjectclass: organizationalPersonobjectclass: inetOrgPersonuid: mick.jaggercn: Micksn: Jaggermail: mick.jagger@test.localpostalCode: 12345userPassword: password123dn: uid=Elton.John,ou=dept1,ou=people,dc=ldap,dc=localobjectclass: topobjectclass: personobjectclass: organizationalPersonobjectclass: inetOrgPersonuid: Elton.Johncn: Eltonsn: Johnmail: Elton.John@test.localinitials: EJtitle: ManagerpostalCode: 12345userPassword: password123employeeNumber:12334343employeeType: EmployeedepartmentNumber: 205preferredLanguage: enou: peopleou: dept1telephoneNumber: +1 408 555 1862facsimileTelephoneNumber: +1 408 555 1992mobile: +1 408 555 1941
Use the command below to load the users.
sudo ldapadd -x -D cn=admin,dc=ldap,dc=local -W -f testusers.ldif
- Create test groups
dn: cn=developers,ou=groups,dc=ldap,dc=localobjectclass: topobjectclass: groupOfNamescn: developersmember: uid=james.brown,ou=people,dc=ldap,dc=localmember: uid=Mick.Jagger,ou=dept1,ou=people,dc=ldap,dc=localdn: cn=admins,ou=groups,dc=ldap,dc=localobjectclass: topobjectclass: groupOfNamescn: adminsmember: uid=james.brown,ou=people,dc=ldap,dc=local
LDAP Search examples
Show all users
sudo ldapsearch -x -b dc=ldap,dc=local -H ldap://localhost
Show for users with objectClass Inetorgperson
sudo ldapsearch -x -b dc=ldap,dc=local -H ldap://localhost -D "cn=admin,dc=ldap,dc=local" -W "objectclass=inetOrgPerson"
Secure your directory
Generate a self-signed certificate
- Create directories for the certificates
You can define your own location to store the certificates or you can use the default location: /etc/ldap/sasl2/
mkdir -p /etc/ssl/openldap/{private,certs,newcerts}
- Open the
/usr/lib/ssl/openssl.cnf
configuration file and set the directory for storing SSL/TLS certificates and keys under the [ CA_default ]
nano /usr/lib/ssl/openssl.cnf
Update the dir
entry as shown below.
#dir = ./demoCA # Where everything is keptdir = /etc/ssl/openldap
- Create the following files which will be used for tracking during the certificate creation process later
echo "1001" > /etc/ssl/openldap/serialtouch /etc/ssl/openldap/index.txt
- Generate the ldap server key
sudo openssl genrsa -aes256 -out /etc/ssl/openldap/private/cakey.pem 2048sudo openssl rsa -in /etc/ssl/openldap/private/cakey.pem -out /etc/ssl/openldap/private/cakey.pem
- Create the certificate
sudo openssl req -new -x509 -days 3650 -key /etc/ssl/openldap/private/cakey.pem -out /etc/ssl/openldap/certs/cacert.pem
- Generate the ldap server key
sudo openssl genrsa -aes256 -out /etc/ssl/openldap/private/ldapserver-key.key 2048sudo openssl rsa -in /etc/ssl/openldap/private/ldapserver-key.key -out /etc/ssl/openldap/private/ldapserver-key.key
- Generate the CSR
sudo openssl req -new -days 365 -key ldap_server.key -out ldap_server.csrsudo openssl req -new -key /etc/ssl/openldap/private/ldapserver-key.key -out /etc/ssl/openldap/certs/ldapserver-cert.csr
- Generate the LDAP server certificate and sign it with CA key and certificate generated above.
sudo openssl ca -keyfile /etc/ssl/openldap/private/cakey.pem -cert /etc/ssl/openldap/certs/cacert.pem -in /etc/ssl/openldap/certs/ldapserver-cert.csr -out /etc/ssl/openldap/certs/ldapserver-cert.crt
- Validate the certificate using the command below.
openssl verify -CAfile /etc/ssl/openldap/certs/cacert.pem /etc/ssl/openldap/certs/ldapserver-cert.crt
You should expect output similar to the example below:
/etc/ssl/openldap/certs/ldapserver-cert.crt: OK
- Now that the certificates have been generated, change the ownership such that they files are owned by the
openldap
user.
chown -R openldap: /etc/ssl/openldap/
Configure SSL on OpenLDAP
- Configure the ldap server to use the certificates. Create a new ldif file (ldap_ssl.ldif) as shown below:
dn: cn=configchangetype: modifyreplace: olcTLSCACertificateFileolcTLSCACertificateFile: /etc/ssl/openldap/certs/cacert.pem-replace: olcTLSCertificateKeyFileolcTLSCertificateKeyFile: /etc/ssl/openldap/private/ldapserver-key.key-replace: olcTLSCertificateFileolcTLSCertificateFile: /etc/ssl/openldap/certs/ldapserver-cert.crt
- Apply the configuration using the comment below
ldapmodify -Y EXTERNAL -H ldapi:/// -f ldap_ssl.ldif
To validate that the certificates have been set, run the command below:
sudo slapcat -b "cn=config" | grep -E "olcTLS"
Configure LDAP Client
- Update the
/etc/ldap/ldap.conf
by adding the following lines
ssl start_tlsssl on
- Restart your ldap server
sudo systemctl restart slapd