DEV Community

kristarking
kristarking

Posted on

The Ultimate Linux Command Guide for DevOps Engineers (With Examples!)

As a DevOps engineer, mastering Linux commands is crucial for managing servers, automating deployments, troubleshooting issues, and handling infrastructure efficiently. This guide covers essential Linux commands that every DevOps engineer should know.

Table of Contents

  1. File and Directory Management
  2. User and Permission Management
  3. Process Management
  4. Networking Commands
  5. Disk and Storage Management
  6. System Monitoring and Performance Tuning
  7. Package Management
  8. Logging and Troubleshooting
  9. Automation and Scheduling

1. File and Directory Management

Print Working Directory

pwd
Enter fullscreen mode Exit fullscreen mode
  • Displays the current working directory.

List Files and Directories

ls -lah
Enter fullscreen mode Exit fullscreen mode
  • -l: Long listing format.
  • -a: Show hidden files.
  • -h: Human-readable file sizes.

Change Directory

cd /path/to/directory
Enter fullscreen mode Exit fullscreen mode
  • Move into a specified directory.

Create a Directory

mkdir new_directory
Enter fullscreen mode Exit fullscreen mode
  • Creates a new directory.

Create an Empty File

touch filename
Enter fullscreen mode Exit fullscreen mode
  • Creates an empty file.

Write to a File

echo 'Hello, World!' > filename
Enter fullscreen mode Exit fullscreen mode
  • Writes 'Hello, World!' into a file (overwrites content).

Append Text to a File

echo 'New Line' >> filename
Enter fullscreen mode Exit fullscreen mode
  • Appends 'New Line' to an existing file.

Open a File for Editing

nano filename
Enter fullscreen mode Exit fullscreen mode
  • Opens the file in the nano text editor.

Remove a File or Directory

rm -rf file_or_directory
Enter fullscreen mode Exit fullscreen mode
  • -r: Remove directories and their contents.
  • -f: Force delete without confirmation.

Copy and Move Files

cp source_file destination
mv source_file destination
Enter fullscreen mode Exit fullscreen mode
  • cp: Copies a file or directory.
  • mv: Moves (or renames) a file or directory.

2. User and Permission Management

Check Current User

whoami
Enter fullscreen mode Exit fullscreen mode
  • Displays the current logged-in user.

Add a New User

sudo useradd -m username
Enter fullscreen mode Exit fullscreen mode
  • -m: Creates a home directory for the user.

Change File Permissions

chmod 755 filename
Enter fullscreen mode Exit fullscreen mode
  • 7: Read, write, and execute for the owner.
  • 5: Read and execute for group and others.

Change File Ownership

sudo chown user:group filename
Enter fullscreen mode Exit fullscreen mode
  • chown: Change ownership of a file or directory.

3. Process Management

View Running Processes

ps aux
Enter fullscreen mode Exit fullscreen mode
  • Displays all running processes.

Kill a Process

kill -9 process_id
Enter fullscreen mode Exit fullscreen mode
  • -9: Forcefully terminates a process.

Monitor System in Real Time

top
Enter fullscreen mode Exit fullscreen mode
  • Displays system resource usage.

4. Networking Commands

Check Network Interfaces

ip a
Enter fullscreen mode Exit fullscreen mode
  • Displays IP addresses and network interfaces.

Test Connectivity

ping google.com
Enter fullscreen mode Exit fullscreen mode
  • Sends ICMP echo requests to check connectivity.

Check Open Ports

netstat -tulnp
Enter fullscreen mode Exit fullscreen mode
  • Lists open ports and their associated processes.

5. Disk and Storage Management

Check Disk Usage

df -h
Enter fullscreen mode Exit fullscreen mode
  • Displays disk space usage in a human-readable format.

Check Directory Size

du -sh /path/to/directory
Enter fullscreen mode Exit fullscreen mode
  • -s: Summarizes the total size.
  • -h: Human-readable format.

6. System Monitoring and Performance Tuning

View System Uptime

uptime
Enter fullscreen mode Exit fullscreen mode
  • Shows how long the system has been running.

Check Memory Usage

free -m
Enter fullscreen mode Exit fullscreen mode
  • Displays memory usage in megabytes.

Monitor Logs in Real Time

tail -f /var/log/syslog
Enter fullscreen mode Exit fullscreen mode
  • Displays live updates from a log file.

7. Package Management

Install a Package (Debian-based)

sudo apt install package_name
Enter fullscreen mode Exit fullscreen mode

Install a Package (Red Hat-based)

sudo yum install package_name
Enter fullscreen mode Exit fullscreen mode

Remove a Package

sudo apt remove package_name
Enter fullscreen mode Exit fullscreen mode

8. Logging and Troubleshooting

Check System Logs

journalctl -xe
Enter fullscreen mode Exit fullscreen mode
  • Displays system logs with detailed errors.

View Authentication Logs

cat /var/log/auth.log
Enter fullscreen mode Exit fullscreen mode
  • Shows login attempts and authentication logs.

9. Automation and Scheduling

Schedule a Cron Job

crontab -e
Enter fullscreen mode Exit fullscreen mode
  • Opens the crontab file for scheduling tasks.

Example Cron Job (Runs every day at 2 AM):

0 2 * * * /path/to/script.sh
Enter fullscreen mode Exit fullscreen mode

Conclusion

Mastering these Linux commands will significantly improve your efficiency as a DevOps engineer. Whether managing servers, troubleshooting issues, or automating tasks, these commands form the foundation of your daily operations.

Which command do you use the most? Let me know in the comments!

Top comments (1)

Collapse
 
soram profile image
Soram Varma

Nice, a blog this covers basic linux commands which a DevOps Engineer should know