How to run privileged programs without elevation by sudo?
I got the mtr
ping-traceroute combo util through Homebrew and I'd like to run on new Terminal windows without having to become superuser (or elevation, sudoing, whatever it's called on UNIX) each time first, or, if there's no way around it, have the program ask me for credentials on the spot like some commands do, e.g; scutil
, sputil
, brew
itself.
A while ago I got into the habit of creating [very simple] scripts, then added the scripts directory to $PATH
for simple tasks like, for instance, to flush the DNS cache I type anywhere flushdns
and it asks me for my creds if I wasn't elevated before because sudo
is in the script.
I thought about doing something similar to this but (1) I'd have to use another command since mtr
is already in the $PATH and, (2) I don't know how to work it with the arguments that the utility needs.
I'd appreciate any suggestions you have, or, a link to an existing question--I didn't find because because the elevation/superuser thing, I think. :/
The low hanging fruit is to make an alias in your shell to avoid having to type sudo
Assuming bash, add this to your ~/.bash_profile (or other dot file of choice)
alias mtr=‘/usr/bin/sudo /usr/local/sbin/mtr’
Then close your command / shell / window or paste the alias command after saving it for next shell start to be active. I would recommend against adding this to the sudoers file, but I’m sure someone will edit this to post how to do that or post another answer if that’s viable. I’d also advise against moving the tool or setting the uid of the tool to be root permissions on the filesystem. Discussion on that decision is happily well covered at the source:
- https://github.com/traviscross/mtr/issues/204#issuecomment-487325796