Linux User Creation and Password Update with Simple Command Chaining

I needed to create a user, grant them super user sudo privileges, update user account passwords and expire them across a number of different servers so that the actual user could log in and be forced to change their password to a (hopefully) secure one after I was complete.  Initially, I thought this would be pretty straight forward by using the useradd command options, this however, failed.  In addition, my attempt to use usermod to set the password also failed.  I did not have time to investigate and troubleshoot the cause of this but like many things in Linux, options abound.

I then went to the never-under-rated-always-useful passwd command with a handy switch of –stdin which allows a password to be injected into the command line.  Below are the separate commands used.  Oh, it should also be noted that this worked for RHEL but milage may vary on other flavors as I've not specifically tested this on anything else.

useradd username
usermod -a -G wheel username
echo PaSSwOrD | passwd --stdin username
chage -d 0 username

Now, each of these commands can be strung together by simply adding a semi-colon between them for the following result.

useradd username;usermod -a -G wheel username;echo PaSSwOrD | passwd --stdin username;chage -d 0 username

If you happen to have the time, and are so inclined, you could modify this into an executable script to apply to multiple users in a given instance using a simple for statement or even expand it further to do validation and only add privileges after choosing that option.  Those are just a few ideas that could be used to expand upon this need.

Torry Crass

Leave a Comment