Skip to content

Lesson 3 of 9 · 24m

LDIF and Safe Directory Changes

Write precise LDIF for adds, modifies, deletes, and renames while avoiding the classic destructive mistakes operators repeat.

LDIF looks deceptively harmless: a few text lines and a blank line. Then someone uses replace on member in a production group and removes 400 users because they only intended to add one. Directory changes are small text diffs with large blast radius. Treat them like database migrations, not configuration snippets.

LDIF is record-oriented

Entries are separated by blank lines. Continuation lines begin with one space. Base64 values use double colons:

dn: uid=amorales,ou=people,dc=sudosecurity,dc=org
changetype: add
objectClass: inetOrgPerson
objectClass: posixAccount
uid: amorales
cn: Ana Morales
sn: Morales
uidNumber: 10420
gidNumber: 10420
homeDirectory: /home/amorales
loginShell: /bin/bash
userPassword:: e1NTSEF9...

Apply it with authentication appropriate to the server:

ldapadd -x -H ldaps://ldap.sudosecurity.org   -D 'cn=Directory Manager' -W -f add-user.ldif

For OpenLDAP configuration changes, ldapmodify against cn=config is often safer than editing generated files under slapd.d directly.

Modify operations are atomic per operation, not per file

dn: uid=amorales,ou=people,dc=sudosecurity,dc=org
changetype: modify
replace: mail
mail: amorales@sudosecurity.org
-
add: telephoneNumber
telephoneNumber: +1-202-555-0148
-
delete: description

Each operation applies to an attribute. replace without values deletes the attribute. replace with values discards every existing value first. That distinction is the root of many multi-valued attribute incidents.

Add one member without losing the rest

Wrong for an existing group:

dn: cn=admins,ou=groups,dc=sudosecurity,dc=org
changetype: modify
replace: member
member: uid=amorales,ou=people,dc=sudosecurity,dc=org

Correct:

dn: cn=admins,ou=groups,dc=sudosecurity,dc=org
changetype: modify
add: member
member: uid=amorales,ou=people,dc=sudosecurity,dc=org

Before changing any multi-valued attribute, export the current values and count them. Afterward, compare expected versus actual counts.

Deletes and renames are sharper than edits

Delete an entire entry only after checking children and references:

dn: uid=contractor-temp,ou=people,dc=sudosecurity,dc=org
changetype: delete

A modrdn changes the naming attribute and optionally moves the entry:

dn: uid=amorales,ou=people,dc=sudosecurity,dc=org
changetype: modrdn
newrdn: uid=ana.morales
deleteoldrdn: 1
newsuperior: ou=people,dc=sudosecurity,dc=org

References in member, ACLs, application mappings, and logs do not magically update. Build a rename procedure that inventories dependents first.

Use dry-run discipline even without a dry-run flag

LDIF does not universally support a production-safe dry run. Your substitute is procedure:

ldapsearch -LLL -b "$DN" -s base '(objectClass=*)' > before.ldif
ldapmodify -f change.ldif
ldapsearch -LLL -b "$DN" -s base '(objectClass=*)' > after.ldif
diff -u before.ldif after.ldif

For large batches, run in a replica or staging directory first, then execute with a change window and rollback LDIF already generated.

Field notes

  • Lines longer than 76 characters are often folded; do not unfold them manually and corrupt binary or UTF-8 values.
  • ldapadd stops or continues differently by client and error type; never assume a multi-entry batch is all-or-nothing.
  • Store generated rollback LDIF beside the change ticket before applying destructive edits.
  • Schema violations are good news during import; they mean the directory rejected inconsistent data before applications consumed it.

Was this lesson helpful?

Sign in to rate