Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

macOS: run without sudo

Wataru Ashihara edited this page Jan 3, 2018 · 6 revisions

Translations: 日本語

On macOS, some process information (CPU/memory usage, process state, etc) is not available:

 PPID   PID USER      PRI  NI  VIRT   RES S CPU% MEM%   TIME+  Command
    0     0 root       21   0     0     0 ?  0.0  0.0  0:00.00 kernel_task
    0     1 root       17   0     0     0 ?  0.0  0.0  0:00.00 └─ launchd
    1 99738 _gamecont  40   0     0     0 ?  0.0  0.0  0:00.00    ├─ gamecontrollerd
    1 99710 root       17   0     0     0 ?  0.0  0.0  0:00.00    ├─ com.apple.ifdrea
    1 99688 root       17   0     0     0 ?  0.0  0.0  0:00.00    ├─ nehelper
    1 98468 wsh        17   0 2438M  2136 ?  0.0  0.0  0:00.70    ├─ /System/Library/PrivateFra

unless htop is executed via sudo:

 PPID   PID USER      PRI  NI  VIRT   RES S CPU% MEM%   TIME+  Command
    0     0 root       32   0 71.1G 2569M ? 12.1 31.4  3h23:10 kernel_task
    0     1 root       17   0 2423M  9080 W  0.9  0.1 19:51.93 └─ /sbin/launchd
    1 99738 _gamecont  40   0 2414M  6792 W  0.0  0.1  0:01.15    ├─ /usr/libexec/gamecontrolle
    1 99710 root       17   0 2405M  2700 W  0.0  0.0  0:00.02    ├─ /System/Library/CryptoToke
    1 99688 root       17   0 2416M  3044 W  0.0  0.0  0:00.06    ├─ /usr/libexec/nehelper
    1 98468 wsh        17   0 2438M  2136 W  0.0  0.0  0:00.70    ├─ /System/Library/PrivateFra

To obtain these info without sudo, give setuid(2) flag to htop and chown it to root.

First, check the mode:

$ ls -l /usr/local/bin/htop
lrwxr-xr-x 1 wsh admin 29 Jan  2 18:29 /usr/local/bin/htop -> ../Cellar/htop/2.0.2/bin/htop*

$ ls -l /usr/local/Cellar/htop/2.0.2/bin/htop
-r-xr-xr-x 1 wsh admin 128544 Jul 22  2016 /usr/local/Cellar/htop/2.0.2/bin/htop*

Then setuid and chown it:

$ chown root:wheel /usr/local/bin/htop

$ sudo chown root:wheel /usr/local/bin/htop

$ ls -l /usr/local/bin/htop /usr/local/Cellar/htop/2.0.2/bin/htop
-r-sr-xr-x 1 root wheel 128544 Jul 22  2016 /usr/local/Cellar/htop/2.0.2/bin/htop*
lrwxr-xr-x 1 wsh  admin     29 Jan  2 18:29 /usr/local/bin/htop -> ../Cellar/htop/2.0.2/bin/htop*

Now htop works fine without sudo!

This is what top does:

ls -l /usr/bin/top
-r-sr-xr-x 1 root wheel 87952 May  5  2016 /usr/bin/top*
Clone this wiki locally