🚀 Advanced Linux Shell Scripting for DevOps Engineers: Mastering User Management 🏗️👥

🚀 Advanced Linux Shell Scripting for DevOps Engineers: Mastering User Management 🏗️👥

In the vast and dynamic sphere of DevOps, Linux shell scripting remains an invaluable skill, enabling you to automate tasks, enhance security, and streamline your workflows 🔄.

💡 Understanding Linux Shell Scripting

Linux Shell Scripting is a powerful way of executing a sequence of commands. Shell scripting involves scripting in various shells such as BASH (Bourne Again SHell), CSH (C Shell), KSH (Korn SHell), and many more. The most common one is BASH, are we are working on the same.

🔐 User Management in Linux

User Management is a pivotal aspect of managing Linux systems. It involves creating, deleting, and managing users, as well as assigning permissions and responsibilities to these users. Having a robust user management system in place ensures a secure and organized system architecture🏰.

Here's how you can master it.

1️⃣ Creating a New User

Creating a new user in Linux is quite straightforward. Use the useradd command followed by the username. When you wish to have home directory of that user use -m. Importently, don't forget to set a password using passwd. Here's an example:

sudo useradd kshitija #Adding the user as kshitija
sudo passwd kshitija #setting password for user -> kshitija
#Now find this user in directory
cat /etc/passwd #see the last line of it; where user kshitija exist
#Now, one intereseting part to understand here. Follow below command
#Go to /home and list the files in it
cd /home
ls
#Output is -> ubuntu ; Here you wont see folder created for kshitija
# *If you want user folder to be exist* at /home directory use below cmd
sudo useradd -m sanket # -m indicates create user(Sanket's) folder in /home
cd /home
ls
#Output is --> ubuntu sanket

#See Specific User
sudo grep kshitija  /etc/passwd

2️⃣ Deleting a User

To delete a user, use the userdel command, followed by the username. For example:

sudo userdel kshitija
sudo userdel -r sanket #delete /home/sanket directory when delete the user
#*userdel: sanket mail spool /var/mail/sanket/ not found
#remove the folder manually and you are good
sudo userdel rm -rf /var/mail/sanket 
#The above output is showing the following warning message that 
#userdel: user sanket is currently used by process 30371 and 
#the same time the user account got deleted from system 
#but the ssh session not killed automatically.
#Once the user logged out from the system then the current session get disconnected and after that he can’t able to login because we have already deleted the account.

3️⃣ User & Groups

Create a Group & Add User

Managing permissions and responsibilities becomes easier when users are grouped. The groupadd command helps you create a new group:

sudo groupadd devops #create a group
sudo useradd -m user123 && adduser user123 newgroup #create a user and add it in group in oenline
cat /etc/group #Contains group information.

You can then add users to this group using the usermod command:

sudo usermod -aG devops kshitija #kshitija is part of devops group
sudo adduser yuvansh dev #create new user with new group
sudo usermod –aG group1, group2, group3 commonuser #add a user to multiple groups at once

Delete a Group & Remove User

The groupdel command helps you create a new group:

sudo userdel user1 #delete user1 from group1
sudo groupdel group1 #delete a group 
sudo gpasswd -d dev user1

4️⃣ Managing User Permissions

Linux uses permissions and ownership for security. To change the owner of a file, you can use the chown command. To modify permissions, you can use the chmod command:

sudo chown kshitija file.txt
sudo chmod 755 file.txt

Let's see some more examples of usermod command, as the name suggests, is a user modification utility in Linux. System administrators use it to modify or change user account properties, such as user home directories, login names, expiration date of the user account, and more.

sudo usermod -l john_doe john #user called 'john' , you want to change the username to 'john_doe'.
#If you need to change a user's home directory, use the -m (move) and -d (directory) options. Let's change the home directory of 'john_doe' to '/home/new_john':
sudo usermod -m -d /home/new_john john_doe  
#The -g option changes the primary group of a user. Suppose we want to change john_doe's primary group to 'admin':
sudo usermod -g admin john_doe
#You can add a user to additional groups using the -a (append) and -G (group) options. Here we add 'john_doe' to the 'sudo' and 'staff' groups:
sudo usermod -a -G sudo,staff john_doe
#To lock a user account, use the -L option. This prevents the user from logging in:
sudo usermod -L john_doe
#To unlock the account, replace -L with -U:
sudo usermod -U john_doe

🛠️ Automating User Management

For a DevOps engineer, automation is the key 🔑. By creating shell scripts to automate user management tasks, you can save time and reduce the possibility of errors. Here's a basic script to create a new user and assign it to a group:

#!/bin/bash
echo "Enter username: "
read username
sudo useradd $username
echo "Enter password: "
read password
echo -e "$password\n$password" | sudo passwd $username
echo "User successfully created"

✨ Conclusion

Understanding user management and being able to automate these tasks is an essential part of your DevOps toolkit 🧰.

When you're dealing with user management, you're handling sensitive aspects of your system architecture, so always be aware before you execute such commands⚠️.

Happy scripting! 🐧👩‍💻👨‍💻

#Linux #whatislinux #whylinux #advance #advancelinux #secure #kernel #shell #terminal #user #group #usermanagement