Run MTR on MacOS without sudo
Coming from Linux/Windows it rather feel bothersome to run mtr
with sudo
each time if you want to quickly diagnosis a hostname/IP address.
Since, mtr
require access to raw packets. MacOS permissions doesn't allow it to access those raw packets without sudo
, fortunately we can fix that by setting ownership of executables to root and SUID by following steps -
Step 1: Find path of mtr executables
First we need to find the path of mtr-packet
which is another program which handles the networking and does the actual tracing.
❯ command -v mtr-packet
/opt/homebrew/sbin/mtr-packet
Here we can see, it's residing at /opt/homebrew/sbin
directory, but that's not the true location of it, let's figure out why!
❯ ls -lsh $(command -v mtr-packet)
0 lrwxr-xr-x 1 jugal admin 34 Aug 31 23:28 /opt/homebrew/sbin/mtr-packet -> ../Cellar/mtr/0.96/sbin/mtr-packet
Ah, we can see /opt/homebrew/sbin/mtr-packet
is actually linked to /opt/homebrew/Cellar/mtr/0.96/sbin/mtr-packet
, that's where we are going to apply the necessary permissions.
Step 2: Change ownership of executables to root
Change ownership of mtr-packet
to root
user by issuing following command -
❯ sudo chown root /opt/homebrew/Cellar/mtr/0.96/sbin/mtr-packet
Step 3: Set SUID bit
We also need to set SUID (Set User ID) to 4755
-
❯ sudo chmod 4755 /opt/homebrew/Cellar/mtr/0.96/sbin/mtr-packet
Run the mtr
command again, and it should work without prompting you for sudo
password.
Note: The paths given as example are taken from Homebrew on M4 cores, Mac Intel architecture's path may differ.
Reference(s):
Last updated