Unix offers a powerful suite of commands for managing system performance. Whether handling resource allocation, controlling processes, or analyzing disk usage, the right tools can significantly improve efficiency. Below is a structured breakdown of key Unix commands that help maintain an optimized system.
Monitoring System Resources
Keeping track of system load, memory usage, and CPU performance is essential for preventing slowdowns.
1. Checking System Load
uptime
– Displays system uptime and load averages.w
– Shows who is logged in and their activity.top
– Provides real-time information on processes, memory, and CPU usage.htop
– A more user-friendly alternative totop
, offering interactive process management.
2. Monitoring CPU Usage
mpstat -P ALL 1
– Displays CPU usage per core every second.sar -u 1 10
– Shows CPU usage statistics at one-second intervals for ten cycles.
3. Checking Memory Usage
free -m
– Displays memory and swap usage in megabytes.vmstat 1 10
– Reports memory, CPU, and I/O statistics every second for ten cycles.
Managing Processes Efficiently
Handling processes properly ensures smooth operation and prevents resource-hungry applications from slowing down the system.
4. Listing Running Processes
ps aux
– Displays all active processes with detailed information.pgrep process_name
– Searches for processes by name.
5. Controlling Processes
kill PID
– Terminates a process by its PID.killall process_name
– Stops all processes with the specified name.pkill -f process_name
– Kills a process using a partial match of its command.nice -n 10 command
– Runs a process with lower priority.renice 10 -p PID
– Adjusts the priority of a running process.
6. Background and Foreground Processes
nohup command &
– Runs a command in the background, immune to hang-ups.jobs
– Lists background processes.fg %1
– Brings the first background job to the foreground.
Analyzing Disk Usage and Managing Storage
Disk space management prevents performance degradation due to bloated logs or unnecessary files.
7. Checking Disk Usage
df -h
– Displays disk usage in a human-readable format.du -sh directory_name
– Shows the total size of a directory.ncdu
– A faster, interactive disk usage analyzer.
8. Finding Large Files
find / -type f -size +100M
– Searches for files larger than 100MB.du -ah / | sort -rh | head -n 10
– Lists the ten largest files or directories.
9. Managing Log Files
journalctl --disk-usage
– Checks the size of system logs.truncate -s 0 logfile
– Clears a log file without deleting it.logrotate -f /etc/logrotate.conf
– Forces immediate log rotation based on predefined rules.
10. Removing Unnecessary Files
rm -rf /tmp/*
– Clears temporary files.apt-get autoremove
– Removes unused packages and dependencies.
Network Performance and Traffic Analysis
Monitoring network traffic ensures smooth connectivity and prevents bottlenecks.
11. Checking Network Usage
iftop
– Displays real-time network usage per connection.netstat -tulnp
– Lists active network connections and open ports.
12. Testing Network Speed and Latency
ping -c 5 google.com
– Checks latency to a remote server.traceroute google.com
– Displays the path taken by packets to their destination.
13. Managing Network Interfaces
ip a
– Lists all network interfaces.ethtool eth0
– Displays detailed network interface statistics.
File System Optimization
Optimizing file access speeds up operations and reduces latency.
14. Checking File System Health
fsck -A
– Checks and repairs all file systems.tune2fs -l /dev/sda1
– Displays file system parameters.
15. Optimizing File Access
sync
– Flushes cached data to disk.iotop
– Monitors disk I/O usage by processes.
Automation and Task Scheduling
Scheduling tasks reduces manual workload and ensures maintenance runs on time.
16. Scheduling Jobs with Cron
crontab -e
– Edits the user’s crontab file.crontab -l
– Lists scheduled jobs.echo "0 3 * * * /path/to/script.sh" | crontab -
– Schedules a script to run daily at 3 AM.
17. Automating Tasks with Anacron
anacron -t
– Lists scheduled tasks for non-continuous systems.echo "1 5 myjob /path/to/script.sh" >> /etc/anacrontab
– Runs a script five minutes after boot if missed.
System Performance Tuning with Unix Toolbox
Power users benefit from structured tools for fine-tuning performance. The Unix Toolbox provides an extensive collection of commands and techniques for optimizing CPU, memory, and storage efficiency. Keeping a reference of essential commands helps streamline daily operations and troubleshooting.
18. Tweaking System Performance Settings
sysctl -w vm.swappiness=10
– Reduces swap usage.echo 3 > /proc/sys/vm/drop_caches
– Frees cached memory.
19. Adjusting Process Limits
ulimit -n 65535
– Increases the maximum number of open files.sysctl -w fs.file-max=2097152
– Raises the global file descriptor limit.
Security and System Hardening
Securing the system prevents performance degradation from malicious activity.
20. Checking Active Users
who
– Displays logged-in users.last
– Shows login history.
21. Auditing System Logs
grep "Failed password" /var/log/auth.log
– Identifies failed login attempts.dmesg | tail
– Reviews the latest kernel messages.
22. Monitoring Open Ports
ss -tulwn
– Lists listening ports and active connections.iptables -L -v -n
– Displays active firewall rules.
Key Takeaways
- Resource Monitoring: Commands like
top
,htop
, andvmstat
help track system health. - Process Management: Tools like
kill
,renice
, andnohup
control CPU usage. - Disk Usage Analysis: Commands such as
du
,df
, andncdu
identify storage bottlenecks. - Networking:
netstat
,iftop
, andtraceroute
optimize connectivity. - Automation: Cron and anacron ensure scheduled tasks run smoothly.
- System Tuning:
sysctl
andulimit
fine-tune system settings.
Mastering these commands keeps Unix systems running efficiently. Regular monitoring, proactive resource management, and well-tuned configurations ensure peak performance.