bash - Shell script creates linux user account but password goes wrong -
i've written shell script create user accounts. script reads user account name , password text file , create account info. when execute script creates accounts, when try log in accounts can't log in due invalid password, please try again
error.
here script used create user accounts:
file_name="t.txt" while read user pass useradd -p ${pass} ${user} done < $file_name
edit-1: t.txt
file contains user account information: space separated username , password per line. here snippet of file:
user1 abcxyz user2 defxyz user3 ijklmn
edit-2: when follow method recommended steve kline shows me following result: (still created accounts can't logged in given password)
passwd: unrecognized option '--stdin' usage: passwd [options] [login] options: -a, --all report password status on accounts <---------------------------------- skipped -------------------------------> -x, --maxdays max_days set maximum number of days before password change max_days adding user user1 password abcxyz123
still created accounts can't logged in given password
so current method adding plaintext passwords /etc/shadow.
using script is... created this. mind you, added few digits @ end of password match standard 8 characters suppress "are 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.txt user1 abcxyz123 user2 defxyz142 user3 ijklm1564 user4 abcxyz123 user5 defxyz142 user6 ijklm1564
the script
#!/bin/bash -e #adding user1, user2, user3 using method. file_name="t.txt" while read user pass #useradd ${user} -p ${pass} useradd -p ${pass} ${user} echo "adding user "${user}" password "${pass} done < $file_name #adding user4, user5, user6 using recommended method. file_name="s.txt" while read user pass #useradd ${user} -p ${pass} useradd ${user} echo "${pass}" | passwd --stdin ${user} echo "adding user "${user}" 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 method posted works. update: ubuntu working method below. stdin confirmed rhel based systems.
useradd ${user} echo "${pass}" | passwd --stdin ${user}
if heart absolutely set on using method, suggest installing mkpasswd , using syntax.
useradd -p $(mkpasswd ${pass}) ${user}
update ubuntu passwd stdin
echo ${user}:${pass} | /usr/sbin/chpasswd
Comments
Post a Comment