Top 10 Linux Tips Worth Knowing
Advanced System Monitoring and Process Management
Effective system administration requires more than just knowing basic commands. Understanding how processes interact with kernel resources is paramount. Here are some advanced techniques.
1. Mastering Process Inspection with 'ps' and Signals
While top is useful, the combination of ps auxfww and signal handling provides deeper insight. Use ps -ef --ppid | grep to trace process lineage. Furthermore, understanding signals (like SIGTERM vs. SIGKILL) is crucial for graceful service shutdowns. Always attempt a controlled termination via kill -TERM before resorting to forceful kills.
Advanced Networking and Connectivity Diagnosis
Modern Linux distributions have moved away from the legacy tools like ifconfig. Utilizing the newer, more comprehensive utilities provides accurate real-time network data.
2. Using 'ip' and 'ss' for Network Diagnostics
- Network Interface Status: Always use
ip a(orip addr show) to view interface details, including MAC addresses and assigned IP ranges. - Socket Inspection: Replace
netstatwithss -tulnpa. The 's' utility is faster and provides richer output, allowing you to quickly identify which process (via PID/Program Name) is listening on a specific port or socket type.
The Power of Pipes: Grep, Sed, and Awk
These three utilities form the backbone of Linux scripting. Understanding how they chain together via pipes (|) allows for complex data manipulation without writing extensive scripts.
3. Advanced Data Filtering with 'grep'
Do not limit yourself to simple string searches. Use advanced regex anchors (^ for start, $ for end) and grouping/alternation ((A|B)). For example, searching only lines that contain a specific pattern at the beginning AND the end requires careful use of lookaheads or multiple pipes.
4. Stream Editing with 'sed'
sed is an indispensable stream editor. Instead of just substitution (s/old/new/g), master address ranges and conditional execution. Examples include performing substitutions only on lines 10 through 20, or using the b command to branch logic within the script.
Shell Scripting Best Practices
Writing robust shell scripts requires discipline regarding error handling and variable scope.
5. Implementing Robust Error Trapping
Always wrap critical sections of your script using set -e (exit immediately if any command fails) and utilize the trap command. A proper trap EXIT TERM INT block ensures that cleanup tasks, such as deleting temporary files or releasing locks, execute even if the script is interrupted.
6. Utilizing Associative Arrays
For scripts handling configuration data (like IP-to-Port mappings), associative arrays are far superior to simple variable assignments. They allow you to treat keys and values like a dictionary structure within Bash.
System Service Management with systemd
The modern standard for managing services is systemd. Learning to write proper Unit Files (Service, Target, Socket) is critical for professional deployment.
7. Creating Custom Systemd Units
- Type Selection: Understand the difference between
Type=simple(basic process),Type=forking(for legacy daemons that fork), andType=oneshot(for scripts that run once). - Dependencies: Use
Requires(hard dependency) andAfter(ordering dependency) directives to guarantee services start in the correct sequence, preventing race conditions.
Resource Management Techniques
Understanding what consumes resources is as important as managing them.
8. Disk Usage Analysis: Beyond 'df'
While df -h shows filesystem space, use the du -sh * command to analyze directory consumption. For inode usage (which can be a silent killer), use find /path/to/dir -inode | wc -l to count available inodes separately from block usage.
9. Memory and Cache Management
Use the free -h output carefully. The 'Available' column is more indicative of usable memory than simply subtracting used RAM from total RAM, as it accounts for reclaimable cache space.
Shell Optimization and Globbing
These tips focus on making your command line interactions faster and less error-prone.
10. Mastering Command History and Parameter Expansion
- History Search: Use the
!modifier (e.g.,!$to repeat the last argument) or utilize `history | grep` for complex recall. - Parameter Expansion: Instead of writing lengthy conditional logic, use parameter expansion like
${variable:-default_value}(use default if variable is unset/null) or${variable//pattern/replacement}(global string replacement within a variable). This drastically cleans up scripting code.