passwd命令抱怨用户不存在

时间:2016-02-20 22:12:55

标签: linux bash scripting

这是我的剧本:

#!/bin/bash

for i in $(cat usernames.txt)

do
    echo $i
    useradd $i -m -s /bin/bash $i -G groupy $i
    passwd=echo "password" | passwd $i
    passwd -e $i
    echo "User must change password when they come back"

done

它应该做的是:

  • 使用主目录中文件中给出的名称创建用户
  • 他们的密码是默认的“密码”,他们必须在下次登录时更改
  • 将它们放入已创建的名为“groupy”的组中

这只是我们刚读完章节的大部分时间的练习。我们的老师希望我们尝试这一点,但我们阅读的章节与这种脚本构建无关。我真的不确定我做错了什么。任何人都可以指出我做错了什么和/或指出我正确的方向吗?

我继续将其作为输出:

passwd: user 'wworthington' does not exist
./myscript: line 8: password: command not found
passwd: Permission denied.
User must change password when they come back

2 个答案:

答案 0 :(得分:2)

看起来您最直接的问题是权限。用户' wworthington'没有创建,这意味着useradd不起作用。大多数系统要求您以超级用户身份运行,方法是以root登录或使用sudo命令运行useraddpasswd等管理命令(请参阅{ {3}}了解更多信息)。 passwd可以由普通用户为自己的帐户运行,也可以由超级用户为任何用户帐户运行(请参阅intro to sudo)。

此外,passwd=echo "password" | passwd $i将名为passwd的变量设置为echo,然后运行不存在的命令"password"将(空)输出传递给passwd $i 。您可以将用户$i的密码设置为"密码"并使用useradd-p选项分别在-e命令中设置到期日期(请参阅passwd man page)。这两个选项都需要选项标记后面的值(就像您使用-G groupy-s /bin/bash一样)。

答案 1 :(得分:0)

[root@bogon test]# useradd --help
Usage: useradd [options] LOGIN
   useradd -D
   useradd -D [options]

Options:
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                            new account
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       home directory of the new account
  -D, --defaults                print or change default useradd     
configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new      
 account
  -g, --gid GROUP               name or ID of the primary group of the new
                            account
  -G, --groups GROUPS           list of supplementary groups of the new
                            account
  -h, --help                    display this help message and exit
  -k, --skel SKEL_DIR           use this alternative skeleton directory
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -l, --no-log-init             do not add the user to the lastlog and
                            faillog databases
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -N, --no-user-group           do not create a group with the same name as
                            the user
  -o, --non-unique              allow to create users with duplicate
                            (non-unique) UID
  -p, --password PASSWORD       encrypted password of the new account
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
  -s, --shell SHELL             login shell of the new account
  -u, --uid UID                 user ID of the new account
  -U, --user-group              create a group with the same name as the user

-Z, - serinux-user SEUSER使用特定的SEUSER进行SELinux用户映射

你的useradd有太多的参数,只是简单地说: useradd -m -s / bin / bash -G groupy $ i

passwd = echo"密码" | passwd $ i
你想传递密码' passwd? 正确的方法是:echo "password" | passwd --stdin $i

最后,工作脚本:

#!/bin/bash

for $ in $(cat usernames.txt)

DO     echo" useradd -m -s / bin / bash -G groupy $ i"     useradd -m -s / bin / bash -G groupy $ i     echo"密码" | passwd --stdin $ i     passwd -e $ i     echo"用户返回时必须更改密码"

完成

相关问题