Day 5: Advanced Linux Shell Scripting for DevOps Engineers with User Management

Hi, I’m Rakshita. A Cloud, DevOps, AI, and Python enthusiast passionate about learning and simplifying technology for others. I love exploring how modern tools and automation can make systems smarter and more efficient. Here, I write about: ☁️ Cloud & DevOps practices 🤖 AI in the world of automation 🐍 Python for real-world problem-solving 💡 Growth, consistency, and the learner’s mindset My goal is to bridge the gap between learning and doing, and help others grow confidently in the evolving tech landscape.
Create Directories Using Shell Script:
Write a bash script
create_directories.shthat, when executed with three arguments (directory name, start number of directories, and end number of directories), creates a specified number of directories with a dynamic directory name.- When executed as
./create_directories.shday 1 90, it creates 90 directories asday1 day2 day3 ... day90.
- When executed as
#!/bin/bash
dir_name=$1
start_day=$2
end_day=$3
mkdir $(eval echo $dir_name{$start_day..$end_day})
Make this file executable:
chmod 700 create_directories.sh
Run the file:
./create_directories.sh day 1 90
Then list the directories:
ls
Understanding Cron and Crontab : Automating Tasks on Linux
In the world of Linux, cron and crontab are essential tools for automating repetitive tasks. Whether you're a developer, a system administrator, or just a Linux enthusiast, knowing how to use these tools can save you time and effort.
What is Cron?
Cron is a time-based job scheduler in Unix-like operating systems. It runs in the background and executes commands at specified times and dates. Think of it as a personal assistant that handles your routine tasks for you.
What is Crontab?
Crontab (short for "cron table") is a file that contains the schedule of cron jobs. Each user can have their own crontab file, which lists the commands to be run and the times they should be executed.
Structure of a Crontab Entry
A typical crontab entry looks like this:
* * * * * command_to_be_executed
- - - - -
| | | | |
| | | | +----- Day of the week (0 - 7) (Sunday is both 0 and 7)
| | | +------- Month (1 - 12)
| | +--------- Day of the month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)
Each asterisk represents a field for time and date, and they can be replaced with specific values.
Understanding User Management
User management in Linux involves creating, modifying, and deleting user accounts. These accounts are essential for defining permissions, access levels, and individual user environments.
Creating and Managing User Accounts
- Adding a New User
To add a new user, you can use the useradd command followed by the username. For example:
sudo useradd rakshita
- Setting a Password
After creating a user, set a password using the passwd command:
sudo passwd rakshita
- Deleting a User
To delete a user and their home directory, use the userdel command with the -r flag:
sudo userdel -r rakshita
Managing Groups
- Adding a New Group
To add a new group, use the groupadd command:
sudo groupadd devops
- Adding a User to a Group
To add a user to a group, use the usermod command with the -aG flags:
sudo usermod -aG devops rakshita
- Viewing Group Membership
To view the groups a user belongs to, use the groups command:
groups rakshita
Understanding Permissions
Linux file permissions are represented by a set of three characters for the owner, group, and others. For example:
-rw-r--r--
The first character represents the file type ('-' for regular files, 'd' for directories). The next nine characters are divided into three sets of three:
Owner permissions (rw-): Read and write.
Group permissions (r--): Read-only.
Other permissions (r--): Read-only.
Permission Types
r: Read permission. The file can be read.
w: Write permission. The file can be modified.
x: Execute permission. The file can be executed as a program.
Viewing File Permissions
To view the permissions of a file or directory, use the 'ls -l' command:
ls -l <filename>
This will display the permissions, owner, group, and other details about the file.
Changing File Permissions
The 'chmod' command is used to change the permissions of a file or directory.There are two ways to use chmod: symbolic mode and numeric mode.
Symbolic Mode
In symbolic mode, you use letters to specify the changes:
ufor user (owner)gfor groupofor othersafor all
For example, to add execute permission for the owner:
chmod u+x <filename>
To remove write permission for the group:
chmod g-w <filename>
Numeric Mode
In numeric mode, you use a three-digit number to set the permissions. Each digit represents a different category of users (owner, group, others) and is the sum of the values for read (4), write (2), and execute (1) permissions.
For example:
'7' (4+2+1) gives read, write, and execute permissions.
'5' (4+1) gives read and execute permissions.
'4' gives read permission only.
To set the permissions to rwxr-xr-- (755):
chmod 755 <filename>
Using 'chown' and 'chgrp'
The 'chown' command changes the owner of a file or directory:
sudo chown <newowner> <filename>
The 'chgrp' command changes the group of a file or directory:
sudo chgrp <newgroup> <filename>
We can also change both the owner and group at the same time:
sudo chown <newowner>:<newgroup> <filename>
Conclusion
Mastering user and file management in Linux is essential for maintaining a secure and efficient system. We explored how to create and manage users and groups using commands like useradd, usermod, and groupadd. We delved into file permissions, understanding how to view and modify them using chmod, both in symbolic and numeric modes.
By applying these concepts, you can ensure your Linux environment is well-organized and secure. Whether you're a beginner or an experienced user, these skills are fundamental to effective Linux system administration.
Happy managing, and keep exploring the powerful world of Linux! If you have any questions or tips, feel free to share them in the comments.
HAPPY LEARNING🚀




