2025-02-05
Linux system administrators often rely on powerful commands to diagnose and manage servers efficiently. Below are five essential commands that can help streamline your workflow, along with practical examples and explanations.
- Filter Active Network Connections on a Specific Port
Command:
netstat -tulnp | grep ':22' | awk '{print $5, $7}'
Explanation:
– `netstat` lists active TCP and UDP connections.
– `grep` filters connections on port 22 (SSH).
– `awk` extracts and displays the remote IP address and corresponding process.
2. List Users with Active SSH Connections
Command:
who | awk '{print $1}' | sort | uniq -c | sort -nr
Explanation:
– `who` lists currently logged-in users.
– `awk` extracts usernames.
– `sort` and `uniq -c` count and sort the number of connections per user.
– `sort -nr` sorts the results in descending order.
3. Find Recently Modified Files by a User
Command:
find /home -type f -user utilisateur -mtime -7 -ls | awk '{print $7, $11}'
Explanation:
– `find` searches for files modified by a specific user in the last 7 days.
– `awk` displays the file size and path.
4. Monitor Failed SSH Login Attempts
Command:
grep 'Failed password' /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -nr | head -10
Explanation:
– `grep` searches for failed login attempts in the `auth.log` file.
– `awk` extracts the source IP addresses.
– `sort` and `uniq -c` count and sort the most frequent IPs.
– `head -10` displays the top 10 results.
5. Check Memory Usage by Process (Sorted)
Command:
ps aux | awk '{print $2, $4, $11}' | sort -k2nr | head -10
Explanation:
– `ps aux` lists all processes with memory usage.
– `awk` extracts the PID, memory usage (%), and command.
– `sort -k2nr` sorts processes by memory usage in descending order.
– `head -10` shows the top 10 memory-consuming processes.
What Undercode Say
Linux system administration is a critical skill for maintaining secure and efficient servers. The commands shared above are invaluable for diagnostics and administration. Here are additional commands and tips to enhance your Linux expertise:
1. Check Disk Usage:
df -h
Displays disk space usage in a human-readable format.
2. Monitor Real-Time System Activity:
top
Provides a dynamic view of system processes and resource usage.
3. **Search for Files by Name:**
find / -name "filename"
Searches the entire filesystem for a specific file.
4. Analyze Log Files in Real-Time:
tail -f /var/log/syslog
Monitors log files as they are updated.
5. Secure File Transfers with SCP:
scp file.txt user@remote:/path/to/destination
Transfers files securely between local and remote systems.
6. Check Network Connectivity:
ping google.com
Tests connectivity to a remote host.
7. List Open Files by a Process:
lsof -p
Displays files opened by a specific process.
8. Kill a Process by PID:
kill -9
Forcefully terminates a process.
9. Check System Uptime:
uptime
Shows how long the system has been running.
10. View Kernel Messages:
dmesg
Displays kernel ring buffer messages.
For further reading, explore these resources:
Mastering these commands and techniques will significantly improve your ability to manage and secure Linux systems. Whether you’re troubleshooting, monitoring, or optimizing, these tools are indispensable for any sysadmin.
Note: This article is written to provide practical, human-like insights into Linux system administration.
References:
Hackers Feeds, Undercode AI