[Samba] copy the password hash from one user to another user

Simon FONTENEAU sfonteneau at tranquil.it
Wed Aug 17 08:44:36 UTC 2022

python :

from samba.netcmd.user import GetPasswordCommand
from samba.samdb import SamDB
from samba.param import LoadParm
from samba.credentials import Credentials
from samba.param import LoadParm

lp = LoadParm()
creds = Credentials()

samdb = SamDB(url='/var/lib/samba/private/sam.ldb', 
session_info=system_session(),credentials=creds, lp=lp)

testpawd = GetPasswordCommand()
testpawd.lp = lp
for user in samdb.search(base=basedn, 
        passwordhash = 

For assign a password hash to a user (bash) :

pdbedit --set-nt-hash=username "hash"

If you want to do it in python you will have to use the samr library


Le 09/08/2022 à 14:08, Marcos Ariel Negrini via samba a écrit :
> any idea?
> El 5/8/22 a las 10:49, Marcos Ariel Negrini via samba escribió:
>> Hello everyone:
>> for the integration of a system with samba4AD I need that when 
>> creating a user takes by default the password of another (for example 
>> that when creating the user B, is generated with the password of A). 
>> I was looking how to obtain the hash value of the password of 
>> A(ldbsearch -H /path/sam.ldb '(cn=A)' unicodepwd), but I did not find 
>> how to assign this hash to B. Is it possible to do this? I was trying 
>> to do all this with python, I managed without difficulty to change 
>> the password of B from a string, but not from the corresponding hash.
>> greetings

More information about the samba mailing list