A.LAND
A.LAND

Reputation: 1

passwd command complains about user not existing

Here is my script:

#!/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

What it is supposed to be doing is:

This is just practice for a class where the majority of the time we just read chapters. Our teacher wants us to attempt this but the chapter we read has nothing to do with this kind of script building. I really am unsure as to what I am doing wrong. Can anyone point out what I am doing wrong and/or point me in the right direction?

I continue to get this as output:

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

Upvotes: 0

Views: 2412

Answers (2)

chenyb999
chenyb999

Reputation: 139

[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, --selinux-user SEUSER use a specific SEUSER for the SELinux user mapping

your useradd has too many param, just simply: useradd -m -s /bin/bash -G groupy $i

passwd=echo "password" | passwd $i
are you want to pass 'password' to passwd? the right way is : echo "password" | passwd --stdin $i

finally,the working script:

#!/bin/bash

for i in $(cat usernames.txt)

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

done

Upvotes: 0

eclark
eclark

Reputation: 491

It looks like your most immediate problem is with permissions. User 'wworthington' was not created, which means that useradd did not work. Most systems require you to run as a super-user, either by logging in as root or using the sudo command to run administrative commands like useradd and passwd (see intro to sudo for more info). passwd can be run by a normal user for his/her own account or by the super-user for any user account (see passwd man page).

Also, passwd=echo "password" | passwd $i sets a variable called passwd to echo and then runs a non-existent command "password" piping the (empty) output to passwd $i. You can set user $i's password to "password" and set the expiration date in your useradd command with the-p and -e options, respectively (see useradd man page). Both options require a value after the option tag (like you did with -G groupy and -s /bin/bash).

Upvotes: 2

Related Questions