🐧 DevOps Day 6 — Linux Fundamentals and Shell Scripting Basics

 

🚀 Introduction

Welcome to Day 6 of the DevOps series!
Linux is the foundation of modern DevOps infrastructure — from cloud servers to CI/CD pipelines and containers, nearly every DevOps tool runs on Linux.

As a DevOps engineer, mastering Linux and shell scripting helps you:

  • Automate manual tasks

  • Manage servers efficiently

  • Understand logs, permissions, and system resources

  • Integrate scripts into CI/CD pipelines

Today, we’ll explore Linux fundamentals, essential shell commands, and scripting basics that every DevOps professional should know.




🧩 What is Linux?

Linux is a free and open-source operating system based on UNIX.
It is known for its stability, security, and flexibility.

💡 Why Linux in DevOps?

  • Most cloud servers (AWS EC2, Azure VM, GCP Compute) use Linux distributions like Ubuntu, CentOS, or Amazon Linux.

  • Tools like Docker, Kubernetes, Ansible, and Jenkins run best on Linux.

  • You can easily automate infrastructure setup using shell scripts.

🧠 Common Linux Distributions

  • Ubuntu / Debian — popular for DevOps and CI/CD servers

  • CentOS / Red Hat (RHEL) — common in enterprise infrastructure

  • Amazon Linux 2 — AWS-optimized version for EC2

  • Alpine Linux — lightweight and used in Docker containers


⚙️ The Linux Architecture

Linux is made up of several layers working together:

  1. Hardware — CPU, RAM, storage, network

  2. Kernel — core of the system; manages hardware communication

  3. Shell — interface for users to execute commands

  4. User Space — applications and utilities

When you run a command like:

cat file.txt

→ The shell interprets it → passes it to the kernel → retrieves data from the disk → displays the output.


💻 Navigating the Linux File System

📂 Basic Commands

TaskCommandDescription
Show current directorypwdDisplays full path of current location
List fileslsLists files and folders
Detailed listls -lShows file permissions, owner, and size
Include hidden filesls -aShows hidden files (those starting with .)
Change directorycd /pathMove to a specific folder
Move up one levelcd ..Goes one directory up
Go to home directorycd ~Returns to your home folder

💡 Tip: Combine commands with logical operators, for example:

cd /var/log && ls -l

This moves into /var/log and lists all files if the first command succeeds.


🧱 Working with Files and Directories

📄 Create, Move, Copy, Delete Files

touch newfile.txt # Create a new file
cp file.txt /tmp/ # Copy file to /tmp
mv file.txt docs/ # Move file to docs directory
rm oldfile.txt # Delete a file

📁 Directory Operations

mkdir backups # Create a new folder
rmdir emptydir # Remove empty folder
rm -rf old_project # Remove folder with contents (force)

💡 Be cautious with rm -rf — it permanently deletes files.


🖋️ Viewing and Editing Files

🧩 View File Contents

cat filename.txt # View entire file
head -n 10 filename.txt # Show first 10 lines
tail -n 10 filename.txt # Show last 10 lines
less filename.txt # Scroll through long files
grep "error" logfile.txt # Search for specific text

🧑‍💻 Create or Edit with VI Editor

vi notes.txt

Inside VI:

  • Press i → Insert mode

  • Type your content

  • Press Esc → Type :wq → Save and quit

💡 Shortcut: To quit without saving → :q!


📊 File Permissions and Ownership

Every file has permissions (read, write, execute) and an owner.

🔍 Check Permissions

ls -l

Example output:

-rwxr-xr-- 1 user group 512 Oct 10 script.sh

Meaning:

  • rwx (owner can read, write, execute)

  • r-x (group can read, execute)

  • r-- (others can read only)

🔧 Change Permissions

chmod 755 script.sh

🧑 Change Owner

sudo chown ubuntu:ubuntu script.sh

🧠 Resource Monitoring and Management

🧩 System Information

uname -a # Kernel and OS info
hostnamectl # System hostname details
whoami # Shows logged-in user

💾 Disk Usage

df -h # Human-readable disk usage
du -sh * # Folder sizes in current directory

🧮 Memory and CPU

free -h # Display RAM usage
top # Real-time CPU and memory stats
htop # (if installed) Interactive system monitor

🕒 System Uptime

uptime

Displays how long the system has been running and load averages.


🧰 Shell Scripting Basics

Shell scripting is about automating repetitive tasks — deployment, backups, monitoring, or system setup.

Example:

#!/bin/bash
echo "System Report"
date
echo "Current user: $(whoami)"
echo "Disk usage:"
df -h | grep '/dev/'

Run Your Script:

chmod +x system_report.sh
./system_report.sh

💡 Tip:

  • Use variables to store reusable values

  • Use loops and conditionals for logic

  • Combine multiple Linux commands with pipes (|) and redirection (>, >>)


🧭 Commonly Used Linux Shortcuts and Tricks

ShortcutFunction
Ctrl + CStop a running command
Ctrl + LClear the terminal
Ctrl + RSearch command history
!!Run the previous command
historyShow command history
alias ll='ls -alF'Create a shortcut command

⚙️ DevOps Use Cases for Linux

  • CI/CD Servers: Jenkins and GitLab runners run on Linux hosts

  • Containers: Docker containers use Linux namespaces and cgroups

  • Infrastructure: Ansible, Terraform, and Kubernetes rely on Linux command execution

  • Monitoring: Shell scripts automate log checks and alerts


🌟 Conclusion

Linux isn’t just an operating system — it’s the command center of DevOps.
By mastering file management, permissions, resource monitoring, and shell scripting, you gain control over automation and cloud infrastructure.

“In DevOps, Linux isn’t a skill — it’s a language every engineer must speak fluently.”

Comments

Popular posts from this blog

🧩 DevOps Day 1 — Fundamentals of DevOps

DevOps Day 23 — Multi-Stage Docker Builds & Distroless Images: Build Smaller, Safer Containers

🚀 DevOps Day 2 — Understanding the SDLC and the Role of DevOps Engineers