1

I've been trying to set up a new user on my Arch Linux system by way of a simple shell script, following the advice of several sources which state that printf "$password\n$password\n" | (passwd $user), or similarly echo -e "$password\n$password\n" | (passwd $user) should do the trick.

In doing so, however, while it does succeed and report that the password has been updated, trying to log in via ssh with that user doesn't work for some reason. Does anyone know why, and how to fix it?

I have tried removing the variables, and simply make the password and user static text as well. The exact same lines of commands; printf "123abcXYZ\n123abcXYZ\n" | (passwd dummy) works in the shell and I can log in via ssh, but take the same line and put it in a script and it will say the password is wrong.

Any ideas?

Casper B. Hansen
  • 176
  • 2
  • 10
  • Curious. Your password changer works fine when I run it as a bash script. – Hannu Mar 08 '18 at 09:36
  • 1
    Parentheses around `passwd $user` are not needed but still your commands should work. Have you tried [chpasswd](https://stackoverflow.com/questions/27837674/changing-a-linux-password-via-script)? – builder-7000 Mar 08 '18 at 09:53
  • 1
    Also printing the values of `$user` and `$password `in your script may give a hint of the problem. Note that if `$user` is not defined in your script then `(passwd $user)` will become `(passwd)` so that you will be changing your own password, not that of the new user. – builder-7000 Mar 08 '18 at 10:25
  • @Hannu Very puzzling indeed! – Casper B. Hansen Mar 08 '18 at 13:52
  • @Sergio I haven't tried chpasswd yet — but shouldn't passwd be run before chpasswd works? Perhaps this isn't necessary? I'll try when I get home. I've printed both values, and both look just fine with a simple echo. – Casper B. Hansen Mar 08 '18 at 13:53

2 Answers2

1

It may be because password contains characters like \ or % which can be interpreted by printf, maybe following can work :

printf '%s\n%s\n' "$password" "$password"

which can also be written (because of printf implicit loop)

printf '%s\n' "$password" "$password"
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
0

Rejoice — I've found the error!

It had absolutely nothing to do with the passwd command or the characters. Rather, it was a mere mistake of evaluating the variable for the group for which the user was to be assigned — my bad! Thank you for your answers though :)

Casper B. Hansen
  • 176
  • 2
  • 10