Skip to main content

Command Palette

Search for a command to run...

Week 2: Linux System Administration & Automation

Published
5 min readView as Markdown
Week 2: Linux System Administration & Automation

📌 Tasks

1️⃣ User & Group Management

  • User and group management is a core component of Linux system administration, ensuring secure access control and efficient permission management.

    1. User Management :

      User management is a key component of Linux system administration, covering user creation, modification, deletion, and permission control.

  • Creating Users: sudo adduser devops_user

  • Deleting Users: sudo deluser devops_user

  • Modifying Users: sudo usermod -l devops_user oldname

  • List all groups: cat /etc/passwd

  1. Group Management :

    Group management in Linux refers to the process of creating, modifying, and managing groups of users. key role in organizing users and assigning permissions efficiently.

. Create a group: sudo groupadd devops_team

. Delete a group: sudo groupdel devops_team

. Add a user in a group : sudo gpasswd -a devops_user devops_team

. Remove a user from a group: sudo gpasswd -d devops_user devops_team

. List all groups: cat /etc/group

2️⃣ File & Directory Permissions :

Linux permissions are divided into three categories Read (r),Write (w) and Execute (x) , Read allow for read only any file or directories, write allow for write, delete and rename any file or directories and Execute allow for run program/script. There is 3 labels of permission owner, group and other . Example - rwx r-x r—-

For example :

If we want owner can edit , group can read and other have no access :

  • Create /devops_workspace and a file project_notes.txt.

We can restrict SSH login for certain users by editing the /etc/ssh/sshd_config file using vim:

First need to install the openssh-server - Sudo apt-get install openssh-server

then need to create file /etc/ssh/sshd_config

3️⃣ Log File Analysis with AWK, Grep & Sed

Logs are crucial in DevOps! You’ll analyze logs using the Linux_2k.log file from Log Hub (GitHub Repo).

Task:

  • Extract insights using commands:

    • Use grep to find all occurrences of the word "failure".

    • To find all occurrences of the word "failure" in a file using grep, you can use the following command:

  • Use awk to extract timestamps and log levels.

  • To extract timestamps and log levels from log files using awk, you'll need a command that matches the specific format of your logs.

  • Use sed to replace all IP addresses with [REDACTED] for security.

    sed ‘s/([0-9]{1,3}\.){3}[0-9]{1,3}/g‘ Linux_2k.log

  • Here's a breakdown of the command:

    • -E: Enables extended regular expressions.

    • s/([0-9]{1,3}\.){3}[0-9]{1,3}/[REDACTED]/g: The substitution pattern:

      • ([0-9]{1,3}\.){3}: Matches the first three octets of the IP address.

      • [0-9]{1,3}: Matches the last octet of the IP address.

      • The pattern is then replaced with [REDACTED].

    • filename: The name of your file where you want to replace the IP addresses.

  • Bonus: Find the most frequent log entry using awk or sort | uniq -c | sort -nr | head -10

  • Here's a breakdown of the command:

    1. awk '{print $0}' filename: This extracts the entire log entry from the file.

    2. sort: This sorts the log entries.

    3. uniq -c: This counts the unique occurrences of each log entry.

    4. sort -nr: This sorts the counts in descending order.

    5. head -10: This displays the top 10 most frequent log entries.

4️⃣ Volume Management & Disk Usage

Volume management is a crucial skill for any Linux enthusiast or DevOps professional.

1: Create a Directory for Mounting Start by creating a directory where the volume will be mounted. This directory acts as the access point for the volume's data.

sudo mkdir -p /mnt/devops_data

Step 2: Set Up a New Volume (or Loop Device) For local practice, you can use a loop device to simulate a new volume. Here's how to create and attach one:

sudo dd if=/dev/zero of=/tmp/devops_volume.img bs=1M count=100
  1. Associate the file with a loop device:

    bash

     sudo losetup /dev/loop0 /tmp/devops_volume.img
    
  2. Format the loop device with a filesystem:

    bash

     sudo mkfs.ext4 /dev/loop0
    

Step 3: Mount the Volume Mount the newly created volume to the directory:

bash

sudo mount /dev/loop0 /mnt/devops_data

Step 4: Verify the Mount Check if the volume is mounted correctly using the following commands:

  1. Display disk usage:

    bash

     df -h
    
  2. Confirm the mount point:

    bash

     mount | grep devops_data
    

Step 5: Unmount and Clean Up (Optional) When you're done, unmount the volume and detach the loop device:

bash

sudo umount /mnt/devops_data
sudo losetup -d /dev/loop0

5️⃣ Process Management & Monitoring

1. Starting a Background Process Run the following command to start a background process and log its output. ping google.com > ping_test.log &

Step 2: Monitor the Process :

Using ps :

Using top:

Using htop:

  • If htop is not installed, install it with sudo apt-get install htop

  • Step 3: Kill the Process

    • Kill -9 <PID>

6️⃣ Automate Backups with Shell Scripting

    • Task:

      • Write a shell script to back up /Devops_workspace as backup_$(date +%F).tar.gz.

Step 1: Create the /Devops_workspace Directory

  1. Open your terminal.

  2. Run the following command to create the directory:

      mkdir -p /Devops_workspace
      mkdir -p /Backup
    

    Step 2: Create the Shell Script

    1. Open your favorite text editor and create a new file, e.g.,BACKUP.sh

        vim BACKUP.sh
      

Step 3: Make the Script Executable

  1. Run the following command to make the script executable:

      chmod 764 BACKUP.sh
    

Step 4: Run the Script

    1. Execute the script by running:

          ./BACKUP.sh
      

Step 5: Schedule the Script Using Cron

  1. Open the crontab file for editing:

      crontab -e
    
  2. Add the following line to schedule the script to run daily at a specified time (e.g., at 2:00 AM):

      0 2 * * * /path/to/BACKUP.sh
    

    Replace /path/to/BACKUP.sh with the actual path to the BACKUP.sh script.

    The first stands for every day, the second stands for every month, the third * stands for every weekday.

  3. Save and close the crontab file.