How to Create local users using script.

Shubham Soni
5 min readNov 21, 2023

--

When we have to create an account for a user in our Linux server then we can run the script and provide the user name, so that it will create a user, its password and tell the hostname of the system.

REQUIREMENT:

The script should be executed with root user else exit with some status 1 and error message.

Script will take 1st argument as user and the rest will be treated as comment.

Auto-generated password for the user.

Upon successful execution of the script, display the following:

Username: <user-name>

Password: <auto-generated-password>

Host: <host-name>

STEPS:

Check if the script is being executed with superuser privileges.

If the user does not provide at least one argument, then give them help.

The first parameter is the user name.

The rest of the parameters are for the comments.

Generate a password.

Create the user with the password.

Check to see if the useradd command succeeded.

Set the password.

Check to see if the passwd command succeeded.

Force password change on the first login.

Display the username, password and host where the user is created.

SCRIPT:

Go to the specific folder where you want to create the script and then create a new file called “user_creation.sh” using vim editor.

$cd /home/Yuvraj/projects/
$vim user_creation.sh

step 1:

After creating a new file, we start with writing the shebang line of the script, then we comment and inform about the version of the script and date of creation of the file.

#!/bin/bash
# Version- 1
# Date of creation of script –

Step 2. Check if the script is being executed with superuser privileges.

#Script should be executed with sudo/root access
if [[ "${UID}" -ne 0 ]]
then
echo "Please run the script with sudo or root user"
exit 1
fi

Here we are confirming whether the script is executed with sudo or root user. For this we are using UID, as we know the UID of the root user is ZERO so we are using that to confirm, if it is not equal to zero then echo “Please run the script with sudo or root user” and exit the script at the same step.

But if it is executed by sudo or root user then execute the next step.

Step 3. If the user does not provide at least one argument, then give them help.

#User should provide at least one argument as username else guide the admin
If [[ "${#}" -lt 1 ]]
then
echo "Usage: ${0} USER_NAME [COMMENT]…"
echo "Create a user with name USER_NAME and comments field of COMMENT "
exit 1
fi

In this step, we are using the if-condition to check whether the admin is providing any argument with the execution of the script or not.

${#} means the length of the argument, if it is less than 1, then echo “Usage: ${0} USER_NAME [COMMENT]…” to help the admin how to give the argument while running the script.

After that echo “Create a user with name USER_NAME and comments field of COMMENT ”

If the condition is true then exit the script at the same step and end the if condition with fi.

Step 4. The first parameter is the user name.

#Store the 1st argument as the user name
USER_NAME="${1}"
echo $USER_NAME

here in this step, we are storing the 1st argument provided by the admin to a variable called USER_NAME and to verify it we can echo the USER_NAME.

Step 5. The rest of the parameters are for the comments.

#In case of more than one argument, store it as account comments
shift
COMMENT="${@}"
echo $COMMENT

In this step, if there is more than one argument provided by the admin then we are storing all the input to the other variable called COMMENT. For this, we are using “shift” so that the first output is stored in the USER_NAME and all other data is stored in the COMMENT variable.

To verify we can check using the echo command.

Step 6. Generate a password.

#create a password
PASSWORD=$(date +%s%N)
echo PASSWORD

In this step, we are generating a password for this we can use either $RANDOM or the above syntax.

Random is not used because it always prints a random number from 0 to 32757, it may be 2 digit, 3 digit, 4 digit or 5 digit, that’s why we are not using that.

$(date +%s%N) means we are printing the date in a different way as nano-sec type so that the output is a long and fixed number of letters in the password.

We can store this output in the PASSWORD variable so that we can use it in the script later.

Step 7. Create the user with the password.

#Create the user
useradd -c "$COMMENT" -m $USER_NAME

This means we are creating a user named USER_NAME and comment as COMMENT. Here -m means we are also creating the home directory for the user.

Step 8. Check to see if the useradd command succeeded.

# Check if the user is successfully created or not
if [[ $? -ne 0 ]]
then
echo "The Account could not be created"
exit 1
fi

here we are using if-condition, $? is used to check whether the last step is successful (0) or not. If not successful then echo “The Account could not be created” and exit the script and write fi to end the if-condition.

Step 9. Set the password.

#Set the password for the user
echo -e "$PASSWORD\n$PASSWORD" |passwd "$USER_NAME"

here we are creating a password for the user.

Step 10. Check to see if the passwd command succeeded.

#Check if the password is successfully created or not
if [[ $? -ne 0 ]]
then
echo "Password could not be set"
exit 1
fi

Step 11. Force password change on the first login.

#Force password change on its first login
passwd -e $USER_NAME

in this step, we are using passwd with -e means the user must have changed the password on their 1st login.

Step 12. Display the username, password and host where the user is created.

# Display the username, password and host where the user is created.
echo
echo "Username: $USER_NAME"
echo
echo "Password: $PASSWORD"
echo
echo "Hostname: $(hostname)"

Here, the scripting is completed and to save the file and exit the editor press ESC and the write

:wq

the script.

There are the following ways to execute a script, which are as below:

1. use chmod +x user_creation.sh to provide the execute permission of the file to all and then execute the script using user_creation.sh

2. Direct execute using bash user_creation.sh

You can also visit my Github repository for the complete script:

Hope you like my work and got something new. Thank You!!!

--

--

No responses yet