π§° Unix (Ubuntu/Debian) Troubleshooting Commands
π Overview
This wiki provides a detailed reference for Ubuntu/Debian system troubleshooting.
It covers system health, processes, networking, performance, logs, and common service troubleshooting (SSH, Nginx, Apache, MySQL, etc.).
πΉ 1. Basic System Health
| Command | Description |
|---|---|
uname -a |
Displays kernel version and architecture. |
lsb_release -a |
Shows distribution details. |
hostnamectl |
Displays system hostname and OS metadata. |
uptime |
Shows how long the system has been running. |
top / htop |
Interactive process and CPU/memory usage view. |
free -h |
Displays memory and swap usage. |
vmstat 5 |
Monitors system performance at 5s intervals. |
df -h |
Displays disk space usage. |
du -sh * |
Shows directory sizes in current path. |
iostat -xz 1 |
Shows CPU and I/O usage statistics. |
| `dmesg | tail -20` |
Tips
- Monitor uptime and load average for spikes.
- Watch for I/O wait in
iostat. - Use
journalctl -p err -bto quickly see errors since last boot.
πΉ 2. User & Access Management
| Command | Description |
|---|---|
who / w |
Shows logged-in users and sessions. |
last |
Displays recent login history. |
id <user> |
Displays UID, GID, and groups for a user. |
sudo -l -U <user> |
Lists allowed sudo privileges. |
cat /etc/passwd |
Lists all users on the system. |
grep <user> /etc/shadow |
Checks for password hash entries. |
sudo visudo |
Safely edits sudoers configuration. |
sudo adduser <username> |
Add a new user. |
sudo passwd <username> |
Change user password. |
sudo usermod -aG sudo <username> |
Add user to sudo group. |
sudo deluser <username> |
Delete a user (add --remove-home to delete home directory). |
Troubleshooting SSH Access
- Check
/var/log/auth.logfor login failures. - Validate SSH keys and permissions:
ls -ld ~/.ssh && ls -l ~/.ssh/authorized_keys - Ensure correct file permissions:
.ssh= 700,authorized_keys= 600.
πΉ 3. Process & Service Management
| Command | Description |
|---|---|
ps aux --sort=-%cpu |
Lists top CPU-consuming processes. |
| `ps -ef | grep |
systemctl status <service> |
Checks service status. |
systemctl start/stop/restart <service> |
Controls service state. |
systemctl enable/disable <service> |
Enables or disables service at boot. |
journalctl -u <service> |
Displays logs for a specific service. |
kill -9 <pid> |
Terminates a process by PID. |
pkill <process> |
Kills all matching process names. |
nice/renice |
Adjusts process priority. |
Tips
- Use
toporhtopto monitor CPU/memory spikes in real time. - Use
journalctl -xefor detailed service failure logs. - If
systemctlfails silently, check/run/systemd/system/for service overrides.
πΉ 4. Network Troubleshooting
| Command | Description |
|---|---|
ip a |
Displays all interfaces and assigned IPs. |
ip link |
Shows network interface state. |
ip route |
Displays routing table. |
nmcli device status |
Displays NetworkManager connection state. |
ping <ip> |
Tests reachability. |
traceroute <ip> |
Shows path to destination. |
ss -tuln |
Lists open ports and listening services. |
netstat -rn |
Displays routing table. |
dig <domain> |
DNS lookup test. |
nslookup <domain> |
DNS query. |
curl -I http://<host> |
Checks web response headers. |
Troubleshooting Steps
- Verify link state using
ip linkorethtool <iface>. - Check routes and gateways using
ip route. - Review firewall rules (
sudo ufw status verbose). - Use
tcpdump -i <iface> host <ip>to verify packet flow.
πΉ 5. Disk & Filesystem Management
| Command | Description |
|---|---|
lsblk |
Lists block devices and mount points. |
df -hT |
Displays filesystem types and usage. |
sudo fdisk -l |
Lists partitions. |
mount / umount |
Mounts or unmounts devices. |
cat /etc/fstab |
Displays persistent mount configuration. |
sudo fsck -f /dev/sdX |
Checks and repairs filesystem. |
sudo lsof +D <directory> |
Lists open files in a directory. |
sudo du -sh /* |
Checks which directory consumes most disk. |
iostat -xz 1 |
Monitors disk I/O performance. |
truncate -s 0 filename |
clear (empty) the contents of a file. |
Disk Full Troubleshooting
sudo du -h /var | sort -h | tail -20
sudo find /var/log -type f -size +100M
find /path/to/dir -type f -exec du -h {} + | sort -rh | head -20
πΉ 6. Memory & Performance Analysis
| Command | Description |
|---|---|
free -m |
Shows memory and swap summary. |
vmstat 5 |
Monitors CPU and memory statistics. |
top / htop |
Real-time performance view. |
sar -r 5 5 |
Collects memory utilization history. |
| `ps aux --sort=-%mem | head` |
Tips
- Check swap usage under heavy load (
swapon --show). - Reduce memory leaks by restarting misbehaving services.
- Persistent high
wa(I/O wait) invmstatindicates disk bottleneck.
πΉ 7. Logs & Monitoring
| Log File | Path | Description |
|---|---|---|
| System Log | /var/log/syslog |
General system messages. |
| Kernel Log | /var/log/kern.log |
Kernel messages and driver logs. |
| Authentication | /var/log/auth.log |
Login, SSH, and sudo activity. |
| Daemon Logs | /var/log/daemon.log |
Background service logs. |
| Boot Log | /var/log/boot.log |
Boot-time messages. |
| Journalctl | journalctl -xe |
Systemd journal with details. |
Useful Commands
journalctl -p err -b
journalctl --since "1 hour ago"
journalctl -u ssh
tail -f /var/log/syslog
Tip: Configure logrotate for /var/log/ to prevent disk overuse.
πΉ 8. Security & Firewall
| Command | Description |
|---|---|
sudo ufw status verbose |
Checks UFW firewall state. |
sudo ufw enable/disable |
Turns firewall on or off. |
sudo ufw allow <port> |
Opens port for specific service. |
sudo iptables -L -v -n |
Lists raw firewall rules. |
sudo fail2ban-client status |
Shows Fail2Ban jail status. |
Security Checks
- Verify SSH access in
/etc/ssh/sshd_config. - Review
/etc/sudoersfor privilege escalation risks. - Inspect
/var/log/auth.logfor failed attempts.
πΉ 9. Package & Patch Management
| Command | Description |
|---|---|
sudo apt update |
Refreshes package lists. |
sudo apt upgrade |
Installs available updates. |
sudo apt install <pkg> |
Installs a package. |
sudo apt remove <pkg> |
Removes a package. |
| `sudo dpkg -l | grep |
apt-cache show <pkg> |
Displays package details. |
Troubleshooting Broken Packages
sudo apt --fix-broken install
sudo dpkg --configure -a
πΉ 10. Common Service Troubleshooting
π¦ SSH
| Command | Description |
|---|---|
systemctl status ssh |
Checks SSH service status. |
sudo tail -f /var/log/auth.log |
Monitors SSH login attempts. |
| `sudo netstat -tuln | grep 22` |
sudo sshd -t |
Tests SSH config syntax. |
Issues
- Permission denied β Check ownership of
~/.ssh/authorized_keys. - Connection refused β Ensure
sshdrunning and port 22 open.
π© Nginx
| Command | Description |
|---|---|
sudo systemctl status nginx |
Verifies Nginx service status. |
sudo nginx -t |
Tests Nginx configuration syntax. |
sudo tail -f /var/log/nginx/error.log |
Checks runtime errors. |
sudo lsof -i :80 |
Verifies if port 80 is in use. |
Troubleshooting
- 502/504 β Check upstream service health.
- Port conflict β Verify Apache not bound to port 80.
π¨ Apache2
| Command | Description |
|---|---|
sudo systemctl status apache2 |
Checks Apache service status. |
sudo apachectl configtest |
Tests Apache configuration syntax. |
sudo tail -f /var/log/apache2/error.log |
Monitors errors. |
sudo lsof -i :80 |
Checks if Apache is listening on port 80. |
Common Fixes
- Permissions errors β Check
/var/wwwownership. - 403 Forbidden β Review site config and
.htaccess.
π₯ MySQL / MariaDB
| Command | Description |
|---|---|
sudo systemctl status mysql |
Checks database service status. |
mysql -u root -p |
Opens MySQL shell. |
sudo tail -f /var/log/mysql/error.log |
Monitors database errors. |
| `sudo netstat -tuln | grep 3306` |
sudo mysqladmin -u root -p processlist |
Checks active DB connections. |
Tips
- βCan't connect to MySQLβ β Check socket and bind-address.
- βToo many connectionsβ β Increase
max_connectionsinmy.cnf.
πΉ 11. File, Folder, and Permission Management
| Command | Description |
|---|---|
pwd |
Prints the current working directory. |
ls -l |
Lists files with permissions, owner, and size. |
ls -lh |
Lists files with human-readable sizes. |
cd <directory> |
Changes to a specific directory. |
mkdir <folder> |
Creates a new folder. |
mkdir -p /path/to/folder |
Creates nested folders if not existing. |
touch <file> |
Creates an empty file. |
cp <source> <destination> |
Copies files or directories (-r for recursive). |
mv <old> <new> |
Moves or renames files. |
rm <file> |
Deletes a file. |
rm -rf <directory> |
Force deletes directory recursively (use with caution). |
cat <file> |
Displays file content. |
less <file> |
Opens file for scrollable view. |
head -n 20 <file> |
Displays first 20 lines of a file. |
tail -f <file> |
Follows file updates in real-time (useful for logs). |
nano <file> |
Opens file in Nano text editor. |
vim <file> |
Opens file in Vim editor. |
chmod 755 <file> |
Changes file permission (owner can read/write/execute). |
chmod -R 644 <folder> |
Applies permission recursively. |
chown user:group <file> |
Changes file ownership. |
chown -R user:group <folder> |
Changes ownership recursively. |
stat <file> |
Displays file details (permissions, owner, timestamps). |
Quick Permission Reference
| Numeric | Meaning | Permission Breakdown |
|---|---|---|
| 700 | Owner full control | rwx------ |
| 755 | Owner full + others read/execute | rwxr-xr-x |
| 644 | Owner read/write, others read | rw-r--r-- |
| 600 | Owner read/write only | rw------- |
Tips
- Use
sudowhen editing or creating system files. - Use
chmod +x <file>to make scripts executable. - Backup critical configuration files before editing.
- Prefer
nanofor simple edits;vimfor advanced usage.
πΉ 12. Backup & Restore
| Command | Description |
|---|---|
rsync -avz /source /backup |
Syncs directories. |
scp file user@host:/path |
Secure file transfer. |
tar -czvf backup.tar.gz /dir |
Creates compressed archive. |
tar -xzvf backup.tar.gz -C /restore |
Extracts backup. |
βοΈ 12. Common Troubleshooting Scenarios
π© Scenario 1 β High CPU Usage
topβ Identify culprit process.pidstat -p <pid> 1β Check CPU usage by PID.- Restart or
reniceif needed.
π¨ Scenario 2 β Disk Full
df -hβ Identify partition.du -sh /var/* | sort -hβ Find largest folders.- Truncate large logs or move archives.
π¦ Scenario 3 β Service Fails to Start
systemctl status <service>journalctl -u <service>sudo systemctl daemon-reexecand retry.
π§ Scenario 4 β Network Unreachable
ip a/ping <gateway>sudo ufw statussudo systemctl restart NetworkManager
π₯ Scenario 5 β SSH Denied
- Review
/var/log/auth.log. - Check permissions on
~/.ssh. - Confirm user shell and access rights.