Linux Commands Cheat Sheet for DevOps Engineers
1.System Information
lsb_release -a :Displays Ubuntu version, codename, and release details.
uname -a :Shows kernel version, hostname, architecture, and more.
cat /etc/os-release :Provides OS name, version, and other metadata.
free -h :Shows memory (RAM) usage in a human-readable format.
df -h :Displays disk space usage.
hostnamectl :Displays hostname, OS, and virtualization details.
uptime :Shows how long the system has been running and load averages.
2.Folder Structure
/boot :Stores files needed for booting the system (not relevant in containers).
/usr :Contains most user-installed applications and libraries.
/var :Stores logs, caches, and temporary files that change frequently.
/etc :Stores system configuration files.
User & Application-Specific Directories
/home :Default location for user home directories.
/opt :Used for installing optional third-party software.
/srv :Holds data for services like web servers (rarely used in containers).
/root :Home directory for the root user.
Temporary & Volatile Directories
/tmp :Temporary files (cleared on reboot).
/run :Holds runtime data for processes.
/proc :Virtual filesystem for process and system information.
/sys :Virtual filesystem for hardware and kernel information.
/dev :Contains device files (e.g., /dev/null, /dev/sda).
Mount Points
/mnt :Temporary mount point for external filesystems.
/media :Mount point for removable media (USB, CDs).
/data :Likely your mounted volume from Windows (C:/ubuntu-data).
3.User Management in Ubuntu
/etc/passwd :Stores user account details.
/etc/shadow :Stores encrypted user passwords.
/etc/group :Stores group information.
/etc/gshadow :Stores secure group details.
sudo adduser devops :To create a new user
passwd username :To set or change a user’s password
useradd -m username :To create a user with a home directory
useradd -s /bin/bash username :To specify a shell
chage -M 90 username :Set password expiry days
passwd -l username :Lock a user account
passwd -u username :Unlock a user account
Modifying Users
usermod -l new_username old_username :Change the username
usermod -d /new/home/directory -m username :Change the home directory
usermod -s /bin/zsh username :Change the default shell
Deleting Users
userdel username :To remove a user but keep their home directory
userdel -r username :To remove a user and their home directory
Working with Groups
groupadd groupname :Creating Groups
usermod -aG groupname username :Adding Users to Groups
groups username :Viewing Group Memberships
usermod -g new_primary_group username :Changing Primary Group
Sudo Access and Privilege Escalation
usermod -aG sudo username
Granting Specific Commands with Sudo
visudo
username ALL=(ALL) NOPASSWD: /path/to/command
4.File and Directory Management
ls – Lists files and directories in the current location.
cd /path/to/directory – Changes the working directory.
pwd – Prints the current working directory.
mkdir new_folder – Creates a new directory.
rmdir empty_folder – Removes an empty directory.
rm file.txt – Deletes a file.
rm -r folder – Deletes a folder and its contents.
cp file1.txt file2.txt – Copies a file.
cp -r dir1 dir2 – Copies a directory recursively.
mv old_name new_name – Moves or renames a file or directory.
File Viewing and Editing
cat file.txt – Displays file content.
tac file.txt – Displays file content in reverse order.
less file.txt – Opens a file for viewing with scrolling support.
more file.txt – Similar to less, but only moves forward.
head -n 10 file.txt – Displays the first 10 lines of a file.
tail -n 10 file.txt – Displays the last 10 lines of a file.
nano file.txt – Opens a simple text editor.
vi file.txt – Opens a powerful text editor.
echo 'Hello' > file.txt – Writes text to a file, overwriting existing content.
echo 'Hello' >> file.txt – Appends text to a file without overwriting.
5.File Permissions Management
Introduction to File Permissions
Owner (User): The creator of the file.
Group: Users belonging to the assigned group.
Others: All other users on the system.
Permissions are represented as:
Read (r or 4) – View file contents.
Write (w or 2) – Modify file contents.
Execute (x or 1) – Run scripts or programs.
To check file permissions, use:
ls -l filename
Changing Permissions with chmod
Modify permissions using symbols:
Add (+), remove (-), or set (=) permissions.
chmod u+x filename # Add execute for user
chmod g-w filename # Remove write for group
chmod o=r filename # Set read-only for others
chmod u=rwx,g=rx,o= filename # Set full access for user, read/execute for group, and no access for
others
Using Numeric (Octal) Mode
Each permission has a value:
Read (4), Write (2), Execute (1).
chmod 755 filename # User (rwx), Group (r-x), Others (r-x)
chmod 644 filename # User (rw-), Group (r--), Others (r--)
chmod 700 filename # User (rwx), No access for others
Changing Ownership with chown
Modify file owner and group:
chown newuser filename # Change owner
chown newuser:newgroup filename # Change owner and group
chown :newgroup filename # Change only group
Recursively change ownership:
chown -R newuser:newgroup directory/
Changing Group Ownership with chgrp
chgrp newgroup filename # Change group
chgrp -R newgroup directory/ # Change group recursively
Special Permissions
chmod u+s filename : Allows users to run a file with the file owner's permissions
SetGID (s on group execute bit)
chmod g+s filename # Set on file
chmod g+s directory/ # Set on directory
Sticky Bit (t on others execute bit)
chmod +t directory/
6.Process Management in Ubuntu
Viewing Processes
ps aux – View all running processes
ps -u username – View processes for a specific user
ps -C processname – Show a process by name
pgrep processname – Find a process by name and return its PID
pidof processname – Find the PID of a running program
Monitoring System Processes
top – Interactive process viewer
htop – User-friendly process viewer (requires installation)
nice -n 10 command – Run a command with a specific priority
renice -n -5 -p PID – Change priority of an existing process
Daemon Process Management
systemctl list-units --type=service – List all system daemons
systemctl start service-name – Start a daemon/service
systemctl stop service-name – Stop a daemon/service
systemctl enable service-name – Enable a service at startup
Viewing Process Details
ps -u username :Show processes for a specific user
ps -C processname :Show a process by name
Using pgrep
pgrep processname :Find a process by name and return its PID
Using pidof
pidof processname :Find the PID of a running program
Managing Processes
kill PID :To terminate a process by PID
pkill processname :To terminate using process name
kill -9 PID :Force kill a process
pkill -9 processname :Kill all instances of a process
Stopping & Resuming Processes
kill -STOP PID :Stop a running process
kill -CONT PID :Resume a stopped process
Monitoring System Processes
Interactive process viewer:
Press k and enter a PID to kill a process.
Press r to renice a process.
Press q to quit.
Daemon Processes
systemctl list-units --type=service :Daemon processes run in the background without user
intervention. List all system daemons
systemctl start service-name :Stop a daemon
systemctl enable service-name :Enable a service at startup
7.System Monitoring
CPU and Memory Monitoring
top – Real-time system monitoring
htop – Interactive process viewer (requires installation)
vmstat – Report system performance statistics
free -m – Show memory usage
Disk Monitoring
df -h – Check disk space usage
du -sh /path – Show disk usage of a specific directory
iostat – Display CPU and disk I/O statistics
Network Monitoring
ifconfig – Show network interfaces (deprecated, use ip a)
ip a – Show network interface details
netstat -tulnp – Show active connections and listening ports
ss -tulnp – Alternative to netstat for socket statistics
ping hostname – Test network connectivity
traceroute hostname – Show network path to a host
nslookup domain – Get DNS resolution details
Log Monitoring
tail -f /var/log/syslog – Live monitoring of system logs
journalctl -f – Live system logs for systemd-based distros
dmesg | tail – View kernel logs
Network Monitoring
Checking Network Interfaces
ip a # Show IP addresses and interfaces
Viewing Open Ports and Connections
netstat -tulnp # Show listening ports
ss -tulnp # Alternative to netstat
Testing Connectivity
ping google.com # Test internet connection
traceroute google.com # Trace the path to Google
Checking DNS Resolution
nslookup example.com
Log Monitoring
Live Monitoring of System Logs
tail -f /var/log/syslog # Follow logs in real-time
journalctl -f # Systemd logs
Checking Kernel Logs
dmesg | tail