1

I have tried to hash my password with perl when adding a user using a script but when I try to switch to that user, it says the password is incorrect. Here is what I wrote:

hidden=$(perl -e 'print crypt($pass, "salt"),"\n")
useradd -m -p $hidden -d /home/$user -s /bin/bash $user 

And above this I attributed "pass" my new password and "user" my new username.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Anda
  • 670
  • 7
  • 7
  • 1
    Please note that `crypt` hashes a password and does not encrypt it. You cannot reverse this operation but you can verify the password by running the same operation again and comparing it to the previously generated value. – Artjom B. Nov 21 '20 at 08:22
  • I am confused. If i simply try to login using the command "su user" and then I enter my password shouldn't it automatically do this verification? – Anda Nov 21 '20 at 08:32
  • Yes, it does. It automatically looks up the stored hash value and hashes the password with the proper procedure and compares the values. If you use other utilities that might not be the case and you'd have to do the verification yourself. – Artjom B. Nov 21 '20 at 08:39
  • 1
    Your perl command is missing an ending single quote `'`. – TLP Nov 21 '20 at 11:19
  • See [How can I process options using Perl in -n or -p mode?](https://stackoverflow.com/q/53524699/589924) [All the options also work even when `-n`/`-p` isn't used] – ikegami Nov 22 '20 at 08:42
  • @Artjom B., Yeah, but what's your point. `useradd` requires a hashed password. Please delete your off-topic comments. – ikegami Nov 22 '20 at 08:43

1 Answers1

1

You seem to want to use $pass from the shell environment, but inside the Perl interpreter it looks like the Perl variable $perl.

Try

hidden=$(perl -e 'print crypt($ENV{pass}, "salt")')

to refer to the pass environment variable instead.

mob
  • 117,087
  • 18
  • 149
  • 283