Simplify user operation with shell script
Introduction:
In this blog, I have am going to demonstrate the user operation i.e., creation of the user, assigning password to it, adding user to a group, using shell script.
Requirements:
- Access to Linux Machine/Linux machine hosted on virtual box/Linux configured on WSL/AWS EC2 Linux instance
Tasks:
- The user management should be done with root user. Hence at the beginning of the script, I have defined the below condition to check if the script is being executed by root user or any other user. If user is not root, the script will not execute.
if [[ $UID -ne 0 ]]
then
exit 1
2. Next, I have defined the below choices which would be performed by the user.
echo "*************************"
echo "Welcome to user operation"
echo "*************************"
echo "A -- Create a new user"
echo "B -- Assign password to the user"
echo "C -- Add user to a group"
echo "D -- Change primary group of the user"
echo "E -- Delete user"
echo "F -- Create Group"
echo "*************************"
echo "Enter the option to proceed:"
3. In next step, I defined “read” command to read the inputs and defined the case statements.
read vars
case $vars in
4. In next step, I have defined commands for 1st case statement where the script will check if the user which I am trying to create, exists or not. If it exists, then the script will not proceed further. Else, the script will create the user, create its home directory, assign permissions to the home directory and will display the detail of user and its home directory.
A)
echo "******User Creation operation******"
read -p "Enter the user name you want to create: " user
echo "Checking if the user exists"
id $user &> /dev/null
if [[ $? -ne 0 ]]
then
echo "The user doesn't exist. Hence creating the new one"
mkdir /home/$user
useradd -d /home/$user $user
chmod 770 /home/$user
chown $user:$user /home/$user
echo " "
echo "The user is created. Please refer the details below:"
id $user
echo "The detail of home directory is as below:"
ls -ld /home/$user
else
echo "The user $user already exists. Can't create the user again"
fi;;
5. I have defined my next case statement to assign password to user. The script will check if the “user name” for which the password change is requested exists or not. If not, then it will ask requestor to create the user. Else it will proceed further, ask user to enter the password, re-enter it for confirmation. If both passwords are correct, then it will assign/change password for the user.
B)
echo "******Password change operation******"
read -p "Enter the user name to assign password: " user1
id $user1 &> /dev/null
if [[ $? -ne 0 ]]
then
echo "The user $user1 doesn't exist, please create it :("
else
echo "Enter the password: "
read -s pass1
echo "Re-enter the password (If not similar with previous password, the operation will fail: "
read -s pass2
if [[ "$pass1" != "$pass2" ]]
then
echo "The check has failed!!! Hence can't update the password!!!"
else
echo "Assigning password to the user"
echo "$user1:$pass1" | chpasswd 2> /dev/null
if [[ $? -eq 0 ]]
then
echo " "
echo "The password for $user1 is updated"
else
echo " "
echo "Couldn't update the password"
fi
fi
fi;;
6. I have defined my next case statement to add user to a group. Here, the script will check if the user exists or not. If exists, it will ask for group name where the user needs to be added. If the group exists, it will check if the user is already added to the group. If already added, it will not proceed further or else it will add user to the group. If both the user and groups doesn’t exist else it will not proceed further.
C)
echo "******Adding user to a group******"
read -p "Provide the username which needs to be added in the group: " user2
id $user2 &> /dev/null
if [[ $? -eq 0 ]]
then
read -p "Provide the group name: " grp1
cat /etc/group | grep $grp1 &> /dev/null
if [[ $? -eq 0 ]]
then
id $user2 | awk '{print $3}' | grep $grp1 &> /dev/null
if [[ $? -ne 0 ]]
then
echo "Adding the user to the group"
usermod -a -G $grp1 $user2
echo " "
echo "The user is added to the group, please check the details"
id $user2
else
echo " "
echo "The user is already added to group $grp1"
id $user2
fi
else
echo " "
echo "The group $grp1 doesn't exist"
fi
else
echo " "
echo "Invalid user name!! Please recheck the user name"
fi;;
7. I have defined my next case statement to change the primary group of a user. Here, the script will check if the user exists or not. If exists, it will ask for group name which needs to be added as primary group. If the group exists, it will check if the the group is already updated as primary group. If yes, it will not proceed further. Else it will update the primary group of the user. If both the user and groups doesn’t exist else it will not proceed further.
D)
echo "******Modifying the primary group of the user******"
read -p "Enter the user name: " user3
id $user3 &> /dev/null
if [[ $? -eq 0 ]]
then
read -p "Enter the group name which will be assigned as primary group: " grp2
cat /etc/group | grep $grp2 &> /dev/null
if [[ $? -eq 0 ]]
then
id $user3 | awk '{print $2}' | grep $grp2 &> /dev/null
if [[ $? -ne 0 ]]
then
echo "Updating the primary group"
usermod -g $grp2 $user3
echo " "
echo "The primary group is changed. Please check the status below"
id $user3
else
echo " "
echo "The group $grp2 is already the primary group"
id $user3
fi
else
echo " "
echo "The group $grp2 does not exist"
fi
else
echo " "
echo "Invalid user name!! Please recheck the user name"
fi;;
8. I have defined my next case statement to delete a user. It will forst check if the user exists. If yes, it will ask confirmation from requestor to proceed with the operation. If it receives confirmation, it will take backup of user home directory and will delete the user and its primary group.
E)
echo "******User Deletion******"
read -p "Enter the user name to be deleted: " user4
id $user4 &> /dev/null
if [[ $? -ne 0 ]]
then
echo "The username $user4 is invalid!!! Please provide correct details!!!"
else
read -p "The user will be deleted permanently along with its home directory. Are you sure to delete it? [YES/NO]: " response
if [[ $response == "YES" ]]
then
echo "Taking backup of user home directory"
mkdir /home/backup_$user4_$(date +%d%m%y%H%M%S)
cp -r /home/$user4 /home/backup_$user4_$(date +%d%m%y%H%M%S)
ls -ld /home/backup_$user4_$(date +%d%m%y%H%M%S)
id $user4 &> /dev/null
if [[ $? -eq 0 ]]
then
echo "**Removing the user**"
userdel -r $user4 &> /dev/null
groupdel -r $user4 &> /dev/null
echo " "
echo "User $user4 is removed"
else
echo "Thanks!! The user $user4 won't be removed"
fi
fi
fi;;
9. I have defined the next case statement to create a group. The script will take the group name from terminal and will check if it exists. If yes, it will not proceed further. If no exist, it will proceed with the creation of the group.
F)
echo "******Creation of Group******"
read -p "Enter the Group name: " grp3
cat /etc/group | grep $grp3 &> /dev/null
if [[ $? -eq 0 ]]
then
echo "The group $grp3 already exists!! Please refer the below details"
cat /etc/group | grep $grp3
else
echo "Proceeding with group creation..."
groupadd $grp3
sleep 1
echo "The group is created!! Please refer the below details"
cat /etc/group | grep $grp3
fi;;
10. The next case statement is defined to notify requestor to choose correct option apart from the above defined option.
*) echo "Please enter the correct option to perform the operation"
esac
11. The entire script will execute through a while loop which will keep on running until the user terminates it. It will help user to avoid executing the script again and again to perform operation. The entire script is like below.
#!/bin/bash
#
#Complete User operation
#########################
#Checking if the execution is being performed by root user
if [[ $UID -ne 0 ]]
then
exit 1
else
while true
do
echo "*************************"
echo "Welcome to user operation"
echo "*************************"
echo "A -- Create a new user"
echo "B -- Assign password to the user"
echo "C -- Add user to a group"
echo "D -- Change primary group of the user"
echo "E -- Delete user"
echo "F -- Create Group"
echo "*************************"
echo "Enter the option to proceed: "
read vars
case $vars in
A)
echo "******User Creation operation******"
read -p "Enter the user name you want to create: " user
echo "Checking if the user exists"
id $user &> /dev/null
if [[ $? -ne 0 ]]
then
echo "The user doesn't exist. Hence creating the new one"
mkdir /home/$user
useradd -d /home/$user $user
chmod 770 /home/$user
chown $user:$user /home/$user
echo " "
echo "The user is created. Please refer the details below:"
id $user
echo "The detail of home directory is as below:"
ls -ld /home/$user
else
echo "The user $user already exists. Can't create the user again"
fi;;
B)
echo "******Password change operation******"
read -p "Enter the user name to assign password: " user1
id $user1 &> /dev/null
if [[ $? -ne 0 ]]
then
echo "The user $user1 doesn't exist, please create it :("
else
echo "Enter the password: "
read -s pass1
echo "Re-enter the password (If not similar with previous password, the operation will fail: "
read -s pass2
if [[ "$pass1" != "$pass2" ]]
then
echo "The check has failed!!! Hence can't update the password!!!"
else
echo "Assigning password to the user"
echo "$user1:$pass1" | chpasswd 2> /dev/null
if [[ $? -eq 0 ]]
then
echo " "
echo "The password for $user1 is updated"
else
echo " "
echo "Couldn't update the password"
fi
fi
fi;;
C)
echo "******Adding user to a group******"
read -p "Provide the username which needs to be added in the group: " user2
id $user2 &> /dev/null
if [[ $? -eq 0 ]]
then
read -p "Provide the group name: " grp1
cat /etc/group | grep $grp1 &> /dev/null
if [[ $? -eq 0 ]]
then
id $user2 | awk '{print $3}' | grep $grp1 &> /dev/null
if [[ $? -ne 0 ]]
then
echo "Adding the user to the group"
usermod -a -G $grp1 $user2
echo " "
echo "The user is added to the group, please check the details"
id $user2
else
echo " "
echo "The user is already added to group $grp1"
id $user2
fi
else
echo " "
echo "The group $grp1 doesn't exist"
fi
else
echo " "
echo "Invalid user name!! Please recheck the user name"
fi;;
D)
echo "******Modifying the primary group of the user******"
read -p "Enter the user name: " user3
id $user3 &> /dev/null
if [[ $? -eq 0 ]]
then
read -p "Enter the group name which will be assigned as primary group: " grp2
cat /etc/group | grep $grp2 &> /dev/null
if [[ $? -eq 0 ]]
then
id $user3 | awk '{print $2}' | grep $grp2 &> /dev/null
if [[ $? -ne 0 ]]
then
echo "Updating the primary group"
usermod -g $grp2 $user3
echo " "
echo "The primary group is changed. Please check the status below"
id $user3
else
echo " "
echo "The group $grp2 is already the primary group"
id $user3
fi
else
echo " "
echo "The group $grp2 does not exist"
fi
else
echo " "
echo "Invalid user name!! Please recheck the user name"
fi;;
E)
echo "******User Deletion******"
read -p "Enter the user name to be deleted: " user4
id $user4 &> /dev/null
if [[ $? -ne 0 ]]
then
echo "The username $user4 is invalid!!! Please provide correct details!!!"
else
read -p "The user will be deleted permanently along with its home directory. Are you sure to delete it? [YES/NO]: " response
if [[ $response == "YES" ]]
then
echo "Taking backup of user home directory"
mkdir /home/backup_$user4_$(date +%d%m%y%H%M%S)
cp -r /home/$user4 /home/backup_$user4_$(date +%d%m%y%H%M%S)
ls -ld /home/backup_$user4_$(date +%d%m%y%H%M%S)
id $user4 &> /dev/null
if [[ $? -eq 0 ]]
then
echo "**Removing the user**"
userdel -r $user4 &> /dev/null
groupdel -r $user4 &> /dev/null
echo " "
echo "User $user4 is removed"
else
echo "Thanks!! The user $user4 won't be removed"
fi
fi
fi;;
F)
echo "******Creation of Group******"
read -p "Enter the Group name: " grp3
cat /etc/group | grep $grp3 &> /dev/null
if [[ $? -eq 0 ]]
then
echo "The group $grp3 already exists!! Please refer the below details"
cat /etc/group | grep $grp3
else
echo "Proceeding with group creation..."
groupadd $grp3
sleep 1
echo "The group is created!! Please refer the below details"
cat /etc/group | grep $grp3
fi;;
*) echo "Please enter the correct option to perform the operation"
esac
done
fi
12. Now lets demonstrate the script.
User Creation:
Trying to create existing user:
Taking password in secret mode and assigning it to the user:
Trying to assign wrong password:
Trying to assign password to non-existent user:
Adding existing user to an existing group:
Trying to add non-existing user to existing group:
Trying to add an existing user to non-existing group:
Trying to add user to a group where it is already added:
Changing the primary group of the user:
Trying to change the primary group of non-existent user:
Trying to add a non-existent group as primary group of the existing user:
Trying to assign the primary group which is same as existing primary group:
Deletion of user:
Trying to delete a non-existent user:
Trying to create an existing group:
Creating a new group:
Conclusion:
As it is seen, all the demonstrations are successfully completed and all requirements are met.
Thanks for your time!! If you like my work and want to encourage me to publish more such content, you can buy me a coffee.