Useradd with given password

6,661

Read the man page. The argument to -p is not the plaintext password:

-p, --password PASSWORD

The encrypted password, as returned by crypt(3). The default is to disable the password.

and it would be an extremely dangerous command if it were. There is no easy way to do what you want, because what you want is dangerous.

Edit: I understand that you feel your circumstances justify this loss of security, and you might be right: but there is no way for the passwd command to know what you're thinking.

If you're sure you don't mind this loss of security, you could do

echo fr00zalgn3t | passwd --stdin user

to set the initial password to the echoed string, but if you do this, you should also do

chage -d 0 user

immediately after, to require that the password be changed on first login. You should also ensure that your shell history is cleared after doing this, so the passwords used aren't sitting in a permanent record on-disc. And you should not, ever, use the username as an initial password, no matter how low-end you feel the system is.

Share:
6,661

Related videos on Youtube

plaidshirt
Author by

plaidshirt

Updated on September 18, 2022

Comments

  • plaidshirt
    plaidshirt almost 2 years

    I would like to add new user without needed any interaction, so with specified password. I have tried command above:

    useradd -m -p user -s /bin/bash user
    

    So it should be a user with password user, but I can't login via SSH.

  • TheDudeAbides
    TheDudeAbides over 5 years
    I gather from other similar posts on SE that the preferred way on Linux nowadays is to use chpasswd. You may see how-tos on the web suggesting openssl passwd -crypt, or variations of that, but there are too many combinations of hashes and salts that will probably depend on your particular distro. Save yourself the headache.
  • TheDudeAbides
    TheDudeAbides over 5 years
    Additionally, security is always a compromise. I personally feel perfectly fine setting passwords in this way for private / internal / test systems, provided they're good (random) initial passwords, the user is forced to change the password upon first login (as you mention, with chage, or the account is short-lived.