Skip to content
Go back

Linux Fundamentals for DevOps

Edit page

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

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
# Reverse search through command history
Ctrl+R

# Execute command by history number
!123

# List files recursively
ls -R

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

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

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

Alternative Installation Methods

# Add PPA repository
sudo add-apt-repository ppa:repository-name

# Install snap package
sudo snap install packagename

Vim Text Editor

Essential for editing configuration files on servers:

Basic Operations

CommandAction
iEnter insert mode
EscExit insert mode
:wqSave and quit
:q!Quit without saving

Text Manipulation

CommandAction
ddDelete current line
d10dDelete 10 lines
uUndo last change
2uUndo last 2 changes
CommandAction
AGo to end of line (insert mode)
0Go to beginning of line
$Go to end of line
10GGo 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

User Accounts and Groups

User Types

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

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

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)

Changing Ownership

# Change owner and group
chown username:groupname filename

# Change only owner
chown username filename

# Change only group
chgrp groupname filename

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

Numeric Method

PermissionValue
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--

Pipes and Redirection

Standard Streams

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

Less Navigation

KeyAction
SpaceNext page
bPrevious page
qQuit

Shell Scripting Basics

Script Structure

#!/bin/bash

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

# Command substitution
config_files=$(ls config)

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

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"

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

Functions

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

# Call function
process_file "config.yaml"
result=$?  # Get return value

Environment Variables

Viewing Variables

# List all environment variables
printenv | less

# View specific variable
printenv USER
echo $USER

# Search for variables
printenv | grep USER

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

PATH Management

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

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

Networking Fundamentals

Network Components

IP Addressing

192.168.1.0/24
│          │
│          └─ Subnet mask (255.255.255.0)
└─ Network address

Network Services

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

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)
  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

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

SSH Security Best Practices


Edit page
Share this post on:

Previous Post
Version Control with Git