Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 38 ยท LDAP Protocol and Queries

Modifying Entries: Add, Modify, Delete and Rename

Practise the LDAP write operations with ldapadd, ldapmodify, ldapdelete and ldappasswd: atomic modifications, replace versus add, renaming, moving and error handling.

Reading a directory is only half the job. Administrators and applications also create users, change attributes, rename entries and remove accounts. This lesson shows the four write operations in practice, with ldapadd, ldapmodify, ldapdelete and ldappasswd, explains how the individual changes behave, and shows how to avoid the common mistakes. The stand-in at the end models the rules in Python so you can see what the server checks.

All commands below assume the lab server from Lab Setup and Tools and an administrator DN that is allowed to write.

Adding an entry

Write the new entry in LDIF (see LDIF) and load it:

LDIF
dn: uid=dstar,ou=People,dc=example,dc=com
objectClass: inetOrgPerson
uid: dstar
cn: Dana Star
sn: Star
mail: [email protected]
Terminal
ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f dana.ldif

The server checks that the parent exists, the DN is new, all required attributes are present and every attribute is allowed by the object classes. The failure codes are noSuchObject, entryAlreadyExists, objectClassViolation and invalidAttributeSyntax. The RDN attribute (uid here) must appear in the entry.

Modifying attributes

A modify request lists operations for one entry. Each operation targets one attribute:

Operation Effect Fails when
add Adds the given values A value already exists, or the attribute is single-valued and has one
delete Removes the given values, or the whole attribute if none are listed The attribute or value does not exist
replace Replaces all existing values with the given ones; with no values it removes the attribute (does not fail if the attribute is missing)
LDIF
dn: uid=dstar,ou=People,dc=example,dc=com
changetype: modify
add: mail
mail: [email protected]
-
replace: title
title: Team Lead
-
delete: telephoneNumber
telephoneNumber: +1 555 0100
Terminal
ldapmodify -x -D "cn=admin,dc=example,dc=com" -W -f change-dana.ldif

Two important properties:

  1. Atomic: if any operation in the request fails, none of them takes effect.
  2. Ordered: operations run in the sequence you wrote them.

Choose the right operation

add Append values.
replace Overwrite all values.
delete Remove values or the attribute.

All changes in one request succeed or fail together

1 / 2

replace versus add and delete

replace is the safest way to set a single-valued attribute, because you do not need to know the old value. For a multi-valued attribute such as member, replace will wipe out all other values, which is almost never what you want for a group with 500 members. Use add and delete for individual values instead:

LDIF
dn: cn=engineers,ou=Groups,dc=example,dc=com
changetype: modify
add: member
member: uid=dstar,ou=People,dc=example,dc=com

Replacing member with a single value silently removes every other member. Always add and delete specific values when updating groups.

Deleting entries

Terminal
ldapdelete -x -D "cn=admin,dc=example,dc=com" -W "uid=dstar,ou=People,dc=example,dc=com"

Only leaf entries can be deleted. To delete a branch, delete the children first (or use ldapdelete -r, which does it recursively, a risky shortcut because a typo can wipe a whole subtree). Deleted entries are gone for good in OpenLDAP; Active Directory can keep them in a tombstone or, with the Recycle Bin enabled, in a recoverable state (see the administration chapter).

Renaming and moving (Modify DN)

LDIF
dn: uid=dstar,ou=People,dc=example,dc=com
changetype: modrdn
newrdn: uid=dana.star
deleteoldrdn: 1

That renames the entry. To move it to another parent, add newsuperior: ou=Staff,dc=example,dc=com. The server rewrites the entry's DN but also needs to fix references, such as member values in groups. Whether it does this automatically depends on the product and on overlays; in OpenLDAP the refint overlay maintains such references, and in Active Directory many link attributes (membership, manager) are maintained by the server.

Changing passwords

Avoid writing userPassword by hand. Use the Password Modify extended operation, which lets the server hash the password with its configured scheme (for example or ):

Terminal
ldappasswd -x -D "cn=admin,dc=example,dc=com" -W -S "uid=dstar,ou=People,dc=example,dc=com"

-S prompts for the new password. Active Directory stores passwords differently: unicodePwd, set over an encrypted connection (LDAPS or SASL with encryption), containing the password in double quotes encoded as UTF-16LE.

Handling errors

Error Likely cause Fix
Insufficient access (50) Bound account lacks write rights Use an account with access, or fix the access control
Object class violation (65) Attribute not allowed by classes Add the proper object class
No such attribute (16) on delete Value or attribute missing Check the current values first
Type or value exists (20) on add Duplicate Use replace or remove the value
Constraint violation (19) Policy such as password rules Read the message text
Server is unwilling to perform (53) Policy such as requiring TLS Use LDAPS or StartTLS

Modify rules (Python stand-in)

The stand-in applies a list of modify operations atomically to an entry (a copy is changed, and only committed if everything succeeds), using the rules from the table above.

Python Atomic modify (stand-in)

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.

The failing request changes nothing: the first operation is rolled back because the second operation failed. This is the atomic behavior the protocol guarantees.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A modify request has three operations and the third fails. What is the outcome?

    A modify request is atomic: all operations succeed or the entry stays unchanged.
  2. 2. You want to add one new member to a group of 500. Which operation is right?

    replace would overwrite all members. add appends one value.
  3. 3. Which entries can LDAP delete?

    A delete on an entry with children fails with notAllowedOnNonLeaf.
  4. 4. Why prefer ldappasswd over writing userPassword directly?

    The Password Modify operation lets the server apply policy and hashing.

Summary

  • ldapadd, ldapmodify and ldapdelete apply LDIF records; ldappasswd changes passwords with the Password Modify extended operation.
  • A modify record's operations (add, delete, replace) are applied in order and atomically.
  • Use add and delete for individual values of multi-valued attributes; replace sets all values.
  • Only leaf entries are deleted; Modify DN renames and moves, which changes the DN.
  • Read result codes: 50 access, 65 schema, 20 duplicate, 16 missing, 53 policy.

Next: Controls and Paging