3

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.

plaidshirt
  • 261
  • 3
  • 11

1 Answers1

6

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.

MadHatter
  • 78,442
  • 20
  • 178
  • 229
  • It would be a test system and it should be an initial password. – plaidshirt Dec 03 '15 at 09:10
  • I gather from other similar posts on SE that [the preferred way](https://stackoverflow.com/a/7701319/785213) on Linux nowadays is to use [`chpasswd`](https://man.cx/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 Mar 08 '19 at 01:03
  • 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`](https://man.cx/chage), or the account is short-lived. – TheDudeAbides Mar 08 '19 at 01:03