Thursday, January 30, 2025

Linux 101: Part 8 - System Monitoring and Performance Tuning in Linux


In this eighth part of our Linux 101 series, we will explore System Monitoring and Performance Tuning to help optimize system resources, troubleshoot issues, and improve efficiency.

Why Monitor System Performance?

Monitoring system performance allows you to:

  • Detect and troubleshoot slowdowns.
  • Optimize resource usage.
  • Prevent system crashes and failures.
  • Ensure servers run efficiently.

Essential System Monitoring Commands

1. Checking System Load and CPU Usage

  • View real-time CPU and process usage:
    top
    
  • Enhanced process monitoring (interactive UI):
    htop
    
    (Install htop if not available: sudo apt install htop or sudo dnf install htop)
  • Check CPU load averages:
    uptime
    

2. Checking Memory Usage

  • View memory usage in MB:
    free -m
    
  • Detailed memory usage analysis:
    vmstat 5
    
    (Refreshes every 5 seconds)

3. Checking Disk Usage

  • Check disk space usage:
    df -h
    
  • Check which folders use the most space:
    du -sh /home/*
    

4. Monitoring Running Processes

  • List running processes:
    ps aux
    
  • Find a specific process:
    ps aux | grep processname
    
  • Kill a high-resource-consuming process:
    kill PID
    

5. Checking Network Usage

  • View active network connections:
    netstat -tulnp
    
  • Monitor real-time network usage:
    iftop
    
    (Install with sudo apt install iftop or sudo dnf install iftop)

Performance Tuning in Linux

1. Optimizing CPU Performance

  • Change process priority using nice and renice:
    nice -n 10 command
    
    renice -n -5 -p PID
    
  • Limit CPU usage of a process:
    cpulimit -p PID -l 50
    
    (Install with sudo apt install cpulimit)

2. Managing RAM and Swap Space

  • Clear cached memory:
    sync; echo 3 > /proc/sys/vm/drop_caches
    
  • Check swap usage:
    swapon -s
    
  • Increase swap space:
    sudo fallocate -l 2G /swapfile
    sudo chmod 600 /swapfile
    sudo mkswap /swapfile
    sudo swapon /swapfile
    

3. Improving Disk Performance

  • Enable write caching:
    sudo hdparm -W1 /dev/sda
    
  • Check disk health:
    sudo smartctl -a /dev/sda
    
    (Install with sudo apt install smartmontools)

4. Managing Services and Startup Applications

  • List enabled services:
    systemctl list-units --type=service
    
  • Disable unwanted services:
    sudo systemctl disable service-name
    
  • View startup applications:
    systemd-analyze blame
    

Automating Performance Monitoring

You can automate system monitoring with tools like cron jobs and log monitoring:

  • Log system resource usage every 10 minutes:
    crontab -e
    
    Add this line:
    */10 * * * * free -m >> /var/log/memory.log
    
  • Monitor CPU and memory with sar (install with sudo apt install sysstat):
    sar -u 5 10
    

Conclusion

System monitoring and performance tuning are essential for maintaining a stable Linux environment. In the next part, we will explore Backup and Recovery Strategies in Linux to ensure data protection and disaster recovery planning.

Stay tuned for Linux 101: Part 9 – Backup and Recovery Strategies in Linux!

0 comments: