Search code examples
pythonlinuxactive-directoryldappython-ldap

Python LDAP - Move user to different OU


Background:
I have been using the python-ldap module on my Linux machine to manage user accounts on a remote Windows Server 2008. I have been able to search for, create, and modify users, with the exception of changing a user's 'ou'.

I have tried using 'modify_s' and 'modrdn_s' but since modrdn only allows you to change the first part of a dn, I haven't had luck modifying the 'ou' or moving a user to a new 'ou'.

Temporarily I am creating a new user and copying all the attributes that I am able to from the old user, then deleting the old user. But this doesn't allow me to retain the user creation date and other un-editable information.

I have thoroughly searched the internet and found a few solutions, but:
on other operating systems: How do you move a user to a different OU using Python
and other programming languages: Active Directory LDAP move user to different OU - Ruby
Is this possible in python-ldap on Linux or are there any work-arounds?
Thank you!


Solution

  • You need to use rename_s and specify the newsuperior parameter.

    Quick sample code:

    ldap.rename_s('cn=UserName,ou=OldContainer,dc=example,dc=com', 'cn=UserName', 'ou=NewContainer,dc=example,dc=com')