TranslateProject/sources/tech/20221117.0 ⭐️⭐️ How to Install and Use htop in Linux.md
2023-02-16 10:56:49 +08:00

198 lines
8.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[#]: subject: "How to Install and Use htop in Linux"
[#]: via: "https://itsfoss.com/use-htop/"
[#]: author: "Sagar Sharma https://itsfoss.com/author/sagar/"
[#]: collector: "lkxed"
[#]: translator: "perfiffer"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
How to Install and Use htop in Linux
======
Windows has its famous task manager. Linux has several GUI and [command line system monitors][1]. Every Linux system comes with a couple of them.
On the command line, the top command is perhaps the goto command for checking the system resource utilization quickly.
[Using top command][2] apart from viewing the processes could be tricky. And this is where htop tops top. Pun aside, htop is a top-like utility but with a better and user-friendly interface.
In this guide, I will be showing you how you can install and use htop in Linux.
### Install htop utility in Linux
You wont find htop pre-installed on the majority of Linux distributions but being one of the most popular utilities, you will find htop in default repositories of almost every Linux distro.
So if your machine is powered by something that is based on Debian/Ubuntu, the following command should get your job done:
```
sudo apt install htop
```
Similarly, if youre on Fedora, you can use the given command:
```
sudo dnf install htop
```
And theres also a snap package available if you like to avoid building packages from the source:
```
sudo snap install htop
```
If youre on something else or want to build from a source, you can always refer to [htops GitHub page][3] for detailed instructions.
Once youre done with the installation, you just have to use the htop command in the terminal, and it will reflect all the ongoing processes in your system:
```
htop
```
![install and use htop][4]
In htop, there is a color coding for the individual section, so lets have a look at what each color indicates while using htop.
##### What different colors and statistics indicate in htop
So lets start with the CPU usage bar, as it utilizes the maximum number of colors.
#### CPU usage bar
![cpu process in htop][5]
- **Green:** Resources consumed by user processes.
- **Blue:** Indicates low-priority threads.
- **Red:** CPU resources used by system (kernel) processes.
- **Aqua blue:** Indicates virtualized processes.
#### Memory bar
![memory bar in htop][6]
- **Green:** Memory being utilized by system processes.
- **Blue:** Memory used by buffer pages.
- **Orange:** Memory allocated for cache pages.
#### Statistics
![task statistics in htop][7]
- **1.86** is the average load for the last minute.
- **1.75** is the average load for the last 4 minutes.
- **1.47** is the average load for the last 15 minutes.
- **Tasks: 166** shows there is a total of 166 ongoing processes.
- **1249 thr** indicates that those 166 processes are handled by 1249 threads.
- **1 running** indicates that from those 166 processes, only one task is in a state of running.
- **The load** average indicates the average system load over a period of time. Since my system is Hexa-Core, anything under 6.0 is ok. This number may exceed, such as 6.1, so the upcoming processes have to wait for ongoing tasks to be completed.
- **Uptime** is nothing but hours since you logged in.
Now, lets jump to the actual implementation part.
### How to use htop in Linux
As the htop is mainly used to check for system resources, lets have a look at how you can sort the processes based on resource consumption.
#### Sort processes based on Resource Consumption
The easiest way to sort processes based on CPU and memory usage is to use your mouse pointer. Hover the cursor over the CPU or Memory section and click on any of those.
And there you will see an icon of a triangle `△` and based on that you can sort the process based on highest to lowest resource consumption:
But if you are dealing with remote servers, you might not have the privilege to use a mouse and in those cases, you can use keyboard shortcuts.
Press **F6** and it will bring up every option available to sort the ongoing processes:
![sort processes in htop using keyboard shortcut][8]
You can use arrow keys to select a preferred sorting option and then press the Enter key, results should reflect as intended.
#### Search for a specific process
If you want to look for a specific process and its resource consumption, you can press **F3** and it will get you a search prompt as shown below:
![search processes in htop][9]
For example, I searched for htop, and it highlighted the process with light orange color. And you can press **F3** for the next result.
#### Filter ongoing processes
While searching may get you the intended results, I find the filtering process using keywords even more effective as it presents a list of processes.
To filter processes, you have to press **F4** and type the name of the process. For example, I filtered processes related to gnome-boxes:
![filter processes in htop][10]
#### Kill process
Once you made it to find the most resource-hungry and unnecessary process, you just have to press **F9**, and it will present you with termination signals:
![kill process in htop][11]
I cant cover all 15 termination signals, we have a separate guide on [different termination signals][12], so you can refer to that guide if you intend to learn more about them.
But I will recommend you use SIGTERM first, as it is the most efficient and friendly way to kill the process.
#### Customize htop
Here, my aim is to add a date and time and change the color scheme to monochrome.
First, press **F2**, and it will being setup prompt allowing users to change how htop looks:
![customize htop in ubuntu][13]
First, hover to the `Colors` sections and press Enter and it will allow us to change the color scheme. From there, select the Monochrome option and press Enter to save changes:
![change htop colors in linux][14]
Now, go back to the setup option, and from there, use the left arrow key to explore available meters:
![explore available meters in htop][15]
As I intend to add the Date and time, I have to press Enter once I find the option for it.
Here, it will allow you to place the date and time in any of the left and right columns and you can use the up and down arrow keys to change the order of columns.
So I placed the date and time meter with the last styling option (you can change styles using the spacebar):
![add date and time htop][16]
Once you are done aligning the date and time meter, press the enter key to save changes and **F10** to close the setup prompt.
### Wrapping Up
In this guide, I explained how you can install the htop utility in different Linux distributions and how you can use some basic functionalities of htop to manage system resources efficiently.
But htop can do a lot more and for that and to learn more, you can always refer to its man page, and we have a detailed guide on [how you can get the most out of the man page in Linux][17].
--------------------------------------------------------------------------------
via: https://itsfoss.com/use-htop/
作者:[Sagar Sharma][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/sagar/
[b]: https://github.com/lkxed
[1]: https://itsfoss.com/linux-system-monitoring-tools/
[2]: https://linuxhandbook.com/top-command/
[3]: https://github.com/htop-dev/htop
[4]: https://itsfoss.com/wp-content/uploads/2022/11/install-and-use-htop.png
[5]: https://itsfoss.com/wp-content/uploads/2022/11/cpu-process-in-htop-1.png
[6]: https://itsfoss.com/wp-content/uploads/2022/11/memory-bar-in-htop.png
[7]: https://itsfoss.com/wp-content/uploads/2022/11/task-statistics-in-htop.png
[8]: https://itsfoss.com/wp-content/uploads/2022/11/sort-processes-in-htop-using-keyboard-shortcut.png
[9]: https://itsfoss.com/wp-content/uploads/2022/11/search-processes-in-htop.png
[10]: https://itsfoss.com/wp-content/uploads/2022/11/filter-processes-in-htop.png
[11]: https://itsfoss.com/wp-content/uploads/2022/11/kill-process-in-htop.png
[12]: https://linuxhandbook.com/termination-signals/
[13]: https://itsfoss.com/wp-content/uploads/2022/11/customize-htop-in-ubuntu.png
[14]: https://itsfoss.com/wp-content/uploads/2022/11/change-htop-colors-in-linux.png
[15]: https://itsfoss.com/wp-content/uploads/2022/11/explore-available-meters-in-htop.png
[16]: https://itsfoss.com/wp-content/uploads/2022/11/add-date-and-time-htop.png
[17]: https://linuxhandbook.com/man-pages/