20

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 ?

1

9 Answers 9

27

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)
2
  • 1
    openssl passwd expects a password of 0 to 10 characters.
    – Onnonymous
    Dec 10, 2019 at 15:00
  • 3
    One should use openssl passwd -1, otherwise openssl will default to -crypt and truncate the password at 8 chars with a "Warning: truncating password to 8 characters" message. Oct 8, 2020 at 18:20
11

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!

1
  • Why isn't setting a password this way recommended? What are the concerns with it?
    – TerekC
    Sep 6, 2021 at 14:45
9

You may use passwd:

sudo passwd USERNAME 

You need not sudo if you're superuser yourself

3
  • sudo require root password :)
    – Achu
    May 13, 2012 at 18:41
  • @Achu: it's required current user password. But he is superuser.
    – RiaD
    May 13, 2012 at 19:31
  • What does superuser represent?
    – Achu
    May 13, 2012 at 19:37
4

The way to assign a password with usermod (which is what the OP actually asked for) is to use a crypt() hashed password for the -p argument.

SALT="Q9"
PLAINTEXT="secret_password"
HASH=$(perl -e "print crypt(${PLAINTEXT},${SALT})")
echo "Password Hash = \"${HASH}\""

Then use that in your usermod -p command line argument:

usermod -p ${HASH} john
4

A non-interactive single line command for changing the password of a user:

sudo usermod -p `perl -e "print crypt("new-password","Q4")"` john

usermod -p requires encrypted password to work. Please note the new-password will be visible for users who can list the processes.

1
  • Use single-quotes inside the crypt command (thus: double quotes). If you do that, it works.
    – Frank N
    Aug 22, 2020 at 6:02
2

Something that should be added here is the following. This method:

sudo usermod -p `perl -e "print crypt("new-password","Q4")"` john

means that multiple very similar passwords will ALL work. For example, on Oracle Linux 7.4 server and Ubuntu 17.10 desktop consider:

sudo usermod -p `perl -e "print crypt("borkling","Q4")"` orabuntu

Now if one does:

su - orabuntu

you will find that ANY password that starts with borkling will work, e.g.

borkling88
borklingjars

although borkline will not work which is because as stated previously any password that has "borkling" as it's prefix will also work when the password is set in this way.

A way to to this that afaik does not have this unwanted side-effect is the following:

(credit for these goes to "Sandeep" here:
https://stackoverflow.com/questions/2150882/how-to-automatically-add-user-account-and-password-with-a-bash-script)

On RedHaty Linuxes: (omit the "-G wheel" if you don't want sudo privs granted)

sudo useradd -m -p $(openssl passwd -1 ${PASSWORD}) -s /bin/bash -G wheel ${USERNAME}

On Debiany Linuxes (omit the -G sudo if you don't want sudo privs):

sudo useradd -m -p $(openssl passwd -1 ${PASSWORD}) -s /bin/bash -G sudo ${USERNAME}
1

Just type

passwd

In this way normal user can change own password without root privilege if you don't have.

0

We can use commands mkpasswd and usermod. This is simple and reliable.

  • In order to have the command mkpasswd:

    apt install whois
    
  • mkpasswd -m sha-512 your-secret-password
    
  • with the result from mkpasswd, you can launch usermod like this

    usermod -p 'the-result-from-mkpasswd' root
    

Check the screenshot from my test Demo on password change

0
#! /bin/bash
USER=$1
PASS=$2
usermod --password $(echo $PASS | openssl passwd -1 -stdin) $USER
1
  • 1
    Please provide some explanation on how to use this.
    – Paul
    Nov 14, 2021 at 2:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .