If you’re anything like me, you probably spend a lot of time troubleshooting little annoyances on your Linux setup. Whether it's network issues, a full disk, or figuring out why things are running slow, having a quick fix handy can save a lot of headaches. I’ve put together some of my go-to Linux troubleshooting snippets that help me solve these problems without having to reboot or dig through countless forum posts. Hope they help you too!

1. Reset Network Without Rebooting

So, here’s the scenario: I turn on my Linux Mint machine, and for some reason, it just won’t connect to the internet. Super frustrating, right? Instead of rebooting, which I hate doing, this one-liner always works:

sudo systemctl restart NetworkManager

Boom. Internet back up and running in seconds.

If That Doesn't Work: Bring Your Network Interface Up and Down

Sometimes, you’ve got to get a little more hands-on and manually reset the network interface. Here’s how I do it:

First, I check what my interface is called (it’s usually something like wlp3s0 for Wi-Fi):

ip a

Then, I reset it with:

sudo ip link set <interface-name> down
sudo ip link set <interface-name> up

Replace <interface-name> with whatever your interface is called. This trick can get things back online without a full reboot.

Refresh Your IP with DHCP

If you're on a network with DHCP, and you're not getting an IP for some reason, release and renew your IP lease like this:

sudo dhclient -r
sudo dhclient

I use this one a lot when I’ve switched between networks and Linux is a little confused.


2. Running Out of Disk Space? Quick Check!

Ever wonder why your machine is crawling at a snail's pace? It might just be that you're running out of disk space. Here's my quick go-to command to check what’s filling up my hard drive:

df -h

This shows me all mounted file systems and how much space is left on each one, in a human-readable format.

Check Directory Sizes

To see which folders are eating up all your space:

du -h --max-depth=1

This command shows the sizes of directories in the current folder. I like to run this in /home or wherever I think something might be hogging space.


3. Fixing "Device or Resource Busy" Errors

Ever try to unmount a drive or kill a process and you get that annoying "device or resource busy" message? Here's how I handle it:

Find Out What's Using the Resource

I use lsof (list open files) to find out what’s locking up the resource:

lsof +D /path/to/mount

This lists any processes using that specific directory. Once I know which process it is, I can kill it:

kill -9 <PID>

Where <PID> is the process ID from the lsof command.


4. Troubleshooting Slow Systems

If your Linux system starts slowing down, checking what’s eating up CPU or memory is the first thing I do.

Top Command

This is the simplest way to see what processes are using the most resources:

top

It gives you a real-time list of processes, ordered by resource usage. You can quit top by pressing q, but while it’s running, you can sort by CPU, memory, etc. Super helpful for tracking down rogue processes.

Kill the Process

Once I know which process is causing trouble, I can kill it:

killall <process-name>

Or, if it’s really stubborn:

kill -9 <PID>

5. Find and Fix Broken Packages

Once in a while, I’ve installed something, and it broke halfway through. Suddenly, apt or dpkg is stuck, and nothing else will install. To fix that, here’s what I do:

sudo dpkg --configure -a

This finishes configuring any broken packages. If that doesn't solve it, I'll run this to clean things up:

sudo apt install -f

This tries to fix broken dependencies. Works like a charm most of the time.


6. Getting Info About Your Hardware

Sometimes you need to check what hardware you're running, like if you're about to update drivers. This is how I quickly pull that info:

lshw -short

This command shows a nice overview of your system’s hardware. Great for when I need to troubleshoot hardware compatibility.


Bonus: Turn These Snippets into a Handy Bash Script

Here's how you can integrate the instructions for turning the troubleshooting guide into a Bash script within your blog. I'll write this section in the same personal style we used for the blog post:


Bonus: Turn These Snippets into a Handy Bash Script

If you're like me, you don’t want to copy-paste commands every time something goes wrong. That's why I turned all these troubleshooting snippets into a simple Bash script. This way, I just run the script, select the action I need, and let it do the work. Here’s how you can do it too!

Step 1: Create the Script

First, open your favorite text editor (I usually use nano or vim), and create a new file called linux_troubleshooter.sh. You can do this from the terminal like this:

nano linux_troubleshooter.sh

Then, copy and paste the following script into the file:

#!/bin/bash

# Function to restart Network Manager
restart_network() {
    echo "Restarting Network Manager..."
    sudo systemctl restart NetworkManager
}

# Function to reset network interface
reset_interface() {
    echo "Identifying network interfaces..."
    ip a
    echo "Enter the name of the interface to reset (e.g., wlp3s0, eth0):"
    read interface_name
    echo "Resetting network interface $interface_name..."
    sudo ip link set $interface_name down
    sudo ip link set $interface_name up
}

# Function to refresh DHCP lease
refresh_dhcp() {
    echo "Refreshing DHCP lease..."
    sudo dhclient -r
    sudo dhclient
}

# Function to check disk space usage
check_disk_space() {
    echo "Checking disk space usage..."
    df -h
}

# Function to check directory sizes
check_directory_sizes() {
    echo "Checking directory sizes in the current directory..."
    du -h --max-depth=1
}

# Function to find processes locking a resource
find_locks() {
    echo "Enter the directory path to check for locks:"
    read directory_path
    echo "Finding processes locking $directory_path..."
    lsof +D $directory_path
}

# Function to kill a process by ID
kill_process() {
    echo "Enter the PID of the process to kill:"
    read pid
    echo "Killing process $pid..."
    kill -9 $pid
}

# Function to check top resource-hungry processes
check_top() {
    echo "Checking top processes by resource usage..."
    top
}

# Function to fix broken packages
fix_packages() {
    echo "Fixing broken packages..."
    sudo dpkg --configure -a
    sudo apt install -f
}

# Function to get hardware information
get_hardware_info() {
    echo "Getting hardware information..."
    sudo lshw -short
}

# Menu for the script
echo "Linux Troubleshooter"
echo "--------------------"
echo "1. Restart Network Manager"
echo "2. Reset Network Interface"
echo "3. Refresh DHCP Lease"
echo "4. Check Disk Space Usage"
echo "5. Check Directory Sizes"
echo "6. Find Processes Locking a Resource"
echo "7. Kill a Process by PID"
echo "8. Check Top Resource-Hungry Processes"
echo "9. Fix Broken Packages"
echo "10. Get Hardware Information"
echo "11. Exit"

# Prompt user to choose an action
echo "Choose an option:"
read option

case $option in
    1) restart_network ;;
    2) reset_interface ;;
    3) refresh_dhcp ;;
    4) check_disk_space ;;
    5) check_directory_sizes ;;
    6) find_locks ;;
    7) kill_process ;;
    8) check_top ;;
    9) fix_packages ;;
    10) get_hardware_info ;;
    11) echo "Exiting..." ;;
    *) echo "Invalid option, exiting." ;;
esac

Step 2: Make the Script Executable

After saving the file, you’ll need to make it executable so you can run it directly from the terminal. Here’s how you do that:

chmod +x linux_troubleshooter.sh

This command gives the script permission to be executed.

Step 3: Run the Script

Now, whenever you need to troubleshoot an issue, simply run the script:

./linux_troubleshooter.sh

The script will present you with a menu of options. Just pick what you need, and let it handle the rest! It’s super handy to have everything in one place, and now I don’t have to remember every little command.


Wrap-Up

That's it! You now have your own custom troubleshooting script for Linux. This has saved me a ton of time, and I hope it’ll do the same for you. Feel free to tweak it, add more commands, or tailor it to your specific needs.

Linux is powerful, but every now and then, it needs a little nudge to behave. These snippets are some of the quick fixes I use all the time to keep my system running smoothly. Feel free to try them out next time you're stuck, and hopefully, they’ll save you a reboot or two!