Day#5:Advanced Linux Shell Scripting for DevOps Engineers with User Management
Introduction
Welcome to Day 5 of the #90DaysOfDevOps challenge! Today, we dive into advanced Linux shell scripting for DevOps engineers, focusing on tasks like creating directories dynamically, backing up your work, and managing users in Linux. Let’s explore these concepts step by step.
A Thought Experiment
Imagine opening a repository directory named 2023
and discovering 90 sub-directories inside it. What comes to your mind? Were these directories created manually, with a script, or with a single command? The answer might surprise you!
Using the simple command below, all 90 directories were created within seconds:
mkdir day{1..90
Task 1: Create Directories Using a Shell Script
Problem Statement
Write a bash script createDirectories.sh
that, when executed with three arguments (directory name, start number, and end number), creates a specified number of directories dynamically.
Examples
- Example 1: When executed as
./
createDirectories.sh
day 1 90
, it creates 90 directories asday1 day2 day3 ... day90
.
\> Example 2: When executed as ./
createDirectories.sh
Movie 20 50
, it creates 31 directories as Movie20 Movie21 Movie22 ... Movie50
.
Implementation
Here’s the script:
How to Run
Save the script as
createDirectories.sh
.Make it executabl:
chmod +x createDirectories.sh
Run the script with your desired arguments.
Task 2: Create a Backup Script
Importance of Backups
Picture this: you’ve spent hours working on a project, only to lose your progress due to an unexpected system crash. Backups are your safety net, ensuring you never lose important work. As a DevOps engineer, automating this process is crucial.
Implementation
Here’s a backup script to keep your work safe:
1. Check If the Documents
Folder Exists for Source Directory
Run the following command:
ls /home/ubuntu/Documents
If the folder does not exist, create it:
mkdir -p /home/ubuntu/Documents
2. Check If the “Documents” Folder Contains Files:
If the folder exists but is empty, add a test file:
echo "This is a test file." > /home/ubuntu/Documents/test_file.txt
4. Updated Scripts
Steps to Run:
Save the Script: Ensure the script is saved with the changes.
Make Sure the
Documents
Folder Exists:Verify:
ls ~/Documents
If it's empty, add test files:
echo "Test file 1" > ~/Documents/file1.txt echo "Test file 2" > ~/Documents/file2.txt
Run the Script:
./backup.sh
Expected Output:
If everything is set up correctly, the output should look something like this:
Task 3: Read About Cron and Crontab to Automate the Backup Script:
Cron is a time-based job scheduler in Unix-like operating systems, including Linux. It automates the execution of scripts or commands at specified intervals or times.
Crontab (short for "cron table") is a configuration file that specifies what tasks to run and when to run them. Each user on a system can have their own crontab file.
How Cron and Crontab Work
Cron Daemon (
crond
):- A background service that reads crontab files and executes the specified tasks on schedule.
Crontab Command:
crontab
allows users to manage their scheduling entries.
Crontab Syntax: A crontab entry consists of time fields and the command to execute:
MIN HOUR DOM MON DOW COMMAND
MIN
: Minute (0-59)HOUR
: Hour (0-23)DOM
: Day of the Month (1-31)MON
: Month (1-12 or names likejan
)DOW
: Day of the Week (0-7, where both 0 and 7 represent Sunday)COMMAND
: The script or command to run
Common Commands for Crontab
List Crontab Entries:
crontab -l
Edit Crontab:
crontab -e
- This opens the crontab file in your default text editor (usually
nano
).
- This opens the crontab file in your default text editor (usually
Remove Crontab:
crontab -r
Automating the Backup Script
You can use cron to automate the execution of your backup script.
Steps to Automate the Backup Script
Ensure the Script is Ready:
Save your script, for example, as
/home/ubuntu/
backup.sh
.Make it executable:
chmod +x /home/ubuntu/backup.sh
Open the Crontab File:
crontab -e
Add a New Cron Job: Add an entry to schedule the backup script. For example:
To run the script every day at 2:00 AM:
Explanation of the fields:
0
: Minute (at the start of the hour)2
: Hour (2 AM)*
: Every day of the month*
: Every month*
: Every day of the week
Save and Exit:
- Save the file and exit the editor. The cron daemon will automatically pick up the changes.
Verify the Cron Job:
List your crontab entries to ensure the job is scheduled:
crontab -l
Delete crontab :
crontab -i -r
Task 4: Read About User Management:
A user is an entity in a Linux operating system that can manipulate files and perform several other operations. Each user is assigned an ID that is unique within the system. IDs 0 to 999 are assigned to system users, and local user IDs start from 1000 onwards.
Create 2 users and display their usernames.
Script for User Management:
#!/bin/bash
# Function to create a new user
create_user() {
local username=$1
if id "$username" &>/dev/null; then
echo "User $username already exists."
else
sudo useradd "$username"
echo "User $username created successfully."
fi
}
# Function to set a password for the user
set_password() {
local username=$1
echo "Setting password for $username..."
sudo passwd "$username"
}
# Function to display user details
display_user_details() {
local username=$1
echo "Details for user $username:"
id "$username"
echo
}
# Main script execution
echo "Starting user management script..."
# Create two users
USER1="TWS"
USER2="Junoon"
create_user "$USER1"
create_user "$USER2"
# Set passwords for the users
set_password "$USER1"
set_password "$USER2"
# Display the usernames and details
echo "Displaying usernames and details:"
display_user_details "$USER1"
display_user_details "$USER2"
echo "User management script completed."
Step 1: Create Two Users
To create a user, we will use the useradd
command. We can also specify additional parameters such as the user’s home directory and shell.
Here are the commands to create two users, TWS and Junoon:
sudo useradd TWS
sudo useradd Junoon
This will create two users: TWS and Junoon. By default, these users will be assigned UIDs starting from 1000 (or the next available UID in your system).
Step 2: Set Password for the Users
Once the users are created, we need to assign passwords for them. This can be done using the passwd
command.
You'll be prompted to enter a password for each user.
Step 3: Display Usernames and UIDs
To display the usernames and UIDs, you can use the id
command, which will show the UID, GID (Group ID), and groups the user belongs to.
Alternatively, you can display the usernames directly by running:
whoami
This command will return the current logged-in user. To check other users, you can inspect the /etc/passwd
file, which contains user information.
This will return lines like:
TWS:x:1001:1001::/home/TWS:/bin/sh
Junoon:x:1002:1002::/home/Junoon:/bin/sh
Here, 1001 and 1002 are the UIDs of TWS and Junoon, respectively.
Step 4: Displaying Usernames
To display only the usernames without any additional details, you can use:
This will display a list of all usernames in the system, including TWS and Junoon.
Summary: Key Concepts in User Management
User IDs (UIDs): Every user has a unique identifier in Linux. System users have UIDs ranging from 0 to 999, and local users start from UID 1000.
Creating Users: The
useradd
command is used to create new users, while thepasswd
command assigns a password to the user.Displaying User Info: The
id
andwhoami
commands are useful for viewing user information, while/etc/passwd
contains a list of users and their associated details.