Terminal - don't require password for shell script to run

I am running Yosemite 10.10.5 and I have a shell script which I want to use to toggle whether my wifi connections run through tor or not. This uses the networksetup a couple of times and so asks for my password each time I run or cancel it.

Is there a way to make a shell script run without requiring my password each time?

Thanks!


You can add a rule to the /etc/sudoers file to give your user access rights to networksetup without a password.

  1. Note the user name of the user account which should be allowed to run the command (you can get the user name by running whoami in Terminal)
  2. Log in as an admin user
  3. Open Terminal
  4. VISUAL=nano sudo visudo - opens the sudoers file in a kind of friendly editor
  5. Jump to the bottom
  6. Add

    USERID-FROM-STEP1 ALL = NOPASSWD: /usr/sbin/networksetup
    

    followed by an empty line(!)

  7. Save and quit

You should now be able to run networksetup as the user from step 1 without a password prompt by simply entering sudo networksetup.