Creating User Accounts on a Linux System Using Bash Scripting: A Guide to Automating User Management for Efficiency and Scalability

George Baidoo Jr.
4 min readMar 24, 2024

--

In this project I will be showing you how to create local users on a Linux System using Bash Scripting.

The overall purpose of bash scripting is to help automate any repetitive administrative tasks that help system administrators/engineers save time and focus on tasks that require more time and attention.

Image was created using DALLE3

1.) Create a Virtual Machine

In order to get this project up and running we will be creating a local virtual machine using Vagrant and Virtualbox.

The following link contains the download links to get both Vagrant and Virtualbox installed on your local machine.

  • Be sure to download the files needed based upon your local machine Ex: Mac/Windows/Linux
  • On your local machine’s terminal/command line you will be creating a new directory. This new directory will contain the Vagrant and VM files needed to get the VM setup and up and running.
mkdir shellclass 

Change into the new directory:

cd shellclass
  • While you are “IN” the shellclass. You will create an additional directory called “localusers”. Once you have created the “localusers” directoy, change into it.
mkdir localusers
cd localusers

Initialize the Vagrant project:

vagrant init jasonc/centos7 

2.) Configure the VM:

  • You will edit the “Vagrantfile” using the text editor of your choice Ex: “nano, vim, vi..etc”. The choice is yours. Add the following text below into the script and save and close the file.
config.vm.hostname = "localusers"

3.) Start the VM and Log In:

  • The following command will start the virtual machine and connect to it.
vagrant up
vagrant ssh

4.) Time to Write the Script

  • We will create our text file that will contain the script that will be used to automatically create new users for the linux system.
  • Name the file “add-local-user.sh” and use the text editor of your choice to add the following script to it.
  • The first line of the script will start of with what’s known as the “Shebang”. Shebang denotes the beginning of a script.
  • The script we will be writing will be used to create new user accounts for new employees joining the company that you are working at, so that you can free up your time to focus on important tasks.
  • The script will be executed with superuser privileges.

User account creation:

  • The script when executed properly will prompt the user(help desk personnel) to enter the new user/employee’s info, followed by a unique password that will ultimately led to the successful creation of the user/employee accounts.

Change the permission privileges to grant full administrative access

chmod 755 add-local-user.sh

5.) Let’s Test Things Out!

  • Its time to test out the script. The first user to be created will be named “Albert Einstein”.
  • Feel free to create as many users as you like to get more practice.
Enter the username: einstein
Enter the name: Albert Einstein
Password: E=mc2theory$
Successful creation of one user account

Let’s change the password for our newly created user:

  • Using superuser privileges we are going to change the password for “einstein”.
  • Enter your current password when prompted.
  • Enter the new password when prompted.

Congratulations if you made this far that means that you have successfully completed the project and learned how to create user accounts using bash scripting!

Follow me on:

My LinkedIn: https://www.linkedin.com/in/georgebaidoojr/

My Twitter: https://twitter.com/GeorgeBaidooJr

My GitHub: https://github.com/GeorgeBaidooJr9

--

--

George Baidoo Jr.

Transitioning IT Professional | AI & ML Enthusiast| | AWS Community Builder | 👉🏿 https://www.linkedin.com/in/georgebaidoojr/ |