Shell script creates linux user account but password goes wrong Shell script creates linux user account but password goes wrong shell shell

Shell script creates linux user account but password goes wrong


So your current method is adding plaintext passwords to /etc/shadow.

Using your script as is... Created this. Mind you, I added a few digits at the end of your the password to match the standard 8 characters to suppress the "are you sure" prompts.

user1:abcXYZ123:16963:0:99999:7:::user2:DEFxyz142:16963:0:99999:7:::user3:ijkLM1564:16963:0:99999:7:::

The users files

[root@localhost ~]# cat t.txt s.txtuser1 abcXYZ123user2 DEFxyz142user3 ijkLM1564user4 abcXYZ123user5 DEFxyz142user6 ijkLM1564

The Script

#!/bin/bash -e#Adding user1, user2, user3 using your method.file_name="t.txt"while read user passdo    #useradd ${user} -p ${pass}    useradd -p ${pass} ${user}    echo "Adding user "${user}"  with the password  "${pass}done < $file_name#Adding user4, user5, user6 using the recommended method.file_name="s.txt"while read user passdo    #useradd ${user} -p ${pass}    useradd ${user}    echo "${pass}" | passwd --stdin ${user}    echo "Adding user "${user}"  with the password  "${pass}done < $file_name

Using both syntaxes, produced these results in /etc/shadow

user1:abcXYZ123:16963:0:99999:7:::user2:DEFxyz142:16963:0:99999:7:::user3:ijkLM1564:16963:0:99999:7:::user4:$1$NpazYQAn$tlhfQLlP0CaFiUeNeK8HW.:16963:0:99999:7:::user5:$1$4z8G4gvh$v0jzcV5xbhWixU1LG9mwW.:16963:0:99999:7:::user6:$1$cBkcYJkJ$7A.j6E3gy/umUcVmY0tgt0:16963:0:99999:7:::

So, therefor the method I posted works. Update: Ubuntu working method below. This the stdin is confirmed for Rhel based systems.

useradd ${user}echo "${pass}" | passwd --stdin ${user}

If your heart is absolutely set on using that method, I would suggest installing mkpasswd and using this syntax.

 useradd -p $(mkpasswd ${pass}) ${user}

Update for Ubuntu Passwd stdin

echo ${user}:${pass} | /usr/sbin/chpasswd