Commands to Find and Troubleshoot a Linux Server

Commands to Find and Troubleshoot a Linux Server

Linux servers are known for their stability, performance, and security, making them a popular choice for businesses and developers. However, like any system, they can encounter issues that require troubleshooting. Understanding the commands to find and troubleshoot problems on a Linux server is crucial for system administrators and anyone working with Linux. This article explores essential commands for identifying and resolving issues on a Linux server.

1. Understanding System Information

Before diving into troubleshooting, it’s essential to understand the server’s current state. Several commands can help you gather critical information about the system.

1.1. Checking the System Uptime

The uptime command provides the server’s current time, how long it has been running, and the average load.

uptime

This command is helpful to determine if the system has been rebooted recently, which can be an indicator of underlying issues.

1.2. Viewing System and Kernel Information

The uname -a command gives you detailed information about the system’s kernel and architecture.

uname -a

This command is particularly useful when troubleshooting compatibility issues or when checking the system’s kernel version after an update.

1.3. Checking System Resource Usage

To check how system resources are being used, the top command provides a real-time view of system processes and resource consumption.

top

For a more detailed and customizable view, htop is a more user-friendly alternative, though it may need to be installed first.

2. Troubleshooting Disk Issues

Disk space and performance are common issues that can impact a server’s stability and functionality.

2.1. Checking Disk Usage

The df -h command displays disk usage for all mounted filesystems in a human-readable format.

df -h

This command helps identify if any partitions are running out of space, which can cause system issues.

2.2. Checking Disk Inodes

Inodes represent metadata about files. If a filesystem is out of inodes, it can prevent the creation of new files, even if there is space left. The df -i command checks inode usage.

df -i

This command is particularly useful when troubleshooting issues where new files cannot be created despite available disk space.

2.3. Analyzing Disk I/O

To analyze disk I/O performance, the iostat command provides insights into input/output statistics for devices.

iostat -x 5

This command helps identify if disk I/O is a bottleneck, which can slow down the server

iostat -x 5

3. Troubleshooting Network Issues

Network connectivity and performance are crucial for server operations. Understanding network troubleshooting commands is essential.

3.1. Checking Network Configuration

The ifconfig command displays network interfaces and their configuration.

ifconfig

3.2. Testing Network Connectivity

This command helps verify if network interfaces are configured correctly.

To check if the server can reach another system, use the ping command.

ping -c 4 google.com

This command helps test basic network connectivity and latency.

3.3. Diagnosing Network Routes

The traceroute command tracks the path packets take to reach a destination, helping diagnose routing issues.

traceroute google.com

This command is useful for identifying where network packets are getting lost or delayed.

3.4. Monitoring Network Traffic

The netstat command displays network connections, routing tables, and interface statistics.

netstat -tuln

This command is helpful for identifying active connections and ensuring services are running on the correct ports.

4. Troubleshooting CPU and Memory Issues

High CPU or memory usage can slow down a server or cause it to become unresponsive.

4.1. Monitoring CPU Usage

The mpstat command provides detailed CPU usage statistics.

mpstat -P ALL

This command helps identify if specific CPUs or cores are under heavy load.

4.2. Checking Memory Usage

To check memory usage, the free -m command provides a snapshot of memory usage, including total, used, and free memory.

free -m

This command helps identify if the server is running low on memory, which can cause performance issues.

4.3. Analyzing Process Resource Usage

The ps aux --sort=-%mem command lists processes sorted by memory usage, helping identify resource-hungry processes.

ps aux --sort=-%mem

This command is useful for finding processes that may be consuming excessive resources.

5. Troubleshooting Services and Daemons

Services and daemons are background processes critical to server operations. Ensuring they are running correctly is essential.

5.1. Checking Service Status

The systemctl status command checks the status of services and daemons managed by systemd.

systemctl status nginx

This command helps verify if a service is running or has encountered issues.

5.2. Restarting Services

If a service is not functioning correctly, restarting it using systemctl restart can resolve issues.

systemctl restart nginx

This command is useful for services that have hung or stopped unexpectedly.

5.3. Checking Service Logs

Logs are critical for troubleshooting. The journalctl command displays logs from systemd services.

journalctl -u nginx

This command helps identify errors or warnings that may explain why a service is not working as expected.

6. Managing and Analyzing Logs

Logs provide valuable information for diagnosing issues. Understanding how to manage and analyze logs is crucial.

6.1. Viewing System Logs

The dmesg command displays kernel messages, which can include errors related to hardware and drivers.

dmesg | less

This command helps identify low-level system issues.

6.2. Searching Logs for Specific Entries

The grep command is powerful for searching log files for specific entries.

grep "error" /var/log/syslog

This command helps quickly identify relevant log entries, making it easier to troubleshoot issues.

6.3. Monitoring Logs in Real-Time

The tail -f command is useful for monitoring logs in real-time as they are written.

tail -f /var/log/syslog

This command is helpful for watching logs while reproducing an issue to capture relevant information.

7. Managing File Permissions and Ownership

File permissions and ownership are fundamental to Linux security and functionality.

7.1. Checking File Permissions

The ls -l command displays detailed information about file permissions and ownership.

ls -l /path/to/file

This command helps verify if permissions are set correctly, which can resolve access issues.

7.2. Changing File Permissions

The chmod command changes file permissions.

chmod 755 /path/to/file

This command is useful for correcting permission issues that prevent scripts or applications from running.

7.3. Changing File Ownership

The chown command changes the ownership of files or directories.

chown user:group /path/to/file

This command is essential when files need to be owned by a specific user or group to function correctly.

8. Managing and Troubleshooting Users and Groups

User and group management is essential for maintaining system security and access control.

8.1. Viewing User Information

The id command displays user and group information for the current user or a specified user.

id username

This command helps verify if a user has the correct group memberships.

8.2. Managing User Accounts

The useradd and usermod commands are used for adding and modifying user accounts, respectively.

useradd newuser
usermod -aG sudo newuser

These commands are useful for troubleshooting access issues related to user accounts.

8.3. Checking Last Login

The lastlog command shows the last login time for all users.

lastlog

This command helps identify if unauthorized access has occurred.

9. Troubleshooting Boot Issues

Boot issues can prevent a server from starting correctly, making troubleshooting challenging.

9.1. Checking Boot Logs

The journalctl -b command displays logs from the current boot session.

journalctl -b

This command is useful for identifying errors that occur during the boot process.

9.2. Managing GRUB Bootloader

If the server fails to boot, you may need to interact with the GRUB bootloader. Commands like grub-install and grub-update are useful here.

grub-install /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg

These commands help resolve issues related to the bootloader configuration.

10. Utilizing Diagnostic and Recovery Tools

Linux provides several tools for diagnosing and recovering from system issues.

10.1. Using Rescue Mode

Many Linux distributions offer a rescue mode, which can be accessed from the boot menu. This mode is useful for fixing critical issues, such as those that prevent the system from booting normally.

10.2. Running a Filesystem Check

The fsck command checks and repairs filesystems. This command is essential for troubleshooting and fixing filesystem corruption.

fsck /dev/sda1

This command helps resolve issues related to disk corruption.

Conclusion

Troubleshooting a Linux server requires a deep understanding of various commands and tools. From monitoring system resources to managing services, networks, and user accounts, these commands provide the necessary insights to identify and resolve issues effectively. By mastering these tools, administrators can ensure their servers remain stable, secure, and performant.

Fedya Serafiev

Fedya Serafiev

Fedya Serafiev owns the website linuxcodelab.eu. He finds satisfaction in helping people solve even the most complex technical problems. His current goal is to write easy-to-follow articles so that such problems do not arise at all.

Thank you for reading the article! If you found the information useful, you can donate using the buttons below: