Using Linux, especially as a server, often means dealing with new errors and resolving them. Most of them are easy to fix with just a web search. But some errors may require that you do some digging. In this guide, I’m sharing some Linux commands that will help you diagnose and solve those kinds of Linux errors.
1dmesg
The dmesg commandis a powerful tool for printing Linux kernel ring buffer messages. These messages often include system boot messages and hardware errors. Run the command with sudo, like this:
You can also display them with human-readable timestamps.
The output you get from dmesg can be piped to other commands like grep for further processing.
2systemctl
The systemctl command is used for managing services on systems usingsystemd. you’re able to turn on/off a service, check log entries, andlist all serviceswith details using the command.
If you need to start or stop a service, run:
If you need to restart a service, run:
For example, myApache web serverwasn’t responding. I could check if the service is active.
If it’s not running, I can start or restart it.
3ps
The ps command allows you to display information about andmonitor Linux processes. To get a detailed overview of all processes, run:
If you want to list the processes in a hierarchical format, then run:
You can use the ps command to list processes with resource usage or to know a certain process ID.
4kill
The kill command is useful for forcefullyterminating a running process. Sometimes, multiple processes can conflict with each other. Hence, one process fails to execute. That’s when you can kill the process causing the issue. To kill a process, you must know its process ID (PID.) You can get the PID using the ps command. Kill the process by passing the PID to the kill command,
For example, to kill the Apache process, I’ll run:
You may also need to have sudo to execute the command
5ping
ping is a networking tool for checking the availability of a host on a network. It can help you determine if your network connection is live and if theDNSis resolving correctly.
You can use the ping command by passing a hostname, preferably the URL or the IP address, like this:
The packet loss info also lets you know your network performance. You can alsouse ping on Windows.
6lsof
The lsof commandis used to list open files. It can be used on many occasions, such as network debugging, and listing files by processes, users, and ports.
If you want to see files opened related to the network, run:
If you want to see the files opened in a specific directory, then use:
The lsof command can help you find which processes are using which files so you may address them properly.
7grep
The grep commandis useful for searching for strings and patterns through files and whole directories. To do a simple search, you pass the pattern and the file name or directory to the grep command.
Suppose you have log files with thousands of entries. The grep command will help you search for lines relevant to your problems. You may also search for common words such as “error” or “failed” to find the lines quickly. you may also pipe the output of another command to grep.
8tail
The tail commandis usually used to display the last few lines of a file. If you have log files that are too long and you’re only interested in the latest additions to the file, tail becomes handy. you may also use tail to actively monitor a log file, with this command:
Another useful feature of the command is displaying a certain number of lines. Suppose you want to display the last 20 lines from a file. Then run:
The tail command can also be used to pipe the output of another command.
9journalctl
The journalctl commandis useful for querying and displaying logs from systemd’s journal. you may simply run the command without any parameters (adding sudo is better for getting more details.)
You can also display the most recent logs with extra information.
If you want to check the logs of each service, run:
Suppose you had a system crash. The journalctl command will help you investigate and identify the root cause.
10strace
The strace commandtraces system calls and signals. It provides a detailed look at what a process is doing. A simple use of strace is to trace a running process using its PID.
You can also the above output by system calls.
It also lets you save the trace output to a file.
If a program happens to be hanging, and you can’t find the reason, you can trace the system calls the program is making and reveal why it’s getting stuck.