The top command in Linux gives useful statistics about system resources. We can use it to view CPU and memory usage alongside process information of running services. You can also find zombie processes using top. So, mastering the top command is a must for Linux admins.

The following section provides a simple overview of the top command and shows how to use top in real-world scenarios.

How Does the top Command Work?

By default, top shows a list of running processes alongside standard CPU metrics. You can think of the first part of the output as the dashboard. The bottom section shows the process list and provides a real-time display of all running processes.

The dashboard consists of five lines, each containing some metrics. The first line shows short information about the system, such as the uptime, load average, and the number of users currently logged in. Tasks are shown in the second line. The third one shows CPU load, and the following two lines indicate memory usage.

Note that the commands you specify while running top are case-sensitive. For example, the n and N keys both perform different operations.

1. Display All Running Processes

When used without any arguments, the top command outputs a list of all currently running processes.

        top
    

Output:

output of Linux top command

You can navigate the output using the Up, Down, PageUp, and PageDown keys on your keyboard. Hit q to get to the shell.

2. Sort Linux Processes by PID

You can sort the process list by their Program ID or PID. Press the N key when running top to sort processes based on PID.

3. Sort Processes by Memory and CPU Usage

The default top output sorts the process list based on the CPU usage. You can sort the list by memory usage using the M key on your keyboard. Enter P to sort by CPU again.

4. Sort Processes by Running Time

If you want to find out for how long processes are running on your machine, press the M and T keys.

sort process using running time

5. Display Running Processes for Specific Users

We can view a list of all running processes that belong to a specific user. When inside top, press u and then supply the username and press Enter. You can also use the -u option of top followed by the username to do this.

        top -u root
    

The aforementioned command outputs all processes invoked by root.

6. Highlight Active Processes

If you enter the z key inside top, it will highlight all actively running Linux processes. It makes navigating active processes simpler.

highlight running processes using top command in Linux

7. Change top's Interval Period

By default, top refreshes its output every three seconds. However, you can easily set this to a custom value by pressing d followed by the required value.

8. Change Process Priority

You can change the priority of a Linux process by setting a custom renice value inside top. Type r followed by the PID of the process and then enter its new renice value.

9. Display Idle Processes Using top

We can see a list of all the idle processes by pressing the i key.

show idle processes using Linux top command

10. Kill a Linux Process by PID

The top command in Linux allows us to kill a running process directly from the interface. To kill a process, type k followed by the PID of that process. It will be useful when dealing with zombie processes.

Own System Processes with the top Command

The top command in Linux makes process management simpler for beginners. We can use top for all sorts of tasks, including monitoring system resources and managing hanged processes. So no matter whether you're a Linux newbie or an expert, the top command has got something for you.

System monitoring is only a part of an admin's day. How do you monitor network connections? The ss command is here for this job solely.