Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 38 · Directory Concepts

LDIF: The LDAP Data Interchange Format

Learn LDIF, the text format for LDAP entries and changes: record syntax, line folding, base64 values, add, modify, delete and modrdn change records.

LDIF (LDAP Data Interchange Format, RFC 2849) is the plain-text format for exporting, importing and changing directory data. Administrators use it for backups, migrations, bulk loads and to describe changes in scripts. Every tool in the OpenLDAP suite reads and writes it, and Active Directory has its own LDIF tool (ldifde).

In this lesson you will learn the two kinds of LDIF file (content records and change records), the formatting rules (comments, line folding, base64) and how to write each change type. A Python stand-in parser will show you how the format is read.

Content records

The simplest LDIF file is a list of entries, separated by blank lines:

LDIF
# Two entries
dn: ou=People,dc=example,dc=com
objectClass: organizationalUnit
ou: People

dn: uid=asmith,ou=People,dc=example,dc=com
objectClass: inetOrgPerson
uid: asmith
cn: Alice Smith
sn: Smith
mail: [email protected]

Rules:

  • Each record starts with a dn: line.
  • Each following line is attribute: value. Put exactly one space after the colon.
  • Repeat the attribute line for each value of a multi-valued attribute.
  • A blank line ends the record.
  • Lines starting with # are comments.
  • Parents must come before children, because loading each entry follows the add rules.
  • A first line version: 1 is optional and allowed.

Load it with ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f people.ldif, and export with ldapsearch -LLL -x ... where -LLL prints clean LDIF without comments or version lines.

LDIF at a glance

Content Records that describe entries.
Changes add, modify, delete, modrdn.
Folding Long lines continue after a space.
Base64 Safe form for odd values.

Blank line = end of record

1 / 2

Line folding

A line longer than 76 characters is conventionally folded: the next physical line begins with a single space, which means "continue the previous line":

LDIF
description: This description is long enough that it has been wrapped onto a sec
 ond physical line, which begins with exactly one space.

The parser removes the line break and that one space.

Base64 values and ::

Some values cannot be written as plain text: values with leading spaces, a leading : or <, non-UTF-8 bytes (JPEG photos, certificates), or newlines. LDIF encodes these in base64 and signals it with a double colon:

LDIF
cn:: QWxpY2Ugw4BsdmFyZXo=
jpegPhoto:: /9j/4AAQSkZJRgABAQAAAQABAAD...

Passwords stored as userPassword are base64 when printed, because they are binary-safe values (for example hashes). A value from a file can also be given with a URL form: jpegPhoto:< file:///tmp/alice.jpg.

Change records

For modifications you add changetype: after the dn: line.

Add

LDIF
dn: cn=hq-laser-1,ou=Printers,dc=example,dc=com
changetype: add
objectClass: device
cn: hq-laser-1
description: HQ laser printer

Delete

LDIF
dn: uid=olduser,ou=People,dc=example,dc=com
changetype: delete

Modify

A modify record lists one or more operations, each beginning with add:, delete: or replace: and a dash - line between them:

LDIF
dn: uid=asmith,ou=People,dc=example,dc=com
changetype: modify
add: telephoneNumber
telephoneNumber: +1 555 0100
-
replace: title
title: Principal Engineer
-
delete: description

Meanings: add appends values; replace swaps all values (creating the attribute if it is missing); delete followed by attribute lines removes those specific values, or the whole attribute if no value is listed. All operations in one record are applied atomically: either all succeed or none does.

ModifyDN (rename or move)

LDIF
dn: uid=asmith,ou=People,dc=example,dc=com
changetype: modrdn
newrdn: uid=alice.smith
deleteoldrdn: 1
newsuperior: ou=Staff,dc=example,dc=com

deleteoldrdn: 1 removes the old RDN value from the entry; 0 keeps it as an extra value. newsuperior is optional and moves the entry.

Apply change records with ldapmodify, or ldapadd for adds. Check their order the same way as you would for scripts.

Active Directory notes

ldifde works like ldapmodify for AD. Instead of passwords, AD users usually use unicodePwd, which needs a quoted UTF-16LE value and an encrypted LDAPS connection. Many LDIF files for AD also use dn: lines in the AD style (CN=Alice Smith,OU=Engineering,DC=corp,DC=example,DC=test).

A small LDIF parser (Python stand-in)

The block below parses content records, handling comments, folded lines and base64 values. It is a learning model: real parsers also handle change records, URL values and error checks.

Python Parse content records

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Write LDIF from Python data

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When a value looks strange in ldapsearch output with ::, decode it with echo 'QWxpY2U=' | base64 -d. It is usually harmless text with non-ASCII letters.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does a line starting with a single space mean in LDIF?

    LDIF folds long lines; a leading space joins the line to the one above.
  2. 2. What does the double colon in cn:: QWxpY2U= signal?

    Attribute names followed by :: carry base64 values.
  3. 3. In a modify change record, how are separate operations separated?

    A single - line separates operations inside one modify record. A blank line ends the whole record.
  4. 4. Why must a parent entry appear before its children in an add LDIF file?

    The server applies each record in order and refuses an add without an existing parent.

Summary

  • LDIF is the text format for entries and changes; records are separated by blank lines.
  • Long lines fold with a leading space; unsafe values use base64 with ::.
  • Change records use changetype: add, delete, modify or modrdn; a modify record is atomic.
  • Load files with ldapadd and ldapmodify (or ldifde in Active Directory); export with ldapsearch -LLL.
  • Order matters: create parents before children.

Next: LDAP Operations