redhat add multiple users

September 13, 2012

useradd script

to add multiple users to a system and force them to change their password upon login try the following script:

Redhat

# for name in someuser anotheruser yetanotheruser; do useradd $name; echo 'password' | passwd --stdin $name; chage -d 0 $name; done

The above script will add three users to the system (someuser anotheruser yetanotheruser) with the password “password” and set the password age to zero. This forces them to change their password upon login.

Ubuntu

apt-get -y install makepasswd
for name in user1 user2 user3; do useradd -m -s /bin/bash -G sudo,admin -p `mkpasswd changethis` $name; chage -d 0 $name; done

the above script will add users user1 user2 user3 to the server, place them in the admin group, set their password to “changethis”, and set the password age to zero (forcing them to change their password on login)

Check if users exist script

To check if the users exists in the system try this command:

# for name in someuser anotheruser yetanotheruser; do if id -u "$name" > /dev/null 2 > ; then echo "user $name exists"; else echo "user $name does not exist"; fi; done

user someuser exists
user anotheruser exists
user yetanotheruser exists