Top 11 Linux Troubleshooting Commands with Examples

Linux stands out as a powerhouse operating system, especially for server management and network administration. It’s essential for professionals in these fields to master troubleshooting using Linux’s command-line tools. These tools not only provide powerful insights into the system’s operations but also allow for effective problem-solving. I will detail the top 11 Linux troubleshooting commands that we at UltraBoxHosting use in our everyday troubleshooting, exploring their most useful options along with practical examples.

1. top command

The top command is vital for monitoring real-time processes and resource usage. It provides a dynamic view of the system’s running processes.

Options

  • -d: Set the delay time between updates (in seconds).
  • -i: Ignore idle and zombie processes; useful for focusing on active processes.
  • -u: Show processes for a particular user, enhancing user-specific troubleshooting.
  • -p: Monitor specific processes using their PID, which helps in detailed analysis.
  • -c: Display the full path of the command, useful for identifying the exact program file location.

Example

To monitor processes of user ‘root’ updating every 2 seconds:

top -d 2 -u root

2. ps command

ps displays information about active processes. It’s highly customizable, making it a staple in system diagnostics.

Options

  • -e: Show all processes, providing a complete view of system activity.
  • -f: Offer a full-format listing, which includes extra details like UID, PID, PPID, and more.
  • -u: User-oriented output, showing user-specific process details.
  • --forest: Display parent-child relationships among processes, making the hierarchy clear.
  • -o: Custom output format, allowing specific information to be displayed.

Example

To display all processes in a full, user-oriented format with hierarchical relationships:

ps -ef --forest

3. df command

The df command checks the amount of disk space used and available on mounted filesystems.

Options

  • -h: Output in human-readable form (e.g., KB, MB, GB).
  • -a: Include dummy filesystems in the report, for completeness.
  • -i: Display inode information, useful for inode troubleshooting.
  • -T: Show filesystem type, which can help identify filesystem-related issues.
  • --total: Provide a grand total for all filesystems, useful for overall summary.

Example

To view all system disk usage including filesystem types in a readable format:

df -haT

4. du command

du estimates the disk space used by files and directories, which is crucial for managing space effectively.

Options

  • -h: Human-readable output, showing sizes in an understandable format.
  • --max-depth=n: Limit the output to a specific directory depth.
  • -s: Display only a total for each argument, simplifying the output.
  • -a: List all files, not just directories, for detailed analysis.
  • --exclude: Exclude files that match a given pattern, focusing the results.

Example

To display the total space used in the home directory, excluding MP4 files:

du -hs --exclude='*.mp4' /home/root

5. ping command

ping tests the reachability of a host on an IP network and measures the round-trip time for messages sent to the destination.

Options

  • -c: Number of echo requests to send, controlling the test duration.
  • -i: Interval in seconds between successive packet sends.
  • -t: Set the Time to Live for packets.
  • -q: Quiet output, which only shows the summary.
  • -s: Specify the size of packets in bytes.

Example

To send 5 packets with a 1-second interval to a host:

ping -c 5 -i 1 google.com

6. netstat command

netstat is used to display network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.

Options

  • -t: Show TCP connections, focusing on TCP data.
  • -u: Show UDP connections, useful for diagnosing UDP protocols.
  • -l: Display only listening sockets, helpful for server configuration checks.
  • -n: Display addresses and port numbers in numerical form, speeding up the display.
  • -p: Show the PID and the program name associated with each socket.

Example

To view all active listening TCP and UDP ports with process IDs:

netstat -tulpn

7. traceroute command

traceroute shows the route packets take to reach a network host, useful for diagnosing path issues.

Options

  • -m: Maximum number of hops to search for the target.
  • -q: Number of query packets per hop.
  • -n: Do not resolve IP addresses to hostnames, which speeds up tracing.
  • -I: Use ICMP ECHO instead of UDP datagrams, often more reliable.
  • -T: Use TCP SYN for probing, useful in bypassing firewalls.

Example

Trace the route to a host with no more than 15 hops using ICMP:

traceroute -m 15 -I google.com

8. grep command

grep searches for patterns specified by some string or regular expression, across files.

Options

  • -i: Ignore case distinctions in both the pattern and the input files.
  • -v: Invert the match, showing lines that do not match the pattern.
  • -r: Recursively search through the directory structure.
  • -l: List only the names of files with matching lines, omitting the matched lines themselves.
  • --color: Highlight the matching strings to make them stand out.

Example

To recursively search for “error” in all log files, ignoring case, highlighting matches:

grep -ri --color "error" /var/log/

9. dmesg command

dmesg is used to examine and control the kernel ring buffer, ideal for troubleshooting hardware and driver issues.

Options

  • --level: Filter messages by priority, such as errors or warnings.
  • -T: Show human-readable timestamps, which makes correlating events easier.
  • -c: Clear the ring buffer after displaying, useful in repeated testing scenarios.
  • -D: Disable display of messages to console, can help when capturing output to a file.
  • -F: Use an alternate log file, useful for reviewing historical data.

Example

To display and clear all warning messages with human-readable timestamps:

dmesg --level=warn -T -c

10. systemctl command

systemctl manages ‘systemd’ system and service manager components, crucial for controlling services on systemd-based systems.

Options

  • status: Display current service status.
  • start: Start a service.
  • stop: Stop a service.
  • restart: Restart a service.
  • enable: Set a service to start automatically at boot.

Example

To check the status of and restart the nginx service:

systemctl status nginx && systemctl restart nginx

11. journalctl command

journalctl queries and displays messages from the systemd journal, which is the centralized log management.

Options

  • -f: Follow the log in real time, similar to ‘tail -f’.
  • --since: Show entries since a specific date or time.
  • --until: Show entries up to a specified date or time.
  • -u: Filter entries by a specific systemd unit.
  • -p: Filter entries by priority, such as “info” or “err”.

Example

To display real-time logs of the nginx service from the past day:

journalctl -u nginx --since "yesterday" -f

The commands and options listed above form the backbone of Linux troubleshooting tools. Mastery of these commands equips system administrators with the ability to diagnose and resolve a myriad of system and network issues efficiently. Regular practice and usage of these commands are highly recommended to fully leverage their potential in real-world scenarios.

Leave a Reply

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