Get Real Results with HTOP on MacOS - barkwoofdog/howtowithdog GitHub Wiki
Overview
Homebrew is a package manager that works great with MacOS. You can use it to get utilities like yt-dlp, docker, nmap, and what we are going to discuss today, HTOP!
HTOP is an improved version of TOP that allows you to view your system resources from within a terminal. However if you are observant, or if you have a problem with Lulu's extension eating up your memory you are going to notice that the results are quite inconsistent with Mac's own GUI Utility for resource monitoring, Activity Monitor. There are quite a few discrepancies, and in some case you cannot even see some programs resource usage. The answer for why this is happening is quite simple!
The HTOP program is not running as the root user!
Unlike Activity Monitor which has privilege to run at root level when you execute it, when you execute HTOP you are running as your own user who installed the program.
Let's go through how to fix this from start to finish.
Instructions
-
Install Homebrew if you do not already have it installed.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Run through the installation of the package manager, and then proceed to step 2 -
Install HTOP with Homebrew
brew install htop
-
Once HTOP is installed, you need to find out where the binary is located. With Homebrew, we are going to have to visit a few directories.
which htop
will reveal that the binary is located in/opt/homebrew/bin/htop
run the following now that we have this location
ll /opt/homebrew/bin/htop
this will reveal that this file is actually a link another located with in the parent directory structure, specifically @/opt/homebrew/Cellar/htop/3.2.2/bin/htop
now, one more time run this command to see just what is up
ll /opt/homebrew/Cellar/htop/3.2.2/bin/
-
We need to examine the output of the last command. It should look something like this
-r--r-xr-x 1 yourusername admin 255K Mar 24 2023 htop
As you can see, yourusername is the owner of the actual binary file, so it will execute with those privileges. However, that is not really what we want. What we want is the actual function of htop! -
We are going to need to set the SUID bit within the file permissions. You can read more about special bits here
Essentially, what the SUID bit does is it will always execute a file with the permissions of its owner, and not the user that is actually invoking the command. We can take advantage of this by changing thehtop
file owner toroot
. Run the following
sudo chown root /opt/homebrew/Cellar/htop/3.2.2/bin/htop
-
Now run
ll
on the directory with the binary. You are going to see
-r--r-xr-x 1 root admin 255K Mar 24 2023 htop
Now, we can finish off this process by setting the SUID bit on this file.
sudo chmod u+s /opt/homebrew/Cellar/htop/3.2.2/bin/htop
when you executell
again, you should see a red highlight (or whatever your specified color is for executables with SUID set) -
Enjoy full HTOP. Please be aware that you are now running this process AS ROOT and can kill any process from within the program. Please use responsibly on your system.