answerSeeker
answerSeeker

Reputation: 2772

How can I create an encrypted password for useradd using python crypt or bcrypt?

I'm trying to create a hashed password for debian/ubuntu useradd. I use python to create the hash and use bash to create the user with the hashed password. I think I'm doing something wrong because when I try to login with the user's password it doesn't work. I think it wants me to put in the actual hash.

In python, I created the encrypted hash password like this. It's in a python class but I shortened it so you could get the picture.

import crypt
import random

def salt(self):
    saltchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    return random.choice(saltchars) + random.choice(saltchars)

def custom_user(self):
    user = self.textEditUser.text()
    password = self.textEditPassword.text()
    hashed_pw = crypt.crypt(str(password), "$6$"+self.salt()) # say password is "password"
    print hashed_pw #returns $6$5l$lrm4UvmiYZcducdBFs8NortA.zeWuXrnmoVEYtLxmmDIGLN.9gjs.X6Z/fR6wDkh06lnDN8LTjzwrImSCR72T/

I use a separate bash script to create the user like this:

#!/bin/bash

PASSWORD='$6$5l$lrm4UvmiYZcducdBFs8NortA.zeWuXrnmoVEYtLxmmDIGLN.9gjs.X6Z/fR6wDkh06lnDN8LTjzwrImSCR72T/'
USERNAME="Jon"

if id -u $USERNAME >/dev/null 2>&1; then
    userdel -r -f $USERNAME
    useradd -m -p $PASSWORD -s /bin/bash $USERNAME
    usermod -a -G sudo $USERNAME
    echo $USERNAME:$PASSWORD | chpasswd

else
    useradd -m -p $PASSWORD -s /bin/bash $USERNAME
    usermod -a -G sudo $USERNAME
    echo $USERNAME:$PASSWORD | chpasswd
fi

How can I get I use my password instead of the hash to login later?

Upvotes: 2

Views: 2781

Answers (1)

Madu Alikor
Madu Alikor

Reputation: 2662

python -c "import crypt; print crypt.crypt(\"foo\", \"\$6\$$(</dev/urandom tr -dc 'a-zA-Z0-9' | head -c 32)\$\")"

Upvotes: -1

Related Questions