gaviscon_man
gaviscon_man

Reputation: 993

Python: How do I set the password on an ubuntu user account using Fabric

I'm trying to create a user account on a remote Ubuntu system using Fabric. I want the account to have a strong password. I can use the following to create the account:

 sudo('useradd -m test -s /bin/bash')

The problem is I'm not sure how to set the password. The useradd -p options requires an encrypted password. How do I set the password? How does the salt get passed to the remote system?

Example code would be appreciated.

Thanks

Upvotes: 4

Views: 1573

Answers (2)

grenade
grenade

Reputation: 32169

I do it like this:

sudo('useradd %s' % username)
sudo('echo %s | passwd %s --stdin' % (username, username))
sudo('chage -d 0 %s' % username)

The password will match the username and the user will be prompted to change their password at first login.

Upvotes: 1

jd.
jd.

Reputation: 10958

Try chpasswd. Unlike passwd, you can pass the username and password in the command's standard input. So you can for example upload a file containing the password and put this line in your fabfile:

sudo('chpasswd < my_password_file')

From man chpasswd:

The chpasswd command reads a list of user name and password pairs from standard input and uses this information to update a group of existing users. Each line is of the format:

user_name:password

The supplied passwords must be in clear-text.

Upvotes: 1

Related Questions