I've written a shell script to create user accounts. 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.

我编写了一个shell脚本来创建用户帐户。该脚本从文本文件中读取用户帐户名和密码,并使用这些信息创建帐户。当我执行脚本时,它成功创建了帐户,但是当我尝试登录这些帐户时由于密码无效而无法登录,请再次尝试错误。

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. Here is the snippet of the file:

EDIT-1:t.txt文件包含用户帐户信息:空格分隔的每行用户名和密码。这是文件的片段:

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

仍然无法使用给定密码登录创建的帐户

1 个解决方案

#1


3

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:::

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 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. This the stdin is confirmed for Rhel based systems.

因此,我发布的方法是有效的。更新:Ubuntu工作方法如下。这个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

更多相关文章

  1. linux忘记root密码的两种修改方法
  2. ubuntu下的Samba配置:使每个用户可以用自己的用户名和密码登录自
  3. 在centos中安装了mysql5.7之后解决不知道随机的密码的问题
  4. ubuntu修改根用户密码
  5. 解决useradd 用户后没有添加用户Home目录的情况,Linux改变文件或
  6. mysql数据库忘记ROOT密码时的解决办法
  7. mysql5.6和5.7的权限密码设置
  8. Mysql5.7.10版本安装后空密码登录,退出后提示密码错误连接不上解
  9. MySQL忘记密码破解密码的方法

随机推荐

  1. (亲测有效)Android SDK Manager国内无法
  2. 【android基础】之Android返回键处理(事
  3. Android之XUtils的框架总结
  4. eclipse下android的sdk配置问题
  5. 我是如何自学Android,资料分享(2015 版)
  6. Android中的Shape美化
  7. Android(安卓)TextView文字横向自动滚动(
  8. Android 技术专题系列之三 -- 编译(build)
  9. Android 相对布局:RelativeLayout
  10. Android分区解释:boot, system, recovery,