Time-Saving Tips: How to Create Users with Shell Scripts in Linux - A Mini Project
In today's world, automation is essential in any IT organization. Creating users is one of the routine tasks performed by system administrators, and it can be time-consuming, especially if you're doing it manually. You can save time by doing this repetitive task using shell scripts.
In this mini-project, we will explore how to create a user account for a new team member using a shell script. We will also assign the user an auto-generated password, specific group/department, and sudo privileges and send the user his first login credentials.
Pre-requisites:
Knowledge of user and group management.
Knowledge of variables, and conditional expressions in shell scripting.
'SMTP' service configured on your system.
An email account from any service provider such as Gmail, Yahoo, or Outlook to use an external SMTP server.
So, Let's Go!
The scenario is that you have to add a newly joined team member to one of the department groups like DevOps, Developers, and Testers.
First, all three groups must exist and possess separate working directories under our filesystem.
sudo mkdir /home/DevOps /home/Developers /home/Testers
sudo chmod g+s /home/DevOps /home/Developers /home/Testers
And here's our shell script goes..
Write your shell script--->
vim new_user_creation.sh
Make the script executable and execute the script--->
chmod 700 new_user_creation.sh
./new_user_creation.sh
Check whether the user is created or not.
cat /etc/passwd | tail -5
- Check new member has received login credentials via mail.
- Now let's login using the username and password received in the mail.
Congratulations, You have successfully created a user, assigned a password and group, and provided the user with its login credentials.
Summary:
In this article, we learned how to create a user account for a new team member using a shell script. We started by creating separate working directories for the three department groups- DevOps, Developers, and Testers. Then we wrote a shell script that prompts the user for the new username, assigns the user to one of the department groups, generates a random password, grants sudo privileges if required, sends an email to the user with their login credentials, and displays a message about the new user's creation.
Finally, we tested the script and verified whether the user was created successfully and received their login credentials via email. This script can help system administrators save time by automating the repetitive task of creating user accounts.
Stay tuned for my next blog on "Git and GitHub". I will keep sharing my learnings and knowledge here with you.
Let's learn together! I appreciate any comments or suggestions you may have to improve my Linux blog.
Thank you,
Chaitannyaa Gaikwad