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

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

Advancing with Advanced Linux Shell Scripting: User Management

In Day 5 of the 90 Days of DevOps Challenge, we venture into the realm of advanced Linux shell scripting, particularly focusing on user management. Let's delve into the theoretical underpinnings and the pivotal tasks for today.

  1. You have to do the same using Shell Script i.e using either Loops or command with start day and end day variables using arguments -

    A shell script named createDirectories.sh needs to be created. This script, when executed with specific arguments, will generate directories as per the defined parameters.

Here's an example script to accomplish this:

bashCopy code#!/bin/bash

# Check if three arguments are provided
if [ "$#" -ne 3 ]; then
    echo "Usage: $0 directory_name start_number end_number"
    exit 1
fi

directory_name=$1
start_number=$2
end_number=$3

# Loop to create directories based on arguments
for ((i = start_number; i <= end_number; i++)); do
    mkdir -p "${directory_name}${i}"
done

This script checks if the required number of arguments is provided. If not, it displays the usage. If three arguments are provided, it utilizes a loop to create directories from the start number to the end number, using the given directory name. For instance, executing ./createDirectories.sh day 1 90 would create directories labeled as day1, day2, day3, and so on up to day90.

This script aligns with the specifications, creating directories based on user-provided parameters, enhancing automation and simplifying the process of directory creation.

  1. Create a Script to backup all your work done till now.

    Creating a backup script is crucial for preserving work. Here’s an example of a simple backup script that copies your work to a specified directory:

     bashCopy code#!/bin/bash
    
     # Define source and destination directories
     source_directory="/path/to/your/work"  # Replace with your work directory path
     backup_directory="/path/to/backup"    # Replace with your backup directory path
    
     # Create a backup directory if it doesn't exist
     mkdir -p "$backup_directory/backup_$(date +"%Y-%m-%d")"
    
     # Copy all work to the backup directory
     cp -r "$source_directory" "$backup_directory/backup_$(date +"%Y-%m-%d")"
    

    This script backs up your work by copying it from the source directory to a backup directory. Ensure to replace "/path/to/your/work" and "/path/to/backup" with your actual work directory and the directory where you want to store the backups, respectively. It creates a dated backup folder in the backup directory to maintain a record of each backup session.

  2. Read About Cron and Crontab, to automate the backup Script

    Cron and Crontab are fundamental tools for scheduling and automating tasks in a Unix-like operating system.

    Cron: Cron is a time-based job scheduler in Unix-like operating systems. It's responsible for executing commands or scripts at specific time intervals. These tasks can include system maintenance, backups, and other automated functions. Cron jobs are particularly useful for repetitive tasks that need to be performed regularly without user intervention.

    Crontab: Crontab (short for "cron table") is a file used by the cron daemon to specify tasks that need to be run periodically. Each user on a Unix-based system can have their own crontab, which lists the commands to be executed and the schedule at which they should run.

    A crontab file consists of lines representing individual jobs. Each line has six fields that define the schedule for a particular task:

    1. Minute (0-59)

    2. Hour (0-23)

    3. Day of Month (1-31)

    4. Month (1-12)

    5. Day of Week (0-7, where both 0 and 7 represent Sunday)

    6. Command to be executed

For instance, to schedule a backup script to run daily at 3 AM, you might have an entry like:

    bashCopy code0 3 * * * /path/to/backup_script.sh

This entry instructs the system to run the script /path/to/backup_script.sh every day at 3:00 AM.

By utilizing cron and crontab, users can automate routine tasks, such as backups, system maintenance, log rotations, and more, ensuring these tasks are executed without manual intervention.

  1. Read about User Management

    User management is a crucial aspect of system administration. In Linux operating systems, users are essential entities, each with specific permissions and capabilities. Here's an expanded insight into user management:

    User Management in Linux:

    In a Linux operating system, users are entities that can interact with the system, manipulate files, execute programs, and perform various other operations. Each user is uniquely identified by a user ID (UID), which is integral for determining access permissions and ensuring security within the system.

    • Root User (Superuser): The root user, with a UID of 0, holds supreme administrative privileges and unrestricted access to all system resources. It's essential for system maintenance, configuration, and overall control.

    • System Users: User IDs from 1 to 999 are typically assigned to system users. These accounts are generally used to manage system processes and are not associated with human users.

    • Local Users: User accounts starting from UID 1000 onwards are typically local users created for human use. These users have restricted access and specific permissions based on their roles.

Managing Users with Commands:

Several commands assist in managing users and gathering information about them in a Linux environment:

  • who and w: Display information about currently logged-in users.

  • id: Provides information about the current user, including UID, GID (Group ID), and supplementary group IDs.

  • whoami: Displays the username of the current user.

  • su and sudo: Facilitate user switching and executing commands as another user or the superuser (root).

  • adduser and useradd: Used to create new user accounts.

  • usermod: Enables modifications to existing user accounts, including changing passwords, groups, etc.

  • userdel: Deletes user accounts.

  • passwd: Manages user passwords.

User management is pivotal for maintaining system security and organizing access privileges. Understanding and effectively utilizing these commands is essential for system administrators and DevOps engineers to ensure smooth operations, security, and controlled access within a Linux environment.

  1. Create 2 users and just display their Usernames

    To create two users and display their usernames in Linux, you can use the following commands:

    1. Create Users:

Use the useradd command to create two users. For example, let's create users named user1 and user2:

    bashCopy codesudo useradd user1
    sudo useradd user2
  1. Display Usernames:

To display the usernames, you can use the awk command in conjunction with /etc/passwd, which contains user account information.

    bashCopy codeawk -F: '{ print $1}' /etc/passwd | grep "^user"

The awk command, with the field separator set to :, extracts and prints the first field (username) from /etc/passwd. The grep command filters usernames starting with "user".

This will display the usernames of the users created, in this case, user1 and user2.

Day 5's exploration into Linux operations and user management has equipped us with essential skills for DevOps. From shell scripting automation to user management, these tasks highlight the vital tools and practices pivotal for efficient system administration and automation in the DevOps landscape.