DevOps 8 min read

50 Essential Linux Commands Every DevOps Engineer Must Know

Master the most important Linux commands for DevOps. From file operations to process management, networking, and system administration with practical examples.

MR

Moshiour Rahman

Advertisement

Why Linux Commands Matter for DevOps

As a DevOps engineer, you’ll spend significant time in the terminal. Mastering these commands will make you more efficient at managing servers, debugging issues, and automating tasks.

File and Directory Operations

# Print working directory
pwd

# List files with details
ls -la

# List with human-readable sizes
ls -lh

# List sorted by modification time
ls -lt

# Change directory
cd /var/log
cd ~          # Home directory
cd -          # Previous directory
cd ..         # Parent directory

# Create directory
mkdir mydir
mkdir -p parent/child/grandchild  # Create nested directories

# Remove directory
rmdir emptydir
rm -rf mydir  # Force remove (use carefully!)

File Operations

# Create empty file
touch newfile.txt

# Copy files
cp source.txt destination.txt
cp -r sourcedir/ destdir/  # Copy directory recursively

# Move/Rename
mv oldname.txt newname.txt
mv file.txt /new/location/

# Remove files
rm file.txt
rm -f file.txt     # Force remove
rm -rf directory/  # Remove directory and contents

# View file contents
cat file.txt
less file.txt      # Paginated view (q to quit)
head -n 20 file.txt  # First 20 lines
tail -n 20 file.txt  # Last 20 lines
tail -f /var/log/syslog  # Follow file in real-time

File Permissions

# Change permissions
chmod 755 script.sh   # rwxr-xr-x
chmod +x script.sh    # Add execute permission
chmod u+w file.txt    # Add write for user

# Permission numbers:
# 4 = read (r)
# 2 = write (w)
# 1 = execute (x)
# 755 = rwxr-xr-x
# 644 = rw-r--r--

# Change ownership
chown user:group file.txt
chown -R user:group directory/

Text Processing

Search and Filter

# Search in files
grep "error" logfile.txt
grep -i "error" logfile.txt    # Case insensitive
grep -r "TODO" ./src/          # Recursive search
grep -n "pattern" file.txt     # Show line numbers
grep -v "exclude" file.txt     # Invert match
grep -E "error|warning" file   # Extended regex (OR)
grep -c "pattern" file.txt     # Count matches

# Find files
find /var/log -name "*.log"
find . -type f -mtime -7       # Modified in last 7 days
find . -type f -size +100M     # Files larger than 100MB
find . -name "*.tmp" -delete   # Find and delete

# Find and execute
find . -name "*.sh" -exec chmod +x {} \;

Text Manipulation

# Sort
sort file.txt
sort -n numbers.txt    # Numeric sort
sort -r file.txt       # Reverse sort
sort -u file.txt       # Unique lines only

# Remove duplicates
uniq file.txt
sort file.txt | uniq   # Sort first for accurate results

# Count lines, words, characters
wc -l file.txt         # Line count
wc -w file.txt         # Word count
wc -c file.txt         # Character count

# Cut columns
cut -d',' -f1,3 data.csv  # Fields 1 and 3, comma delimiter
cut -c1-10 file.txt       # Characters 1-10

# Stream editor
sed 's/old/new/g' file.txt           # Replace all occurrences
sed -i 's/old/new/g' file.txt        # Edit in place
sed -n '10,20p' file.txt             # Print lines 10-20
sed '/pattern/d' file.txt            # Delete matching lines

# AWK for column processing
awk '{print $1, $3}' file.txt        # Print columns 1 and 3
awk -F',' '{print $2}' data.csv      # Comma separator
awk '$3 > 100 {print $1}' file.txt   # Conditional print
awk '{sum += $1} END {print sum}'    # Sum column

Process Management

# List processes
ps aux                  # All processes
ps aux | grep nginx     # Filter by name
ps -ef --forest         # Process tree

# Real-time process viewer
top
htop                    # Better alternative (install first)

# Background processes
command &               # Run in background
jobs                    # List background jobs
fg %1                   # Bring job 1 to foreground
bg %1                   # Resume job 1 in background

# Kill processes
kill PID                # Graceful termination
kill -9 PID             # Force kill
killall nginx           # Kill by name
pkill -f "pattern"      # Kill by pattern

# Process priority
nice -n 10 command      # Run with lower priority
renice -n 5 -p PID      # Change priority of running process

System Information

# System info
uname -a                # All system info
hostname                # Server hostname
uptime                  # System uptime and load

# Hardware info
lscpu                   # CPU information
free -h                 # Memory usage
df -h                   # Disk usage
du -sh /var/*           # Directory sizes
lsblk                   # Block devices

# OS info
cat /etc/os-release
cat /etc/*release

# Kernel info
uname -r                # Kernel version
dmesg | tail            # Kernel messages

Networking

Network Configuration

# IP addresses
ip addr show
ip a                    # Shorthand
ifconfig                # Legacy command

# Routing
ip route show
route -n                # Routing table

# DNS
cat /etc/resolv.conf
nslookup google.com
dig google.com

# Hostname
hostnamectl
hostnamectl set-hostname newname

Network Diagnostics

# Connectivity
ping google.com
ping -c 4 192.168.1.1   # 4 packets only

# Trace route
traceroute google.com
mtr google.com          # Better traceroute

# Port checking
netstat -tulpn          # Listening ports
ss -tulpn               # Modern alternative
lsof -i :80             # What's using port 80

# Test connectivity
telnet hostname 80
nc -zv hostname 80      # Netcat
curl -I https://example.com  # HTTP headers only

# Download
wget https://example.com/file.zip
curl -O https://example.com/file.zip
curl -s https://api.example.com/data | jq  # JSON API

Firewall

# UFW (Ubuntu)
ufw status
ufw allow 22
ufw allow 80/tcp
ufw enable

# Firewalld (CentOS/RHEL)
firewall-cmd --list-all
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

# iptables (low-level)
iptables -L -n
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

User Management

# User info
whoami
id
id username

# Add user
useradd -m -s /bin/bash username
passwd username

# Modify user
usermod -aG docker username    # Add to group
usermod -s /bin/zsh username   # Change shell

# Delete user
userdel -r username            # Remove with home dir

# Groups
groups username
groupadd mygroup
gpasswd -a user group          # Add user to group

# Switch user
su - username
sudo -u username command
sudo -i                        # Root shell

Package Management

Debian/Ubuntu (apt)

apt update                     # Update package list
apt upgrade                    # Upgrade packages
apt install nginx              # Install package
apt remove nginx               # Remove package
apt autoremove                 # Remove unused packages
apt search keyword             # Search packages
apt show nginx                 # Package info

RHEL/CentOS (dnf/yum)

dnf update                     # Update packages
dnf install nginx              # Install
dnf remove nginx               # Remove
dnf search keyword             # Search
dnf info nginx                 # Package info

Service Management (systemd)

# Service control
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx         # Reload config without restart
systemctl status nginx

# Enable/Disable on boot
systemctl enable nginx
systemctl disable nginx

# Check if enabled
systemctl is-enabled nginx

# List services
systemctl list-units --type=service
systemctl list-units --type=service --state=running

# View logs
journalctl -u nginx
journalctl -u nginx -f         # Follow logs
journalctl -u nginx --since "1 hour ago"
journalctl -u nginx --since today

Disk and Storage

# Disk usage
df -h                          # Filesystem usage
du -sh /var/*                  # Directory sizes
du -h --max-depth=1            # One level deep

# Disk management
fdisk -l                       # List disks
lsblk                          # Block devices
mount /dev/sdb1 /mnt           # Mount drive
umount /mnt                    # Unmount

# Disk health
smartctl -a /dev/sda           # SMART data

# LVM
pvs                            # Physical volumes
vgs                            # Volume groups
lvs                            # Logical volumes

Compression and Archives

# tar archives
tar -cvf archive.tar files/    # Create
tar -xvf archive.tar           # Extract
tar -tvf archive.tar           # List contents

# tar with gzip
tar -czvf archive.tar.gz files/
tar -xzvf archive.tar.gz

# tar with bzip2
tar -cjvf archive.tar.bz2 files/
tar -xjvf archive.tar.bz2

# zip
zip -r archive.zip directory/
unzip archive.zip
unzip -l archive.zip           # List contents

# gzip
gzip file.txt                  # Compress (replaces original)
gunzip file.txt.gz             # Decompress

SSH and Remote Access

# Connect to remote server
ssh user@hostname
ssh -p 2222 user@hostname      # Custom port

# SSH key generation
ssh-keygen -t ed25519 -C "email@example.com"
ssh-keygen -t rsa -b 4096

# Copy SSH key to server
ssh-copy-id user@hostname

# SCP file transfer
scp file.txt user@host:/path/
scp -r directory/ user@host:/path/
scp user@host:/path/file.txt ./

# SSH config (~/.ssh/config)
# Host myserver
#     HostName 192.168.1.100
#     User admin
#     Port 22
#     IdentityFile ~/.ssh/mykey

# Then connect with: ssh myserver

# SSH tunneling
ssh -L 8080:localhost:80 user@host  # Local forward
ssh -R 8080:localhost:80 user@host  # Remote forward

Quick Reference Table

CategoryCommandPurpose
Filesls -laList all files
Filesfind . -name "*.log"Find files
Textgrep -r "error" .Search text
Textsed 's/old/new/g'Replace text
Processps auxList processes
Processkill -9 PIDForce kill
Networkss -tulpnListening ports
Networkcurl -I URLHTTP headers
Systemdf -hDisk usage
Systemfree -hMemory usage
Servicesystemctl statusService status
Servicejournalctl -fFollow logs

Summary

These commands form the foundation of Linux system administration. Practice them regularly, and you’ll become efficient at managing servers and troubleshooting issues. Consider creating aliases for frequently used commands:

# Add to ~/.bashrc
alias ll='ls -la'
alias grep='grep --color=auto'
alias df='df -h'
alias free='free -h'
alias ports='ss -tulpn'

Master these commands, and you’ll handle most DevOps tasks with confidence.

Advertisement

MR

Moshiour Rahman

Software Architect & AI Engineer

Share:
MR

Moshiour Rahman

Software Architect & AI Engineer

Enterprise software architect with deep expertise in financial systems, distributed architecture, and AI-powered applications. Building large-scale systems at Fortune 500 companies. Specializing in LLM orchestration, multi-agent systems, and cloud-native solutions. I share battle-tested patterns from real enterprise projects.

Related Articles

Comments

Comments are powered by GitHub Discussions.

Configure Giscus at giscus.app to enable comments.