Advanced Linux Shell Scripting: DevOps Mastery with User Management
Table of contents
- Introduction
- Tasks
- Write a bash script create directories.sh that when the script is executed with three given arguments to create directories
- Create a Script to backup all your work done till now
- Read about cron and crontab to Automate Backup Script
- To automate backup script using crontab
- User Management in Linux
- Create 2 users and just display their usernames
- Summary
Introduction
Welcome to the world of Advanced Linux Shell Scripting and User Control! In this journey, we'll explore the magic of the Linux command line, helping you control your system better.
Shell scripting is like having a powerful tool in the Linux world. It lets you automate tasks, improve processes, and make the most of your operating system. We're going beyond the basics into advanced territory, where complex tasks become simple and efficiency is key.
User control is a crucial aspect we'll focus on. Managing users in Linux is vital for system security, access control, and resource management. Whether you're a DevOps engineer, a system administrator, or just eager to enhance your skills, mastering advanced user management techniques will open up new opportunities.
Tasks
Write a bash script create directories.sh that when the script is executed with three given arguments to create directories
#!/bin/bash
# Check if three arguments are provided
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <directory_name> <start_number> <end_number>"
exit 1
fi
# Assigning arguments to variables
directory_name=$1
start_number=$2
end_number=$3
# Loop through the range of directories
for ((i=start_number; i<=end_number; i++)); do
# Creating the directory with dynamic name
mkdir -p "${directory_name}${i}"
echo "Directory ${directory_name}${i} created."
done
echo "Directories created successfully."
Create a Script to backup all your work done till now
#!/bin/bash
# Define backup directory name with timestamp
backup_dir="/home/sh"
timestamp=$(date +"%Y%m%d_%H%M%S")
# Define backup destination
backup_dest="$backup_dir/backup_$timestamp"
mkdir -p "$backup_dest"
cp -R * "$backup_dest"
if [ $? -eq 0 ]; then
echo "Backup Completed Successfully"
else
echo "backup Failed"
fi
Read about cron and crontab to Automate Backup Script
cron is like a scheduling assistant for your computer. It's a tool used in Unix-like operating systems (such as Linux) to automate tasks at specific times or intervals.
Here's how it works:
Setting a Schedule: With cron, you can tell your computer to do something (like run a script or command) at a particular time, on a specific day, or at regular intervals (like every hour or every day).
Using the crontab: The schedule for these tasks is stored in a file called the "crontab" (short for "cron table"). Each user on the system can have their own crontab, which lists the tasks they want to run and when.
Syntax: In the crontab, you specify when you want a task to run using a specific syntax. This syntax includes fields for minutes, hours, days of the month, months, and days of the week. You also specify the command or script you want to run.
Automation: Once you set up a task in the crontab, cron takes care of the rest. It'll execute the specified command or script according to the schedule you've defined, without you needing to do anything manually.
In essence, cron is like having a personal assistant that remembers to do certain tasks for you at the times you specify, making it a handy tool for automating repetitive or scheduled tasks on your computer.
- Commands for the Corntab are as follows:
crontab -e = To select an editor and open corntabEdits a copy of the user's crontab file.
crontab -l \= Lists the user's crontab file.
crontab -r = Removes the user's crontab file from the crontab directory.
To automate backup script using crontab
crontab -e
replace 06 06 with to automate this backup for every minute and every hour.
User Management in Linux
User management in Linux is the process of controlling who can access the system and what they can do once they're logged in. Here's a simple breakdown:
User Accounts: Each person who uses a Linux system has their own user account. This account includes a username and password, which they use to log in.
User Groups: Users can be organized into groups, which helps manage permissions more efficiently. For example, all users who need access to administrative tasks might be in the "sudo" group.
Permissions: Linux systems use permissions to determine what users and groups can do with files and directories. These permissions include read, write, and execute access for the owner of the file, the group associated with the file, and all other users.
User Management Tools: Linux provides various tools for managing users and groups. For example, the
useradd
command is used to add new users,passwd
is used to change passwords, andusermod
is used to modify user attributes.System Accounts: Some user accounts are used by the system itself, rather than by individual users. These accounts typically have restricted permissions and are used to perform specific system tasks.
Overall, user management in Linux is about controlling access to the system, organizing users into groups, and setting permissions to ensure that users can only access the resources they need. It's a fundamental aspect of system administration and security on Linux systems.
Create 2 users and just display their usernames
#To add new user
useradd username
#To check user configuration File
cat/etc/passwd
#To print all the user in linux
awk -F ':' '{print $1}' /etc/passwd
Summary
User management in Linux involves controlling access to the computer and actions users can perform. Each individual has their own account with a username and password. Accounts can be grouped for easier handling. Rules can be set to decide file and program access. Tools exist for adding, changing, and deleting user accounts. This process is crucial for system security and organization.