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.

enter image description here

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.

enter image description here

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:

  • 1
    @dan you are totally right. Anytime you want to edit my posts with a comment hey the edit is better, please do so. I agree - typing the full path on mtr was worth doing, same for sudo. I appreciate the improvement – bmike Jul 20 '19 at 14:07
  • also source ~/.bash_profile spares you having to close your shell for your modified profile to become active – brunobhr Jul 20 '19 at 14:19
  • 1
    @brunobhr Yup - I generally paste the alias in the current term to test it works so it’s usually set. Source gets you to test the syntax right away – bmike Jul 20 '19 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .