Appearance
Linux System Commands
Essential Linux commands for monitoring system resources, managing disk space, and maintaining the Ubuntu server.
🟡 LOW RISK
Most commands here are read-only monitoring commands. Deletion commands are marked with warnings.
Table of Contents
- Disk Space Management
- Memory Monitoring
- CPU & Process Monitoring
- System Logs Cleanup
- Network Monitoring
- System Information
Disk Space Management
Check Disk Usage (df)
bash
# Human-readable format
df -h
# Show only specific filesystem
df -h /
# Show all filesystems including tmpfs
df -haExample Output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 100G 45G 50G 47% /Warning Levels:
< 70%- Normal70-85%- Monitor closely> 85%- Take action to free space> 95%- Critical! System may fail
Check Directory Sizes (du)
bash
# Check specific directory
du -sh /var/www/html
# Check with subdirectories
du -h --max-depth=1 /var
# Find largest directories
du -h --max-depth=1 / 2>/dev/null | sort -rh | head -20Find Large Files
bash
# Files larger than 100MB
find / -type f -size +100M 2>/dev/null | head -20
# Files larger than 1GB
find / -type f -size +1G 2>/dev/null
# With sizes
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null | head -20Find Large Directories
bash
# Top 10 largest directories
du -h --max-depth=2 / 2>/dev/null | sort -rh | head -20Clean Package Cache
🟠 CAUTION
This removes downloaded package files. Safe but irreversible.
bash
# Clean apt cache
sudo apt clean
# Remove old packages
sudo apt autoremove -y
# Check freed space
df -hClean Journal Logs
🟠 CAUTION
This removes old system logs.
bash
# Check journal size
journalctl --disk-usage
# Keep only last 7 days
sudo journalctl --vacuum-time=7d
# Keep only 500MB
sudo journalctl --vacuum-size=500MMemory Monitoring
Check Memory Usage (free)
bash
# Human-readable
free -h
# With totals
free -ht
# In megabytes
free -mExample Output:
total used free shared buff/cache available
Mem: 16Gi 8.5Gi 2.1Gi 512Mi 5.4Gi 6.8Gi
Swap: 4.0Gi 1.2Gi 2.8GiKey Values:
available- Memory available for new applicationsbuff/cache- Memory used for caching (can be freed)swap used- If high, you may need more RAM
Memory Usage by Process
bash
# Top memory consumers
ps aux --sort=-%mem | head -15
# With specific columns
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -15Check Swap Usage
bash
# Swap summary
swapon --show
# Detailed swap info
cat /proc/swapsClear Cache (Emergency)
🟠 USE WITH CAUTION
Only use if system is critically low on memory.
bash
# Clear page cache
sudo sync; echo 1 > /proc/sys/vm/drop_caches
# Clear dentries and inodes
sudo sync; echo 2 > /proc/sys/vm/drop_caches
# Clear all
sudo sync; echo 3 > /proc/sys/vm/drop_cachesCPU & Process Monitoring
Real-time Monitoring (top)
bash
topKey Commands in top:
q- QuitM- Sort by memoryP- Sort by CPUk- Kill a process1- Show individual CPUs
Better Monitoring (htop)
bash
htopInstall if not available:
bash
sudo apt install htophtop Features:
- Color-coded display
- Mouse support
- Tree view (F5)
- Search (F3)
- Filter (F4)
- Kill process (F9)
Check CPU Info
bash
# CPU details
lscpu
# Number of cores
nproc
# CPU model
cat /proc/cpuinfo | grep "model name" | head -1Check Load Average
bash
# Quick view
uptime
# Detailed
cat /proc/loadavgUnderstanding Load Average:
- Three numbers: 1min, 5min, 15min averages
- Load = 1.0 per CPU core is 100% utilization
- Example: 4-core server, load 4.0 = 100% utilized
Find Process by Name
bash
ps aux | grep nginx
ps aux | grep php
ps aux | grep mysqlKill Process
🔴 CAUTION
Killing wrong process can crash services.
bash
# Graceful kill
kill <PID>
# Force kill
kill -9 <PID>
# Kill by name
pkill nginx
killall php-fpm8.3System Logs Cleanup
Check System Log Sizes
bash
du -sh /var/log/*
du -sh /var/log/Clear System Logs
🟠 CAUTION
This removes log history. Back up if needed.
bash
# Clear syslog
sudo truncate -s 0 /var/log/syslog
# Clear auth log
sudo truncate -s 0 /var/log/auth.log
# Clear kern log
sudo truncate -s 0 /var/log/kern.log
# Clear all old logs
sudo find /var/log -type f -name "*.log" -mtime +30 -delete
sudo find /var/log -type f -name "*.gz" -delete
sudo find /var/log -type f -name "*.1" -delete
sudo find /var/log -type f -name "*.old" -deleteClear Specific Service Logs
bash
# Nginx logs
sudo truncate -s 0 /var/log/nginx/access.log
sudo truncate -s 0 /var/log/nginx/error.log
# MySQL logs
sudo truncate -s 0 /var/log/mysql/error.log
# PHP logs
sudo truncate -s 0 /var/log/php8.3-fpm.logRotate Logs Now
bash
sudo logrotate -f /etc/logrotate.confNetwork Monitoring
Check Network Connections
bash
# All connections
netstat -an
# Listening ports
netstat -tlnp
# Established connections
netstat -an | grep ESTABLISHED | wc -lCheck Specific Port
bash
# Who's using port 80
sudo lsof -i :80
sudo netstat -tlnp | grep :80Check Bandwidth (if iftop installed)
bash
sudo iftopSystem Information
System Overview
bash
# OS version
cat /etc/os-release
# Kernel version
uname -r
# All system info
uname -aUptime
bash
uptimeLast Reboot
bash
last reboot | head -5Check Running Services
bash
systemctl list-units --type=service --state=runningQuick Disk Cleanup Script
bash
#!/bin/bash
# Save as /root/cleanup.sh
echo "=== Disk Usage Before ==="
df -h /
echo ""
echo "=== Cleaning apt cache ==="
sudo apt clean
sudo apt autoremove -y
echo ""
echo "=== Cleaning journal logs ==="
sudo journalctl --vacuum-time=7d
echo ""
echo "=== Cleaning old Laravel logs ==="
find /var/www/html/storage/logs -name "*.log" -mtime +7 -delete
echo ""
echo "=== Cleaning old system logs ==="
sudo find /var/log -type f -name "*.gz" -delete
sudo find /var/log -type f -name "*.1" -delete
echo ""
echo "=== Disk Usage After ==="
df -h /Run:
bash
sudo chmod +x /root/cleanup.sh
sudo /root/cleanup.shQuick Reference Card
| Action | Command |
|---|---|
| Disk usage | df -h |
| Directory size | du -sh /path |
| Memory usage | free -h |
| CPU/Process monitor | htop or top |
| Load average | uptime |
| Find large files | find / -type f -size +100M |
| Clear apt cache | sudo apt clean && sudo apt autoremove |
| Clear journal | sudo journalctl --vacuum-time=7d |
| Check ports | netstat -tlnp |
| Kill process | kill <PID> |
| System info | uname -a |
| OS version | cat /etc/os-release |
