0
OS Ubuntu 18.04

I am trying to add a user, with password, home directory, create a group with the same name, using a bash script.

I tried:

sudo useradd -U -m -p $(openssl passwd -1 'some_password') test

The user was created, here's the entry from /etc/passwd:

test:x:1004:1004::/home/test:/bin/sh

The home directory was created:

/home/test

and the group was created.

The problem, is that when I try doing:

su test

It asks me for a password, but when I enter the password, I get the following error:

su: Authentication failure

PS: I realize the dangers of adding passwords to bash scripts, but this is not what I am doing. The script asks the user to enter the password, and uses it in the script like this:

sudo useradd -U -m -p $(openssl passwd -1 '$password') $username

Any ideas?

On the same system, I tried using useradd, to add a test2 user, and entered the password. I was able to su with test2

For diagnostics purposes, I created a bash script doit.sh and had in it:

passwd='SomePassword1234'
username='test2'
sudo useradd -U -m -p $(openssl passwd -1 '$password') $username

I ran it as:

bash -x doit.sh

Here's the output:

ubuntu@myserver:~$ bash -x doit.sh
+ passwd=SomePassword1234
+ username=test2
++ openssl passwd -1 '$password'
+ sudo useradd -U -m -p '$1$okAtvbfD$hVwD0AoMWR5AiTw.9Bztf.' test2

and I try to su:

ubuntu@myserver:~$ su test2
Password: 
su: Authentication failure
ubuntu@myserver:~$ 
EastsideDev
  • 301
  • 3
  • 13

1 Answers1

0

It's the quoting, '$password' will be literally that: $password and not the value of the $password variable. "$password" is required here.

sudo useradd -U -m -p $(openssl passwd -1 "$password") $username
suspectus
  • 548
  • 1
  • 4
  • 11