How to change password using usermod?

I am superadmin of a server and like to change password of an existing user . How can I do that ?

I tried

usermod -p 'new-password' john

but it didn't worked ?


Solution 1:

The usermod -p flag is expecting the data to be the password already in an encrypted format.

Use openssl passwd to generate the encrypted data, or do it like this:

usermod -p `openssl passwd` (USERNAME)

Solution 2:

The reason it didn't work is because usermod's -p option expects the password to be encrypted already.

From usermod's man page:

 -p, --password PASSWORD
       The encrypted password, as returned by crypt(3).

To set a password in this way is not recommended.

Instead You should use passwd <username>. This should (as usermod) be done as root (if you're not changing the currently logged in users password).

To change the password for user foo.

sudo passwd foo

This will prompt for a new password.

Have a look at the man-page for passwd for more info on setting for example expire time.

Good Luck!