Home > Bash, Scripting > Linux create users with a script

Linux create users with a script

November 23rd, 2010 Leave a comment Go to comments

terminal Linux create users with a script

Creating users in bulk with a bash script is pretty simple and on the internet you can find a well worth number of examples and scripts that can help you easily accomplish this, there are times when you need to just create one or two users while still being able to configure parameters like password or expiration date.

This is the case for one of my machines where we create local account to allow people FTP access to the server, as you can imagine I don’t need to create a large number of accounts daily and most important of it all I need to set an expiration date for the newly created account. While this can be easily accomplished with a few commands I’ve create a small script to take care of this.

The script is a pretty simple one written in a rush so feel free to modify it or to enhance it but it does its job, it will ask you for the username to be created, a password and the expiration date giving errors if the username already exists.

Here’s the source code of the script :

#!/bin/bash
# A very simple script used to create users on a CentOS machine interactively
# It is assumed that you will be using the root account for this task
# But you can comment the line with the check if you are using sudo
#
# -------------------------------------------------------------------------
# Copyright (c) 2010 Lethe's Tuxforge <http://blog.tuxforge.com>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------</pre>
if [ $(id -u) -eq 0 ]; then                             #We check if we have root privileges
 read -p "Enter username : " username

 egrep "^$username" /etc/passwd >/dev/null
 if [ $? -eq 0 ]; then
 echo "$username already exists! Please choose another username."        #Exit if user already exists
 exit 1
 else
 read -s -p "Enter password : " password
 echo  "Enter the  expiration date in the format YYYY/MM/DD : "
 read  expiration
 pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
 useradd -m -s /bin/bash -e $expiration -g ftpusers -d /home/$username  -p $pass $username
 [ $? -eq 0 ] && echo "User has been added to system!" && chage -l $username || echo "Failed to add a user!" #We create the user and print on screen expiration parameters
 fi
else
 echo "You need to be root or use sudo to add users to the system!"
 exit 2
fi 

The script has been created for a RHEL/CentOS machine but with little modification it will work for any distribution you are using.

I hope you will find it useful as well and please feel free to enhance/customize it to your needs just remember to share any enhancement you will write for it so that everyone can benefit of it.

Cheers Lethe.

VN:F [1.9.20_1166]
Rating: 5.0/5 (1 vote cast)
Linux create users with a script, 5.0 out of 5 based on 1 rating

Incoming search terms:

Categories: Bash, Scripting