Python: how to set password in ubuntu user account using Fabric

I am trying to create a user account on a remote Ubuntu system using Fabric. I want the account to have a strong password. To create an account, I can use the following:

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

The problem is that I'm not sure how to set a password. The useradd -p options require an encrypted password. How to set a password? How is salt transferred to a remote system?

Sample code will be appreciated.

thanks

+4
source share
2 answers

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

 sudo('chpasswd < my_password_file') 

From man chpasswd :

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

user_name: password

The passwords provided must be in text form.

+1
source

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 correspond to the username, and the user will be prompted to change his password at the first login.

+1
source

Source: https://habr.com/ru/post/1343897/


All Articles