Useful Stuff

Commands in Unix When Things Go Wrong

When working with Unix systems, encountering unexpected issues is common. Whether it’s system crashes, network failures, or corrupted files, Unix provides powerful commands to troubleshoot and resolve such problems. Here’s a comprehensive guide on essential Unix commands for handling issues efficiently.

1. Diagnosing System Performance Issues

top – Real-time Process Monitoring

  • Displays CPU, memory usage, and running processes.
  • Example usage:
top

Press q to exit.

htop – Interactive Process Viewer (better UI than top)

  • Displays real-time CPU/memory usage with intuitive navigation.
  • Install using:
sudo apt-get install htop   # For Debian-based systems

vmstat – System Performance Overview

  • Displays CPU, memory, and I/O performance details.
  • Example usage:
vmstat 1 5   # Update every 1 second for 5 iterations

2. Analyzing Disk Space and Storage Issues

df – Disk Usage Information

  • Shows available and used disk space.
  • Example usage:
df -h

du – Directory Disk Usage

  • Shows the size of files and directories.
  • Example usage:
du -sh *   # Summarizes space used by all files in the current directory

3. Finding and Terminating Problematic Processes

ps – Process Status

  • Displays running processes and their details.
  • Example usage:
ps aux | grep apache

kill – Terminate Processes

  • Used to terminate problematic processes.
  • Example usage:
kill -9 <PID>   # Forcefully terminates the process with the given PID

pkill – Kill Processes by Name

  • Example usage:
pkill -f "process_name"

xargs – Efficiently Handle Multiple Processes

  • Example usage:
ps aux | grep python | awk '{print $2}' | xargs kill -9

4. Debugging Network Issues

ifconfig / ip a – Network Configuration

  • Displays network interfaces and their status.
  • Example usage:
ifconfig
ip a

ping – Test Network Connectivity

  • Example usage:
ping 8.8.8.8

netstat – Display Network Statistics

  • Example usage:
netstat -tuln

ss – Socket Statistics (Modern Alternative to netstat)

  • Example usage:
ss -tuln

traceroute – Trace Network Route

  • Example usage:
traceroute google.com

5. File System Recovery & Data Handling

fsck – File System Consistency Check

  • Repairs and checks file system errors.
  • Example usage:
sudo fsck /dev/sda1

mount / umount – Manage Mount Points

  • Example usage:
mount /dev/sda1 /mnt
umount /mnt

lsof – List Open Files

  • Example usage:
lsof | grep filename

6. Handling User and Permissions Issues

who – Show Logged-in Users

  • Example usage:
who

last – Show Login History

  • Example usage:
last

chmod / chown – Manage File Permissions

  • Example usage:
chmod 755 filename
chown user:group filename

7. System Recovery Commands

reboot / shutdown – Restart or Shut Down System

  • Example usage:
sudo reboot
sudo shutdown -h now

systemctl – Manage System Services

  • Example usage:
systemctl restart apache2
systemctl status sshd

8. Log File Analysis for Troubleshooting

tail / less / cat – View Log Files

  • Example usage:
tail -f /var/log/syslog

journalctl – Systemd Log Viewing

  • Example usage:
journalctl -xe

9. Data Backup and Restoration

rsync – Sync Files and Directories

  • Example usage:
rsync -av /source/ /destination/

tar – Archive Utility

  • Example usage:
tar -czvf backup.tar.gz /folder_to_backup/

Conclusion

Mastering these Unix commands will help you quickly identify and resolve system issues, ensuring minimal downtime. Practice and familiarize yourself with these commands to enhance your troubleshooting skills.

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *