DEV Community

Cover image for Linux Fundamentals for DevOps
Mohammad Ridowan Sikder
Mohammad Ridowan Sikder

Posted on

Linux Fundamentals for DevOps

This is the start of a series where I'll share everything I'm learning about DevOps technologies. Understanding the basics of Linux is essential for anyone in DevOps, so this guide will serve as the foundation for all the upcoming posts.

Linux File System Structure

Understanding the Linux filesystem hierarchy is crucial for DevOps work. Here's the essential directory structure:

/
├── home/          # User home directories
├── root/          # Root user's home directory
├── bin/           # Essential system binaries
├── sbin/          # System administration binaries
├── lib/           # Shared libraries for system programs
├── usr/           # User programs and data
│   ├── bin/       # User binaries
│   ├── sbin/      # Non-essential system binaries
│   ├── lib/       # Libraries for user programs
│   └── local/     # Locally installed software
├── etc/           # Configuration files
├── opt/           # Optional/third-party software
├── tmp/           # Temporary files
├── boot/          # Boot loader files
├── dev/           # Device files
├── var/           # Variable data (logs, cache, etc.)
│   ├── log/       # System and application logs
│   └── cache/     # Application cache data
├── media/         # Removable media mount points
└── mnt/           # Temporary mount points
Enter fullscreen mode Exit fullscreen mode

Why does /usr duplicate directories? Historical reasons. Originally, /usr was a separate partition for user data. Over time, it became a secondary hierarchy for non-essential programs.

Essential CLI Commands

Master these commands for efficient system navigation and administration:

System Information

# CPU information
lscpu

# Memory information
lsmem

# Complete system information
uname -a

# OS release information
cat /etc/os-release
Enter fullscreen mode Exit fullscreen mode

Navigation and History

# Reverse search through command history
Ctrl+R

# Execute command by history number
!123

# List files recursively
ls -R
Enter fullscreen mode Exit fullscreen mode

User Management

# Switch to specific user (with environment)
su - username

# Switch to root user
su -

# Add new user (interactive)
sudo adduser username

# Add new group
sudo addgroup groupname
Enter fullscreen mode Exit fullscreen mode

Package Management

Ubuntu uses multiple package management systems:

APT (Advanced Package Tool)

# Search for packages
apt search packagename

# Install package
sudo apt install packagename

# Remove package
sudo apt remove packagename

# Package sources configuration
/etc/apt/sources.list
Enter fullscreen mode Exit fullscreen mode

APT vs APT-GET: apt is newer, more user-friendly, and combines common apt-get and apt-cache functions.

Alternative Installation Methods

  • Ubuntu Software Center: GUI package manager
  • Snap packages: Universal Linux packages
  • PPA (Personal Package Archives): Third-party repositories
# Add PPA repository
sudo add-apt-repository ppa:repository-name

# Install snap package
sudo snap install packagename
Enter fullscreen mode Exit fullscreen mode

Vim Text Editor

Essential for editing configuration files on servers:

Basic Operations

Command Action
i Enter insert mode
Esc Exit insert mode
:wq Save and quit
:q! Quit without saving

Text Manipulation

Command Action
dd Delete current line
d10d Delete 10 lines
u Undo last change
2u Undo last 2 changes

Navigation

Command Action
A Go to end of line (insert mode)
0 Go to beginning of line
$ Go to end of line
10G Go to line 10

Search and Replace

/searchterm     # Search forward
n               # Next match
N               # Previous match

:%s/old/new     # Replace first occurrence in each line
:%s/old/new/g   # Replace all occurrences
:%s/old/new/gc  # Replace all with confirmation
Enter fullscreen mode Exit fullscreen mode

User Accounts and Groups

User Types

  • Root user: System administrator (UID 0)
  • Regular users: Standard user accounts (UID 1000+)
  • Service users: System service accounts (UID < 1000)

User Management Commands

# View user information
cat /etc/passwd

# Change password
passwd username

# Add user to group
usermod -aG groupname username

# View user's groups
groups username

# Remove user from group
sudo gpasswd -d username groupname
Enter fullscreen mode Exit fullscreen mode

Group Management

# View all groups
cat /etc/group

# Change user's primary group
usermod -g groupname username

# Add user to multiple groups
usermod username -G group1,group2
Enter fullscreen mode Exit fullscreen mode

File Ownership and Permissions

Understanding Permissions

ls -l filename
# Output: -rwxrw-r-- owner group
#         │││││││││
#         │││└┴┴┴┴┴─ Others permissions (r--)
#         ││└┴┴─────── Group permissions (rw-)
#         │└────────── Owner permissions (rwx)
#         └─────────── File type (- for file, d for directory)
Enter fullscreen mode Exit fullscreen mode

Changing Ownership

# Change owner and group
chown username:groupname filename

# Change only owner
chown username filename

# Change only group
chgrp groupname filename
Enter fullscreen mode Exit fullscreen mode

Changing Permissions

Symbolic Method

chmod u+x filename    # Add execute for owner
chmod g-w filename    # Remove write for group
chmod o+r filename    # Add read for others
chmod a+x filename    # Add execute for all
Enter fullscreen mode Exit fullscreen mode

Numeric Method

Permission Value
Read (r) 4
Write (w) 2
Execute (x) 1
# rwx = 4+2+1 = 7
chmod 755 filename    # rwxr-xr-x
chmod 644 filename    # rw-r--r--
Enter fullscreen mode Exit fullscreen mode

Pipes and Redirection

Standard Streams

  • stdin (0): Standard input
  • stdout (1): Standard output
  • stderr (2): Standard error

Common Operations

# Pipe output to less for pagination
history | less

# Search through command output
history | grep "sudo"

# Redirect output to file (overwrite)
history | grep sudo > commands.txt

# Redirect output to file (append)
history | grep sudo >> commands.txt
Enter fullscreen mode Exit fullscreen mode

Less Navigation

Key Action
Space Next page
b Previous page
q Quit

Shell Scripting Basics

Script Structure

#!/bin/bash

# Variables
file_name=config.yaml
echo "Processing $file_name"

# Command substitution
config_files=$(ls config)
Enter fullscreen mode Exit fullscreen mode

Conditional Statements

# Directory check
if [ -d "config" ]; then
    echo "Directory exists"
else
    echo "Creating directory"
    mkdir config
fi

# File check
if [ -f "config.yaml" ]; then
    echo "File exists"
fi

# Numeric comparisons: -eq, -ne, -gt, -ge, -lt, -le
if [ $num -eq 10 ]; then
    echo "Number is 10"
fi

# String comparisons
if [ "$var" == "string" ]; then
    echo "Strings match"
fi
Enter fullscreen mode Exit fullscreen mode

Script Parameters and User Input

# Script parameters
first_param=$1
second_param=$2
all_params=$*
param_count=$#

# User input
read -p "Enter value: " user_input
echo "You entered: $user_input"
Enter fullscreen mode Exit fullscreen mode

Loops

# For loop
for param in $*; do
    echo $param
done

# While loop
sum=0
while true; do
    read -p "Enter number (q to quit): " input
    if [ "$input" == "q" ]; then
        echo "Sum: $sum"
        break
    fi
    sum=$(($sum + $input))
done
Enter fullscreen mode Exit fullscreen mode

Functions

# Simple function
function process_file() {
    echo "Processing $1"
    return 0
}

# Call function
process_file "config.yaml"
result=$?  # Get return value
Enter fullscreen mode Exit fullscreen mode

Environment Variables

Viewing Variables

# List all environment variables
printenv | less

# View specific variable
printenv USER
echo $USER

# Search for variables
printenv | grep USER
Enter fullscreen mode Exit fullscreen mode

Setting Variables

# Temporary (current session only)
export MY_VAR="value"

# Permanent (user-specific)
echo 'export MY_VAR="value"' >> ~/.bashrc
source ~/.bashrc

# System-wide
sudo echo 'MY_VAR="value"' >> /etc/environment
Enter fullscreen mode Exit fullscreen mode

PATH Management

# Add directory to PATH
export PATH=$PATH:/custom/location

# Make permanent
echo 'export PATH=$PATH:/custom/location' >> ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Networking Fundamentals

Network Components

  • LAN: Local Area Network (switch)
  • WAN: Wide Area Network (router/gateway)
  • Subnet: Network segment with same IP range
  • CIDR: Classless Inter-Domain Routing notation

IP Addressing

192.168.1.0/24
│          │
│          └─ Subnet mask (255.255.255.0)
└─ Network address
Enter fullscreen mode Exit fullscreen mode

Network Services

  • NAT: Network Address Translation
  • DNS: Domain Name System
  • Firewall: Network security filtering
  • Port: Communication endpoint

Network Commands

# Network interface information
ifconfig
ip addr show

# Network connections
netstat -tulpn

# DNS lookup
nslookup domain.com

# Test connectivity
ping hostname

# Running processes
ps aux
Enter fullscreen mode Exit fullscreen mode

SSH (Secure Shell)

Key-Based Authentication Setup

  1. Generate key pair on client:
ssh-keygen -t rsa
# Creates ~/.ssh/id_rsa (private) and ~/.ssh/id_rsa.pub (public)
Enter fullscreen mode Exit fullscreen mode
  1. Copy public key to server:
# Method 1: Manual copy
cat ~/.ssh/id_rsa.pub
# Paste into ~/.ssh/authorized_keys on server

# Method 2: Using ssh-copy-id
ssh-copy-id username@hostname
Enter fullscreen mode Exit fullscreen mode

SSH Usage

# Connect with password
ssh username@hostname

# Connect with specific key
ssh -i ~/.ssh/id_rsa username@hostname

# Copy files over SSH
scp localfile username@hostname:/remote/path
scp username@hostname:/remote/file ./local/path
Enter fullscreen mode Exit fullscreen mode

SSH Security Best Practices

  • Use key-based authentication over passwords
  • Keep private keys secure (600 permissions)
  • Use strong passphrases for keys
  • Disable root SSH access in production

Top comments (0)