Skip to main content

Command Palette

Search for a command to run...

Linux For DevOps πŸ§πŸš€

Updated
β€’11 min readβ€’View as Markdown
Linux For DevOps πŸ§πŸš€

In the world of DevOps, mastering Linux is non-negotiable. From deploying cloud services to automating pipelines, Linux provides the foundation for the majority of DevOps tools and platforms. Whether you're new to DevOps or looking to sharpen your skills, understanding Linux is essential to your success.

This guide will take you step-by-step through the most important aspects of using Linux for DevOps, including setting up Linux on AWS EC2, understanding the Linux file system hierarchy, learning basic and advanced Linux commands, and finally, automating tasks with shell scripting through a real-world project.


🐧 Why Linux is Crucial for DevOps?

Linux plays a huge role in the DevOps ecosystem for several key reasons:

  • Open Source and Customizable 🌐: Linux is free to use, open-source, and provides developers with the flexibility to customize the system according to their needs.

  • Stability and Security πŸ”’: Linux is known for its robust security features and stable performance, critical in production environments.

  • Command Line Power πŸ–₯️: The command-line interface (CLI) on Linux provides greater control over system operations, which is key for automation.

  • DevOps Tools Integration πŸ”„: Popular DevOps tools like Docker, Kubernetes, Ansible, and Jenkins are designed to run seamlessly on Linux-based environments.

Let’s dive into the specifics of setting up and using Linux for DevOps workflows.


πŸš€ Setting Up Linux via AWS EC2

One of the best ways to practice Linux is by setting up a Linux server on AWS EC2. AWS offers a free tier option, which makes it a great place to start.

Steps to Launch a Linux Instance on AWS EC2:

  1. Sign Up for AWS πŸ“:

    • Head over to AWS and create a free-tier account.
  2. Launch a New EC2 Instance πŸ–₯️:

    • Navigate to EC2 under the AWS services.

    • Click on Launch Instance and select Amazon Linux 2 or another distribution like Ubuntu.

  3. Choose an Instance Type βš™οΈ:

    • For learning purposes, select t2.micro, which is part of the AWS free tier.
  4. Configure Security Groups πŸ”:

    • Ensure you allow SSH (port 22) traffic from your IP or from anywhere if you plan to access the server from different networks.
  5. Download Key Pair πŸ”‘:

    • Download the .pem file (key pair) when prompted, which will allow you to securely access the instance.
  6. Connect to Your Instance via SSH πŸ”Œ:

    • Once the instance is running, you can SSH into it using the key file:

        ssh -i "your-key.pem" ec2-user@<your-ec2-public-ip>
      
  7. Success! πŸŽ‰:

    • You now have a fully functional Linux instance on the cloud! You can start running Linux commands and set up your DevOps environment.

πŸ—‚οΈ Linux File System Hierarchy: Understanding the Structure

After accessing your Linux instance, the next step is to familiarize yourself with the file system hierarchy. Knowing where files are stored and how to navigate them is essential for system administration and troubleshooting.

Here’s a breakdown of the key directories in Linux:

  • / (Root) 🌳: The top of the file system tree where everything starts.

  • /bin πŸ“‚: Stores essential user command binaries like ls, cp, mv.

  • /etc βš™οΈ: Contains configuration files for the system and installed services (e.g., Nginx, MySQL).

  • /home 🏠: User home directories are located here (/home/user).

  • /var πŸ“‰: Stores variable data like logs, databases, cache (/var/log holds important system logs).

  • /usr πŸ› οΈ: Contains user-installed applications and system libraries.

  • /opt πŸ—ƒοΈ: Reserved for third-party software that you install manually.

  • /tmp ❄️: Temporary files created by the system or applications; automatically cleaned up.

Understanding this structure is key to knowing where to store files, where to find logs, and how to configure applications.


πŸ”§ Basic Linux Commands for DevOps Engineers

Here are some basic commands to help you get comfortable navigating your Linux system. Mastering these commands will enable you to handle files, directories, permissions, and system monitoring efficiently.

File and Directory Management πŸ“‚:

  • ls: List the contents of a directory.

      ls       # List files and directories
      ls -la   # List all files, including hidden ones, with detailed info
    
  • cd: Change directory.

      cd /home/user     # Navigate to a directory
    
  • mkdir: Create a new directory.

      mkdir project     # Create a new directory called 'project'
    
  • cp: Copy files or directories.

      cp file.txt /backup/   # Copy 'file.txt' to the 'backup' directory
    
  • mv: Move or rename files or directories.

      mv file.txt file_old.txt   # Rename file.txt to file_old.txt
      mv /source /destination    # Move the source directory to a new location
    
  • rm: Remove files or directories.

      rm file.txt       # Delete file.txt
      rm -rf directory  # Recursively delete a directory and its contents
    

System Information and Monitoring πŸ“Š:

  • uname -a: Show system information.

      uname -a    # Displays kernel version and system architecture
    
  • df -h: Check disk usage.

      df -h    # Show disk space in human-readable format
    
  • free -m: Display memory usage.

      free -m   # Show memory usage in megabytes
    
  • top: Display running processes and system resource usage.

      top    # Real-time view of system resources and processes
    

These basic commands will help you perform essential tasks like file management, system navigation, and resource monitoring.


βš™οΈ Advanced Linux Commands for DevOps Engineers

Once you’re comfortable with the basics, it’s time to level up by learning advanced Linux commands. These commands are critical for performing system administration, troubleshooting, networking, and automation tasks efficiently.

Process Management πŸ› οΈ:

  • ps: View running processes.

      ps aux       # Display all running processes with detailed information
    
  • kill: Terminate processes by PID.

      kill -9 PID    # Forcefully terminate a process with a specific PID
    
  • htop: Interactive process viewer.

      htop    # Visual, real-time monitoring of processes and system resources
    

Networking 🌐:

  • netstat: Display active network connections and listening ports.

      netstat -tuln    # Show all active and listening network connections
    
  • curl: Transfer data to or from a server, test HTTP endpoints.

      curl http://example.com   # Fetch a URL’s content
    
  • ping: Test network connectivity to a remote server.

      ping google.com   # Test connectivity to Google’s servers
    

File and Text Processing πŸ“:

  • grep: Search for specific patterns in files.

      grep 'error' /var/log/syslog   # Search for 'error' in the system logs
    
  • sed: Perform text transformations in files.

      sed 's/old/new/g' file.txt   # Replace all occurrences of 'old' with 'new'
    
  • awk: Extract and manipulate text data from files.

      awk '{print $1, $2}' file.txt   # Print the first and second columns of a file
    

These advanced commands will allow you to manage processes, troubleshoot network issues, manipulate text files, and much more.


πŸ“ Shell Scripting for DevOps

Automation is at the core of the DevOps philosophy. Shell scripting is one of the most powerful tools a DevOps engineer can use to automate repetitive tasks such as backups, monitoring, deployments, and system management. With shell scripts, you can automate almost any task in a Linux environment, from simple file manipulation to orchestrating complex pipelines.

This section will dive deep into how you can use shell scripting to automate tasks in your Linux environment with a detailed real-world project example. Whether you're a beginner or have some experience with Linux, shell scripting is a skill that will significantly enhance your efficiency as a DevOps engineer.


πŸš€ Why Use Shell Scripting for Automation?

  • Efficiency: Scripts allow you to automate repetitive tasks, saving time and reducing human error.

  • Customization: You can write scripts tailored to your specific needs and workflows.

  • Integration: Shell scripts can interact with other DevOps tools like Git, Docker, Kubernetes, and CI/CD pipelines.

  • Portability: Shell scripts can run on almost any Unix-like operating system, making them ideal for cross-platform automation.

Let’s go through a detailed explanation of shell scripting basics and a project that automates a daily backup process.


πŸ“œ Shell Scripting Basics

Before diving into a full project, let’s cover the basics of shell scripting.

Creating a Shell Script

  1. File Creation: You can write a shell script using any text editor like vim, nano, or VSCode. A shell script file typically ends with the .sh extension.

     touch myscript.sh
    
  2. Script Structure: A basic shell script starts with a shebang (#!), followed by the path to the shell you want to use. Typically, this will be /bin/bash or /bin/sh.

     #!/bin/bash
     echo "Hello, DevOps!"
    
  3. Making the Script Executable: You need to give the script execute permissions to run it.

     chmod +x myscript.sh
     ./myscript.sh
    

πŸ”„ Shell Scripting Constructs

Understanding the basic constructs in shell scripting is important for writing effective scripts.

Variables πŸ’‘

  • Variables store data and can be referenced later in the script.

      # Define a variable
      NAME="DevOpsEngineer"
    
      # Print the variable
      echo "Hello, $NAME"
    

Conditional Statements πŸ“‹

  • Conditionals allow you to make decisions within your script.

      if [ -f "/path/to/file" ]; then
          echo "File exists."
      else
          echo "File does not exist."
      fi
    

Loops πŸ”

  • Loops allow you to repeat a block of code multiple times.

    • For loop:

        for i in 1 2 3; do
            echo "Iteration $i"
        done
      
    • While loop:

        count=1
        while [ $count -le 5 ]; do
            echo "Count: $count"
            count=$((count+1))
        done
      

Functions πŸ› οΈ

  • Functions are reusable blocks of code.

      function greet {
          echo "Hello, $1"
      }
      greet "DevOps"
    

Input and Output Redirection πŸ”„

  • Redirecting input and output allows scripts to interact with files or other commands.

      # Redirect output to a file
      echo "Logging output" >> log.txt
    
      # Redirect input from a file
      while read line; do
          echo "Line: $line"
      done < file.txt
    

πŸ› οΈ Real-World Project: Automating Daily Backups

Now that we've covered the basics, let's move on to an advanced shell scripting project. In this project, we will automate the process of backing up a directory on a Linux server and ensure that backups are performed daily using cron jobs.

πŸ—„οΈ Project Overview: Daily Backup Automation

In this project, we will:

  1. Create a shell script that backs up a specific directory.

  2. Compress the backup into a .tar.gz file.

  3. Name the backup file using the current date.

  4. Set up a cron job to run the script automatically every day at a specific time.

Step 1: Writing the Shell Script

We will write a script that archives the contents of a directory and stores it in a backup directory with a timestamp.

  1. Create the Script: Open a text editor and create a new file called backup.sh.

     #!/bin/bash
    
     # Define variables
     BACKUP_DIR="/backup"
     SOURCE_DIR="/home/user/data"
     DATE=$(date +%Y-%m-%d)
     BACKUP_FILE="$BACKUP_DIR/backup-$DATE.tar.gz"
    
     # Create backup directory if it doesn't exist
     if [ ! -d "$BACKUP_DIR" ]; then
         mkdir -p $BACKUP_DIR
     fi
    
     # Create a compressed backup of the source directory
     tar -czf $BACKUP_FILE $SOURCE_DIR
    
     # Output a message to indicate the backup has been completed
     echo "Backup of $SOURCE_DIR completed and stored as $BACKUP_FILE on $DATE"
    

    Explanation of the Script:

    • BACKUP_DIR and SOURCE_DIR: Define where the backup will be stored and what directory will be backed up.

    • DATE=$(date +%Y-%m-%d): This captures the current date in YYYY-MM-DD format.

    • tar -czf: This command compresses the files from SOURCE_DIR into a .tar.gz archive stored in BACKUP_DIR.

    • echo: Outputs a confirmation message after the backup completes.

  2. Make the Script Executable: After writing the script, make it executable.

     chmod +x backup.sh
    

Step 2: Testing the Script

To ensure your script works, run it manually:

./backup.sh

If the script runs successfully, you should see a confirmation message like:

Backup of /home/user/data completed and stored as /backup/backup-2024-10-21.tar.gz on 2024-10-21

Check the /backup directory to see if the .tar.gz file was created.

Step 3: Automating with Cron Jobs

Now that we have a working backup script, we want to automate it so that it runs every day at a specific time, for example, 2:00 AM.

  1. Open the Cron Table: Use the following command to edit the crontab (the cron configuration file) for your user.

     crontab -e
    
  2. Add a Cron Job: Add the following line to schedule the script to run daily at 2:00 AM.

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

    This cron expression means:

    • 0 2: Run at 2:00 AM.

    • *: Every day of the month, every month, and every day of the week.

  3. Save and Exit: Once you've saved the cron job, it will automatically execute the backup script at 2:00 AM every day.


πŸ“Š Example: Automating System Monitoring

Here’s another common task that can be automated with shell scripting: system monitoring. The following script checks the CPU and memory usage of your server and logs the information into a file.

Monitoring Script Example:

#!/bin/bash

LOG_FILE="/var/log/system_monitor.log"
DATE=$(date +"%Y-%m-%d %H:%M:%S")

# Log CPU and memory usage
echo "System Monitoring Report - $DATE" >> $LOG_FILE
echo "---------------------------------" >> $LOG_FILE
echo "CPU Usage:" >> $LOG_FILE
top -bn1 | grep "Cpu(s)" >> $LOG_FILE
echo "Memory Usage:" >> $LOG_FILE
free -m >> $LOG_FILE
echo "---------------------------------" >> $LOG_FILE

You can schedule this script to run at regular intervals (e.g., every hour) using a cron job to automatically monitor and log system resources. It helps identify performance bottlenecks or system resource issues over time.


🌟 Key Takeaways

  • Shell scripting in Linux is an indispensable tool for automating repetitive tasks, managing deployments, and ensuring your infrastructure runs smoothly.

  • Automation can significantly reduce manual overhead and prevent human errors, especially when used in conjunction with cron jobs or integrated into CI/CD pipelines.

  • Mastering variables, loops, conditionals, and functions in shell scripts will enable you to automate complex workflows efficiently.

  • By writing scripts that handle daily backups, system monitoring, or deployment tasks, you are effectively building the automation needed in DevOps to manage infrastructure and applications at scale.


πŸ’‘ Pro Tip: Once you've mastered basic shell scripting, you can extend your automation by integrating scripts with tools like Docker, Kubernetes, or using them to trigger jobs in a CI/CD pipeline.