如何在 Linux 中安装和使用 htop
top htop
![](/data/attachment/album/202303/14/162909klvn2plolhwcko2k.jpg)


Windows 有其著名的任务管理器。Linux 有几个 GUI 和 [命令行的系统监视器](https://itsfoss.com/linux-system-monitoring-tools/)。每个 Linux 系统都都有几个这样的监视器。


在命令行中，`top` 命令可能是用于快速检查系统资源使用情况的最佳命令。


除了查看进程之外，[使用 top 命令](https://linuxhandbook.com/top-command/) 可能会很麻烦。而这就是 `htop` 的优势所在。抛开双关语不谈，`htop` 是一个类似于 `top` 的实用程序，但具有更好的、用户友好的界面。


在本指南中，我将向你展示如何在 Linux 中安装和使用 `htop`。


### 在 Linux 中安装 htop 实用程序


在大多数的 Linux 发行版中你不会找到预安装的 `htop`，但作为最流行的实用程序之一，你会在几乎每个 Linux 发行版的默认存储库中找到 `htop`。


因此，如果你的机器是基于 Debian/Ubuntu 驱动的，则以下命令应该可以完成你的工作：



```
sudo apt install htop

```

类似的，如果你使用的是 Fedora，则可以使用以下的命令：



```
sudo dnf install htop

```

如果你想避免从源代码构建包，还有一个 Snap 包可用：



```
sudo snap install htop

```

如果你使用的是其它的发行版或者想从源代码构建，你可以随时参考 [htop 的 GitHub 页面](https://github.com/htop-dev/htop)以获得详细说明。


完成安装后，你只需在终端中使用 `htop` 命令，它将反映系统中所有正在运行的进程：



```
htop

```

![安装和使用 htop](/data/attachment/album/202303/14/162940nqvw55vllvvn55nv.png)


在 `htop` 中，每个部分都使用颜色标识，所以让我们看看使用 `htop` 时每种颜色表示什么。


##### htop 中不同的颜色和统计信息表示什么


让我们从 CPU 使用率栏开始，因为它使用的颜色最缤纷多彩。


#### CPU 使用率栏


![htop 中的 cpu 进程](/data/attachment/album/202303/14/162940cooiaz1agogthoi0.png)


* **绿色**：用户进程消耗的资源。
* **蓝色**：表示低优先级线程。
* **红色**：系统（内核）进程使用的 CPU 资源。
* **水蓝色**：表示虚拟化进程。


#### 内存使用率栏


![htop 中的内存栏](/data/attachment/album/202303/14/162940yyc8ao9ykadh8chc.png)


* **绿色**：系统进程正在使用的内存。
* **蓝色**：缓冲页面使用的内存。
* **橙色**：分配给缓存页面的内存。


#### 统计数据


![htop 中的任务统计](/data/attachment/album/202303/14/162940dzgayzx5vqxb1og1.png)


* **1.86** 是最后 1 分钟的平均负载。
* **1.75** 是最后 4 分钟的平均负载。
* **1.47** 是最后 15 分钟的平均负载。
* **Tasks: 166** 表示一共有 166 个正在运行的进程。
* **1249 thr** 表示这 166 个进程由 1249 个线程处理。
* **1 running** 表示在这 166 个进程中，只有一个进程处于运行中。
* **The load average** 表示一段时间内的平均系统负载。由于我的系统是 Hexa-Core，所以 6.0 以下的是没问题的。这个数字可能会超过，比如 6.1，所以即将要运行的进程必须等待正在进行的任务完成。
* **Uptime** 表示你登录后运行的时长。


现在，让我们跳到实际的实现部分。


### 如何在 Linux 中使用 htop


由于 `htop` 主要用于检查系统资源，让我们看看如何根据资源消耗对进程进行排序。


#### 根据资源消耗对进程进行排序


根据 CPU 和内存使用情况对进程进行排序最简单的方法是使用鼠标指针。将光标悬停在 CPU 或内存部分上，然后单击其中任何一个。


在那里你会看到一个三角形的图标 `△`，你可以根据它从最高到最低的资源消耗对进程进行排序：


但是，如果你正在处理远程服务器，你可能无法使用鼠标，在这种情况下，你可以使用键盘快捷键。


按 `F6`，它将显示每个可用的选项来对正在进行的进程进行排序：


![使用键盘快捷键对 htop 中的进程进行排序](/data/attachment/album/202303/14/162941bsc8cvidty8r3rrg.png)


你可以使用方向键选择一个首选的排序选项，然后按回车键，结果应该会如预期的那样反映出来。


#### 搜索特定进程


如果你想查找一个特定的进程及其资源消耗，你可以按 `F3`，它会给你一个搜索提示，如下所示：


![在 htop 中搜索进程](/data/attachment/album/202303/14/162941msrq7cq2s75jsrnw.png)


例如，我搜索 `htop`，它用浅橙色高亮显示了进程。你可以按 `F3` 获得下一个结果。


#### 过滤正在运行的进程


虽然搜索可能会让你获得预期的结果，但我发现使用关键字的过滤过程更加有效，因为它提供了一个过程列表。


要过滤进程，你必须按 `F4` 并输入进程的名称。例如，我过滤了与 `gnome-boxes` 相关的进程：


![在 htop 中过滤进程](/data/attachment/album/202303/14/162941vcpczh3ufupynb9j.png)


#### 杀死进程


一旦找到最耗资源和不必要的进程，你只需按 `F9`，它就会向你显示终止信号：


![在 htop 中杀死进程](/data/attachment/album/202303/14/162942aff2h9bqqlf9y66b.png)


我无法介绍所有的 15 个终止信号，我们有一个关于 [不同终止信息](https://linuxhandbook.com/termination-signals/) 的单独指南，因此如果你想了解更多信息，可以参考该指南。


但我会建议你首先使用 `SIGTERM`，因为它是终止进程的最有效和友好的方式。


#### 自定义 htop


在这里，我的目标是添加日期和时间并将配色方案更改为单色。


首先，按 `F2`，它会出现设置提示，允许用户更改 `htop` 的外观：


![在 ubuntu 中自定义 htop](/data/attachment/album/202303/14/162942accxdvaacq2ced8x.png)


首先，将鼠标悬停在各个 `Colors` 部分并按回车键，这将允许我们更改配色方案。从那里，选择 `Monochromatic` 选项并按回车键保存更改：


![在 Linux 中更改 htop 颜色](/data/attachment/album/202303/14/162943p8a4m4zjtzvx4j88.png)


现在，返回到设置选项，然后使用向左方向键探索可用的指标：


![探索 htop 中可用的指标](/data/attachment/album/202303/14/162943k4gzg668488zbtv4.png)


因为我打算添加日期和时间，所以我必须在找到它的选项后按回车键。


在这里，它允许你将日期和时间放置在左右两列中，你可以使用向上和向下的方向键更改列的顺序。


所以我将日期和时间指标放在最后一个样式选项中（你可以使用空格键更改样式）：


![在 htop 中添加日期和时间](/data/attachment/album/202303/14/162943qsg4onc9gei4eedh.png)


完成日期和时间指标的对齐后，按回车键保存更改，然后按 `F10` 关闭设置提示。


### 总结


在本指南中，我解释了如何在不同的 Linux 发行版中安装 `htop` 实用程序，以及如何使用 `htop` 的一些基本功能来有效的管理系统资源。


但是 `htop` 可以做更多的事情，要了解更多信息，你可以随时参考它的手册页，我们有一个详细的指南，告诉你如何 [在 Linux 中充分利用手册页](https://linuxhandbook.com/man-pages/)。




---


via: <https://itsfoss.com/use-htop/>


作者：[Sagar Sharma](https://itsfoss.com/author/sagar/) 选题：[lkxed](https://github.com/lkxed) 译者：[perfiffer](https://github.com/perfiffer) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

Windows has its famous task manager. Linux has several GUI and [command line system monitors](https://itsfoss.com/linux-system-monitoring-tools/). 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](https://linuxhandbook.com/top-command/?ref=itsfoss.com) 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 won’t 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 you’re on Fedora, you can use the given command:

`sudo dnf install htop`


And there’s also a snap package available if you like to avoid building packages from the source:

`sudo snap install htop`


If you’re on something else or want to build from a source, you can always refer to [htop’s GitHub page](https://github.com/htop-dev/htop?ref=itsfoss.com) for detailed instructions.

Once you’re 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](https://itsfoss.com/content/images/wordpress/2022/11/install-and-use-htop.png)

In htop, there is a color coding for the individual section, so let’s have a look at what each color indicates while using htop.

#### What different colors and statistics indicate in htop

So let’s start with the CPU usage bar, as it utilizes the maximum number of colors.

### CPU usage bar

![cpu process in htop](https://itsfoss.com/content/images/wordpress/2022/11/cpu-process-in-htop-1.png)

**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](https://itsfoss.com/content/images/wordpress/2022/11/memory-bar-in-htop.png)

**Green:**Memory being utilized by system processes.**Blue:**Memory used by buffer pages.**Orange:**Memory allocated for cache pages.

### Statistics

![task statistics in htop](https://itsfoss.com/content/images/wordpress/2022/11/task-statistics-in-htop.png)

**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.**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.**Uptime**tells how long the system has been running since last reboot.

Now, let’s jump to the actual implementation part.

## How to use htop in Linux

As the htop is mainly used to check for system resources, let’s 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:

![sort processes based on resourse usage](https://itsfoss.com/content/images/wordpress/2022/11/sort-processes-based-on-resourse-usage.gif)

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](https://itsfoss.com/content/images/wordpress/2022/11/sort-processes-in-htop-using-keyboard-shortcut.png)

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](https://itsfoss.com/content/images/wordpress/2022/11/search-processes-in-htop.png)

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](https://itsfoss.com/content/images/wordpress/2022/11/filter-processes-in-htop.png)

### 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](https://itsfoss.com/content/images/wordpress/2022/11/kill-process-in-htop.png)

I can’t cover all 15 termination signals, we have a separate guide on [different termination signals](https://linuxhandbook.com/termination-signals/?ref=itsfoss.com), 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](https://itsfoss.com/content/images/wordpress/2022/11/customize-htop-in-ubuntu.png)

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](https://itsfoss.com/content/images/wordpress/2022/11/change-htop-colors-in-linux.png)

Now, go back to the setup option, and from there, use the left arrow key to explore available meters:

![explore available meters in htop](https://itsfoss.com/content/images/wordpress/2022/11/explore-available-meters-in-htop.png)

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](https://itsfoss.com/content/images/wordpress/2022/11/add-date-and-time-htop.png)

Once you are done aligning the date and time meter, press the enter key to save changes and **F10** to close the setup prompt.

## More than htop?

htop is good. But there are other similar tools that might be better than it. Check them out if you want to.

[5 htop Alternatives to Monitor Your Linux SystemLooking for htop alternatives? Here are a few good options to monitor the system usage in real-time in Linux.](https://itsfoss.com/htop-alternatives/)![](https://itsfoss.com/content/images/wordpress/2022/11/htop-alternatives.png)


![](https://itsfoss.com/content/images/wordpress/2022/11/htop-alternatives.png)

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](https://linuxhandbook.com/man-pages/?ref=itsfoss.com).