Linux Fundamentals & Essential Shell Commands for DevOps Engineers
Introduction to Linux
Linux is an open-source, Unix-like operating system used extensively in servers, cloud, and DevOps environments. It is stable, secure, and highly configurable, ideal for automation and infrastructure management.
Linux Kernel
The Linux kernel is the core of the OS that manages CPU, memory, and hardware resources. It provides system calls for applications and enforces security and permissions.
Types of Shells
- Bash (Bourne Again Shell) — Default, widely used, supports scripting.
- Sh (Bourne Shell) — Simple, older shell for scripting.
- Zsh (Z Shell) — Advanced shell with plugins and features.
- Ksh (Korn Shell) — Legacy scripting shell.
- Csh/Tcsh — C-like shell for scripting.
Use: Shells allow you to execute commands, write scripts, and automate tasks.
Linux Fundamentals
Filesystem Hierarchy
Linux uses a tree-like directory structure to organize files:
Use: Understanding directories helps navigate, manage, and configure Linux servers.
Users & Permissions
Permission Structure
--rwxr-xr-- 1 user group 4096 Oct 22 file.txt
│ │ │ │ │ │ │ │ │ └─ File name
│ │ │ │ │ │ │ │ └─ Last modification date
│ │ │ │ │ │ │ └─ File size in bytes
│ │ │ │ │ │ └─ Group owner
│ │ │ │ │ └─ User (owner)
│ │ │ │ └─ Number of hard links
│ │ │ └─ Other (world) permissions
│ │ └─ Group permissions
│ └─ Owner (user) permissions
└─ File type (- for file, d for directory, l for symlink)- Owner — File creator, controls access
- Group — Assigned group, shared access
- Other — Everyone else
Commands
whoami # Show current user; helps verify user privileges
id # Show UID, GID, and groups; useful for troubleshooting permissions
groups # Lists all groups user belongs to; ensures correct group membership
adduser user # Add a new user to the system
usermod -aG group user # Add user to a group for shared permissions
passwd user # Change password for a user
chmod 755 file # Set read/write/execute permissions for user/group/others
chown user:group file # Change file ownership and groupUse: These commands ensure security and proper access control on files and directories.
Switching Users and Getting Root Privileges
sudo -i # Start a root login shell using your sudo privileges; becomes root and loads root's environment
sudo -s # Start an interactive shell with root privileges; keeps your current environment
su - # Switch to the root user; requires the root account password
su - username # Switch to another user as a login shell; requires that user's password
sudo -u username -i # Start a login shell as another user using sudo privileges
sudo -u username <cmd> # Run a single command as another user (e.g., sudo -u postgres psql)
sudo !! # Re-run the previous shell command with sudo (convenient when you forgot to prefix sudo)Description: These commands allow you to gain root privileges or switch between users.
- Use
sudofor temporary privilege escalation (safer and logged). - Use
sufor direct user switching when you know the target user's password.
Processes
ps aux # List all running processes with details
top # Interactive real-time process monitoring
htop # Advanced process monitor with easier navigation
kill <PID> # Terminate a process safely using its PID
kill -9 <PID> # Forcefully terminate a process
pkill processname # Terminate a process by name
nice -n 10 command # Run a command with modified CPU priority
sudo kill -9 $(sudo lsof -t -i:<port_number>) # Find and forcefully kill all processes using the specified portUse: Process commands help monitor, manage, and troubleshoot running applications.
File and Directory Management
pwd # Show current directory path
ls -l # List files with details like permissions, owner, and size
cd /path # Change directory to a specified path
cd .. # Move one level up in directory
mkdir dir # Create a new directory
touch file # Create an empty file
cp file1 file2 # Copy files
mv file1 dir/ # Move files to a directory
rm file.txt # Delete a file
rm -rf dir/ # Delete a directory recursively
cat file.txt # Display entire file content
less file.txt # Scrollable view of file content
head -n 10 file # Show first 10 lines of file
tail -n 10 file # Show last 10 lines of file
find /home -name "*.log" # Search files by name pattern
tree /path # Display directory tree structureUse: These commands let you organize, inspect, and manipulate data and directories efficiently.
Package Management
Linux uses package managers to install, update, and remove software.
APT (Debian/Ubuntu)
sudo apt update # Refresh package lists
sudo apt upgrade # Upgrade installed packages
sudo apt install package_name # Install software
sudo apt remove package_name # Remove softwareYUM (RHEL/CentOS)
sudo yum update
sudo yum install package_name
sudo yum remove package_nameDNF (Fedora / RHEL 8+)
sudo dnf update
sudo dnf install package_name
sudo dnf remove package_nameSNAP (Universal packages)
sudo snap install package_name # Install snap package
sudo snap remove package_name # Remove snap package
sudo snap list # List installed snap packagesDPKG (Debian package tool)
dpkg -i package.deb # Install a .deb package
dpkg -r package_name # Remove a package
dpkg -l # List all installed packagesUse: Package management keeps software up-to-date and maintains the system tools needed for DevOps tasks.
Networking & Remote Access
ifconfig / ip addr # Display network interface configuration
ping 8.8.8.8 # Test connectivity to a host
netstat -tuln # List listening ports and services
ss -tulnp # Modern alternative to netstat
traceroute google.com # Trace network route to a host
curl -I http://example.com # Fetch HTTP headers from a URL
wget http://example.com/file # Download files from a URL
nc -zv host port # Check if a port is open
tcpdump -i eth0 # Capture and inspect network packetsSSH & SCP
ssh user@host # Connect to a remote host securely
ssh -i key_file user@host # Connect to a remote host securely with key
scp file user@host:/path # Copy file to a remote host
scp user@host:/path/file . # Copy file from remote hostKey-Based Login (SSH)
- Generate key pair on local machine:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"- Copy public key to server:
ssh-copy-id user@host- Set proper permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pubUse: Enables password-less, secure SSH authentication; essential for automation and secure server access.
Get Kirankunjumon’s stories in your inbox
Join Medium for free to get updates from this writer.
Use: These commands are essential for remote server management and secure file transfer.
System Services & Logs
Linux uses systemd for service and log management.
Systemctl (Services)
systemctl status service_name # Check service status
systemctl start service_name # Start service
systemctl stop service_name # Stop service
systemctl restart service_name # Restart service
systemctl enable service_name # Enable service at boot
systemctl disable service_name # Disable service at bootJournalctl (Logs)
journalctl -xe # View systemd logs with extra info
journalctl -u service # Logs of a specific service
journalctl -u <service> -n <number> -f # Show the last <number> log lines for the service and then follow (live) new entries
tail -f /var/log/syslog # Real-time system logs
less /var/log/auth.log # View authentication logs
grep "error" /var/log/syslog # Filter logs for errorsUse: These commands help monitor and troubleshoot services and system events.
Disk & Storage Management
df -h # Show disk usage in human-readable format
du -sh folder/ # Show size of a folder
lsblk # Display block devices and partitions
mount /dev/sdb1 /mnt/data # Mount a device
umount /mnt/data # Unmount a device
df -i # Show inode usage
ncdu # Interactive disk usage analyzerUse: These commands help manage storage, monitor disk space, and organize data efficiently.
Automation & Process Management
Cron Jobs
crontab -e # Edit user cron jobs
# Example: Run script every day at 3AM
0 3 * * * /home/user/script.shUse: Cron schedules automated tasks at specific times.
Supervisor
sudo systemctl start supervisor
sudo supervisorctl status
sudo supervisorctl start program_nameUse: Supervisor manages long-running processes and ensures they restart on failure.
System Monitoring & Performance
uptime # Show system load and uptime
free -h # Show memory usage in human-readable format
vmstat 1 # CPU, memory, I/O statistics updated every 1 sec
iostat # Show disk I/O statistics
df -h # Show disk usageUse: These commands track system performance to prevent overloads or resource issues.
Text Processing
grep "pattern" file # Search for a text pattern
awk '{print $1}' file # Extract specific column
sed 's/old/new/g' file # Replace text in file
cut -d',' -f1 file # Extract specific fields
sort file | uniq -c | sort -nr # Count duplicates
tr 'a-z' 'A-Z' < file # Convert text to uppercaseUse: Text commands filter, transform, and analyze logs or config files for automation and troubleshooting.









