[英]Shell script creates linux user account but password goes wrong
I've written a shell script to create user accounts. 我编写了一个shell脚本来创建用户帐户。 The script reads user account name and password from a text file and create account with those info. 该脚本从文本文件中读取用户帐户名和密码,并使用这些信息创建帐户。 When I execute the script it successfully creates the accounts, but when I try to log in to those accounts I can't log in due to Invalid password, please try again
error. 当我执行脚本时,它成功创建了帐户,但是当我尝试登录这些帐户时由于Invalid password, please try again
而无法登录Invalid password, please try again
错误。
Here is the script I used to create the user accounts: 这是我用来创建用户帐户的脚本:
file_name="t.txt"
while read user pass
do
useradd -p ${pass} ${user}
done < $file_name
EDIT-1: t.txt
file contains the user account information: space separated username and password per line. EDIT-1: t.txt
文件包含用户帐户信息:每行空格分隔的用户名和密码。 Here is the snippet of the file: 这是文件的片段:
user1 abcXYZ
user2 DEFxyz
user3 ijkLMN
EDIT-2: When I follow the method recommended by Steve Kline it shows me following result: (still the created accounts can't be logged in with the given password) 编辑2:当我按照史蒂夫克莱恩推荐的方法时,它显示以下结果:(仍然无法使用给定的密码登录创建的帐户)
passwd: unrecognized option '--stdin'
Usage: passwd [options] [LOGIN]
Options:
-a, --all report password status on all accounts
<---------------------------------- SKIPPED ------------------------------->
-x, --maxdays MAX_DAYS set maximum number of days before password
change to MAX_DAYS
Adding user user1 with the password abcXYZ123
Still the created accounts can't be logged in with the given password 仍然无法使用给定密码登录创建的帐户
So your current method is adding plaintext passwords to /etc/shadow. 所以你当前的方法是在/ 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. 请注意,我在密码末尾添加了几位数字以匹配标准的8个字符,以抑制“你确定”的提示。
user1:abcXYZ123:16963:0:99999:7:::
user2:DEFxyz142:16963:0:99999:7:::
user3:ijkLM1564:16963:0:99999:7:::
[root@localhost ~]# cat t.txt s.txt
user1 abcXYZ123
user2 DEFxyz142
user3 ijkLM1564
user4 abcXYZ123
user5 DEFxyz142
user6 ijkLM1564
#!/bin/bash -e
#Adding user1, user2, user3 using your method.
file_name="t.txt"
while read user pass
do
#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 pass
do
#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 使用这两种语法,在/ 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. 更新: Ubuntu工作方法如下。 This the stdin is confirmed for Rhel based systems. 这个stdin被确认用于基于Rhel的系统。
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. 如果您的心脏完全使用该方法,我建议安装mkpasswd并使用此语法。
useradd -p $(mkpasswd ${pass}) ${user}
Update for Ubuntu Passwd stdin 更新Ubuntu Passwd stdin
echo ${user}:${pass} | /usr/sbin/chpasswd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.