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
- File and Directory Management
- User and Permission Management
- Process Management
- Networking Commands
- Disk and Storage Management
- System Monitoring and Performance Tuning
- Package Management
- Logging and Troubleshooting
- Automation and Scheduling
1. File and Directory Management
Print Working Directory
pwd
- Displays the current working directory.
List Files and Directories
ls -lah
-
-l
: Long listing format. -
-a
: Show hidden files. -
-h
: Human-readable file sizes.
Change Directory
cd /path/to/directory
- Move into a specified directory.
Create a Directory
mkdir new_directory
- Creates a new directory.
Create an Empty File
touch filename
- Creates an empty file.
Write to a File
echo 'Hello, World!' > filename
- Writes 'Hello, World!' into a file (overwrites content).
Append Text to a File
echo 'New Line' >> filename
- Appends 'New Line' to an existing file.
Open a File for Editing
nano filename
- Opens the file in the nano text editor.
Remove a File or Directory
rm -rf file_or_directory
-
-r
: Remove directories and their contents. -
-f
: Force delete without confirmation.
Copy and Move Files
cp source_file destination
mv source_file destination
-
cp
: Copies a file or directory. -
mv
: Moves (or renames) a file or directory.
2. User and Permission Management
Check Current User
whoami
- Displays the current logged-in user.
Add a New User
sudo useradd -m username
-
-m
: Creates a home directory for the user.
Change File Permissions
chmod 755 filename
-
7
: Read, write, and execute for the owner. -
5
: Read and execute for group and others.
Change File Ownership
sudo chown user:group filename
-
chown
: Change ownership of a file or directory.
3. Process Management
View Running Processes
ps aux
- Displays all running processes.
Kill a Process
kill -9 process_id
-
-9
: Forcefully terminates a process.
Monitor System in Real Time
top
- Displays system resource usage.
4. Networking Commands
Check Network Interfaces
ip a
- Displays IP addresses and network interfaces.
Test Connectivity
ping google.com
- Sends ICMP echo requests to check connectivity.
Check Open Ports
netstat -tulnp
- Lists open ports and their associated processes.
5. Disk and Storage Management
Check Disk Usage
df -h
- Displays disk space usage in a human-readable format.
Check Directory Size
du -sh /path/to/directory
-
-s
: Summarizes the total size. -
-h
: Human-readable format.
6. System Monitoring and Performance Tuning
View System Uptime
uptime
- Shows how long the system has been running.
Check Memory Usage
free -m
- Displays memory usage in megabytes.
Monitor Logs in Real Time
tail -f /var/log/syslog
- Displays live updates from a log file.
7. Package Management
Install a Package (Debian-based)
sudo apt install package_name
Install a Package (Red Hat-based)
sudo yum install package_name
Remove a Package
sudo apt remove package_name
8. Logging and Troubleshooting
Check System Logs
journalctl -xe
- Displays system logs with detailed errors.
View Authentication Logs
cat /var/log/auth.log
- Shows login attempts and authentication logs.
9. Automation and Scheduling
Schedule a Cron Job
crontab -e
- Opens the crontab file for scheduling tasks.
Example Cron Job (Runs every day at 2 AM):
0 2 * * * /path/to/script.sh
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)
Nice, a blog this covers basic linux commands which a DevOps Engineer should know