Linux directories and identity: OpenLDAP, SSSD, Kerberos and FreeIPA
Exit

Lesson 1 of 12 · The LDAP model

The LDAP model — DIT, DN, object classes, schemas and LDIF

40 min Debian 12 Versions Debian 12, Debian 13, Ubuntu 24.04, Rocky 9

Objective

Understand how an LDAP directory organizes its information, be able to name any entry by its DN, and write by hand the LDIF files used to populate and modify a directory.

Key points

  • A directory is a database optimized to be read very often (who is this user? which group are they in?) and modified rarely.
  • The entries form a tree, the DIT (Directory Information Tree). The root carries the domain name: dc=masterlinux,dc=lab.
  • Each entry has a unique name, the DN (Distinguished Name), which is read from the leaf to the root: uid=dev,ou=people,dc=masterlinux,dc=lab.
  • Object classes (objectClass) decide which attributes are required (MUST) and allowed (MAY). They are defined in schemas.
  • LDIF is the text format of the directory: you use it to import, export and modify.

1. Why a directory?

The MasterLinux office in Lille has 40 Linux servers. With local accounts, a new administrator has to be created 40 times, and a departure forgotten on a single server leaves a door open. With a directory, the account exists once; the servers come and read it.

LDAP (Lightweight Directory Access Protocol) is the protocol used to talk to a directory, on port 389 (clear text or with StartTLS) or 636 (LDAPS, encrypted from the start of the connection). Several products speak it:

Server Where you meet it
OpenLDAP (slapd) Debian, Ubuntu, hosting providers, appliances; the most widespread open-source one
389 Directory Server Red Hat's LDAP engine, at the heart of FreeIPA / IdM
Active Directory Windows estates; it speaks LDAP and Kerberos

The commands and the LDIF format in this course work with all three.

2. The DIT: a tree of names

dc=masterlinux,dc=lab                         ← root (the “suffix”)
├── ou=people                                 ← organizational unit: people
│   ├── uid=admin
│   ├── uid=dev
│   └── ou=support
│       └── uid=support-toulouse
├── ou=groups
│   ├── cn=developpeurs
│   └── cn=admins
└── ou=services                               ← technical accounts (backup, applications)
    └── uid=backup

The abbreviations to know:

Abbreviation Stands for Example
dc domain component dc=masterlinux
ou organizational unit ou=people
cn common name cn=developpeurs
uid user id (login name) uid=dev
o organization o=MasterLinux

3. DN and RDN

The RDN (Relative DN) is the name of the entry inside its folder: uid=dev. The DN adds all the parents up to the root:

uid=dev,ou=people,dc=masterlinux,dc=lab
└─RDN─┘ └────────── DN du parent ──────┘
  • The DN is read from left (leaf) to right (root), the opposite of a file path. In the diagram above, the right-hand part is the parent's DN.
  • Attribute names are not case-sensitive (UID=dev = uid=dev); for values, it depends on the attribute (uid is not, userPassword is).
  • A comma inside a value is escaped: cn=Support\, Nantes,ou=groups,….
  • The RDN must be an attribute that is present in the entry: an entry uid=dev,… necessarily contains uid: dev.

4. Object classes, attributes and schemas

An entry is not a bag of free-form attributes. Each objectClass states what is required and what is allowed:

Class Type Required (MUST) Used for
inetOrgPerson STRUCTURAL cn, sn a person: mail, telephoneNumber, displayName…
posixAccount AUXILIARY cn, uid, uidNumber, gidNumber, homeDirectory the Linux account: loginShell, gecos
shadowAccount AUXILIARY uid the equivalent of /etc/shadow: shadowLastChange, shadowMax
posixGroup STRUCTURAL cn, gidNumber a Linux group; members listed with memberUid
groupOfNames STRUCTURAL cn, member a group whose members are DNs
organizationalUnit STRUCTURAL ou a “folder”

An entry has exactly one STRUCTURAL class (what it is) and as many AUXILIARY classes as needed (added “capabilities”).

Classes and attributes are defined in schemas. On OpenLDAP, the most used ones are:

Schema Provides
core organizationalUnit, dcObject, basic attributes (always loaded)
cosine historical attributes (uid, mail…)
inetorgperson inetOrgPerson (depends on cosine)
nis posixAccount, shadowAccount, posixGroup (RFC 2307)
# List the schemas loaded on an OpenLDAP server (local root access)
ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:/// -b cn=schema,cn=config dn
# dn: cn=schema,cn=config
# 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

If you add an entry without a required attribute, the server answers for example ldap_add: Object class violation (65) additional info: object class 'posixAccount' requires attribute 'homeDirectory'.

memberUid or member?

Two ways of describing the members of a group coexist:

  • RFC 2307 (nis schema): posixGroup + memberUid: dev — a plain name; this is the OpenLDAP default on Debian.
  • RFC 2307bis / groupOfNames: member: uid=dev,ou=people,dc=masterlinux,dc=lab — a full DN; this is the model used by FreeIPA and Active Directory, and it allows nested groups.

SSSD can read both, but you have to tell it which one with ldap_schema = rfc2307 or rfc2307bis (lesson 3.1). Getting it wrong gives empty groups.

5. The LDIF format

An LDIF file is a series of entries separated by an empty line. Each entry starts with its dn: line.

# Comment: a line that starts with #
version: 1

dn: uid=admin,ou=people,dc=masterlinux,dc=lab
objectClass: inetOrgPerson
objectClass: posixAccount
uid: admin
cn: Administrateur
sn: Admin
mail: admin@masterlinux.lab
uidNumber: 10000
gidNumber: 10000
homeDirectory: /home/admin
loginShell: /bin/bash

Three rules that catch everyone out:

  1. :: = base64 value. A tool encodes any value that contains an accented character, starts with a space or contains a non-printable character: description:: w4lxdWlwZS…. Decode it with base64 -d.
  2. Folded lines. A line that is too long (more than 76 characters by default with ldapsearch) continues on the next line, which starts with a space. A grep on the full DN then fails. Join the lines first: ldapsearch -o ldif-wrap=no … or sed -e ':a;N;$!ba;s/\n //g'.
  3. Parents first. ldapadd processes the entries in file order: ou=people must come before uid=dev,ou=people,…, otherwise you get No such object (32).
# Decode a base64 attribute
echo 'w4lxdWlwZSBzdXBwb3J0IOKAlCBhZ2VuY2UgZGUgQ2FzYWJsYW5jYQ==' | base64 -d
# Équipe support — agence de Toulouse

# Join the folded lines, then isolate one entry (awk “paragraph” mode)
sed -e ':a;N;$!ba;s/\n //g' export.ldif | awk 'BEGIN{RS=""} /uidNumber: 10007/'

The same format is also used for modifications (changetype: modify): that is the next lesson.

6. Choosing numeric identifiers well

uidNumber and gidNumber are what the kernel really sees: they are what gets written to disk as the owners of files.

  • Reserve a range that never overlaps local accounts: for example 10000 to 59999 for the directory (local Debian/Rocky accounts start at 1000).
  • Never reuse a freed uidNumber: the files of the former owner would belong to the new one.
  • The same uidNumber on two entries = two people sharing the same files. A simple check: getent passwd | awk -F: '{print $3}' | sort | uniq -d.

By version

Item Debian 12 Debian 13 Ubuntu 24.04 Rocky 9
Server package slapd (OpenLDAP 2.5) slapd (OpenLDAP 2.6) slapd (OpenLDAP 2.6) openldap-servers from the plus repository (not enabled by default)
Client tools ldap-utils ldap-utils ldap-utils openldap-clients
Directory offered by the vendor OpenLDAP OpenLDAP OpenLDAP 389 Directory Server / FreeIPA
Schemas loaded at installation core, cosine, nis, inetorgperson same same core only (the others must be loaded)

Red Hat has not shipped the OpenLDAP server since RHEL 8 (the clients remain). On Rocky, you therefore install either openldap-servers from the plus repository, or FreeIPA (lesson 3.4).

Common pitfalls

  • Searching for the full DN with grep in a folded export: it is split across two lines. Use -o ldif-wrap=no or join the lines.
  • Forgetting the RDN attribute: the entry uid=dev,… without uid: dev is rejected (Naming violation).
  • Two unrelated STRUCTURAL classes (inetOrgPerson + posixGroup): rejected. A person is not a group.
  • Mixing memberUid and member from one group to another: clients that expect one do not see the other.

Lab

You prepare the import files for the MasterLinux directory, exactly as you would before a first ldapadd:

  1. the root and the two units people and groups, in the right order;
  2. the Linux account dev (person + POSIX account + shadow);
  3. the group developpeurs;
  4. find a DN in an export with folded lines;
  5. decode a base64 attribute.

The lab container has no running LDAP server: you work on the files, which are the ones you would import on a real server with ldapadd -x -D cn=admin,dc=masterlinux,dc=lab -W -f base.ldif.

Quiz

  1. What does DN stand for, and in which direction is it read?
  2. What is the difference between a STRUCTURAL class and an AUXILIARY class?
  3. What does description:: (double colon) mean in an LDIF file?
  4. Why must ou=people appear before uid=dev,ou=people,… in an imported file?
  5. One group uses memberUid, another uses member. Which one follows RFC 2307?
Answers
  1. Distinguished Name, the unique name of an entry; it is read from the leaf (left) to the root (right).
  2. The STRUCTURAL class says what the entry is (only one per entry); an AUXILIARY class adds attributes (posixAccount adds uidNumber, homeDirectory…).
  3. The value is base64-encoded (accented characters, special characters, leading space).
  4. ldapadd processes the entries in order and rejects an entry whose parent does not exist (No such object).
  5. memberUid (posixGroup, nis schema). member contains DNs (RFC 2307bis, groupOfNames).

Practical exercises

To do on Debian 12

Do this lab on your own machine

The terminal is yours: a Linux VM (VirtualBox, VMware, Hyper-V, WSL…) or Docker/Podman. The ml tool prepares the exercise and checks your work; the lesson is validated here automatically.

Create a free account to get your personal command and have your labs checked automatically.

Create my account

Tasks

  1. Write /root/annuaire/base.ldif with, in this order, the root dc=masterlinux,dc=lab (classes dcObject and organization, attributes dc: masterlinux and o: MasterLinux), then the units ou=people and ou=groups (class organizationalUnit). Separate the entries with an empty line.
    Show a hintThree “dn: …” blocks separated by an empty line. The root must come first: ldapadd rejects an entry whose parent does not exist yet.
    Show the solution
    printf 'dn: dc=masterlinux,dc=lab\nobjectClass: dcObject\nobjectClass: organization\ndc: masterlinux\no: MasterLinux\n\ndn: ou=people,dc=masterlinux,dc=lab\nobjectClass: organizationalUnit\nou: people\n\ndn: ou=groups,dc=masterlinux,dc=lab\nobjectClass: organizationalUnit\nou: groups\n' > /root/annuaire/base.ldif

    Check : awk 'BEGIN{RS=""} {e=tolower($0)} e~/(^|\n)dn: *dc=masterlinux,dc=lab(\n|$)/ && e~/objectclass: *dcobject/ && e~/objectclass: *organization(\n|$)/ && e~/\ndc: *masterlinux/ && e~/\no: *masterlinux/ {r=NR} e~/(^|\n)dn: *ou=people,dc=masterlinux,dc=lab(\n|$)/ && e~/objectclass: *organizationalunit/ {p=NR} e~/(^|\n)dn: *ou=groups,dc=masterlinux,dc=lab(\n|$)/ && e~/objectclass: *organizationalunit/ {g=NR} END{exit !(r && p && g && r<p && r<g)}' /root/annuaire/base.ldif

  2. Write /root/annuaire/users.ldif: the entry uid=dev,ou=people,dc=masterlinux,dc=lab with the classes inetOrgPerson, posixAccount and shadowAccount, and the attributes cn, sn, uidNumber 10001, gidNumber 10001, homeDirectory /home/dev and loginShell /bin/bash.
    Show a hintinetOrgPerson requires cn and sn; posixAccount requires uid, uidNumber, gidNumber and homeDirectory. The attribute uid: dev must also be present, because it forms the RDN.
    Show the solution
    printf 'dn: uid=dev,ou=people,dc=masterlinux,dc=lab\nobjectClass: inetOrgPerson\nobjectClass: posixAccount\nobjectClass: shadowAccount\nuid: dev\ncn: Developpeur\nsn: Dev\nuidNumber: 10001\ngidNumber: 10001\nhomeDirectory: /home/dev\nloginShell: /bin/bash\n' > /root/annuaire/users.ldif

    Check : awk 'BEGIN{RS=""} {e=tolower($0)} e~/(^|\n)dn: *uid=dev,ou=people,dc=masterlinux,dc=lab(\n|$)/ && e~/objectclass: *inetorgperson/ && e~/objectclass: *posixaccount/ && e~/objectclass: *shadowaccount/ && e~/\ncn: *[^\n]+/ && e~/\nsn: *[^\n]+/ && e~/\nuid: *dev(\n|$)/ && e~/\nuidnumber: *10001(\n|$)/ && e~/\ngidnumber: *10001(\n|$)/ && e~/\nhomedirectory: *\/home\/dev(\n|$)/ && e~/\nloginshell: *\/bin\/bash(\n|$)/ {ok=1} END{exit !ok}' /root/annuaire/users.ldif

  3. Write /root/annuaire/groups.ldif: the group cn=developpeurs,ou=groups,dc=masterlinux,dc=lab (class posixGroup), gidNumber 10001, with dev as a member (attribute memberUid).
    Show a hintIn the nis schema (RFC 2307), posixGroup lists its members by login name: memberUid: dev.
    Show the solution
    printf 'dn: cn=developpeurs,ou=groups,dc=masterlinux,dc=lab\nobjectClass: posixGroup\ncn: developpeurs\ngidNumber: 10001\nmemberUid: dev\n' > /root/annuaire/groups.ldif

    Check : awk 'BEGIN{RS=""} {e=tolower($0)} e~/(^|\n)dn: *cn=developpeurs,ou=groups,dc=masterlinux,dc=lab(\n|$)/ && e~/objectclass: *posixgroup/ && e~/\ncn: *developpeurs(\n|$)/ && e~/\ngidnumber: *10001(\n|$)/ && e~/\nmemberuid: *dev(\n|$)/ {ok=1} END{exit !ok}' /root/annuaire/groups.ldif

  4. The file /root/annuaire/export.ldif comes from another directory. Write in /root/annuaire/dn.txt the full DN of the entry whose uidNumber is 10007 (watch out for folded lines).
    Show a hintIn LDIF, a line that starts with a space continues the previous line. Join them first: sed -e ':a;N;$!ba;s/\n //g'
    Show the solution
    sed -e ':a;N;$!ba;s/\n //g' /root/annuaire/export.ldif | awk 'BEGIN{RS=""} /uidNumber: 10007/' | sed -n 's/^dn: //p' > /root/annuaire/dn.txt

    Check : test "$(cat /root/annuaire/dn.txt 2>/dev/null)" = "uid=support-toulouse,ou=support,ou=people,dc=masterlinux,dc=lab"

  5. In the same entry, the description attribute is encoded (double colon “::”). Write its decoded value in /root/annuaire/description.txt.
    Show a hint“::” means base64. Isolate the value from entry 10007, then base64 -d.
    Show the solution
    sed -e ':a;N;$!ba;s/\n //g' /root/annuaire/export.ldif | awk 'BEGIN{RS=""} /uidNumber: 10007/' | sed -n 's/^description:: //p' | base64 -d > /root/annuaire/description.txt

    Check : test "$(printf '%s' "$(cat /root/annuaire/description.txt 2>/dev/null)" | base64 -w0)" = "w4lxdWlwZSBzdXBwb3J0IOKAlCBhZ2VuY2UgZGUgQ2FzYWJsYW5jYQ=="