Running bash script without asking for password

bashpasswordterminal

I wrote a little shell script that switches some settings that I have to switch a lot. I saved it as a .command file, so double-clicking will run it. Now everytime I run it, it's asking for my password (although just changing those settings doesn't require any password when I do it by hand).
Is it possible to make the bash script run, without it asking for a password everytime? All it does is switches my proxy settings off…

The script:

#!/bin/bash
osascript -e 'quit app "MyAppsAnywhere"'
networksetup -setautoproxystate Wi-Fi off
exit 0

Best Answer

You could use

sudo networksetup -setautoproxystate Wi-Fi off

and then configure sudoers(5) to allow that exact command (or any networksetup invocation (or any command whatsoever)) without a password, though this generally requires fiddling around with visudo(8) and if you get things wrong may lock you out of future use of sudo(1). Open a root shell, make a backup of /etc/sudoers, edit the file by running visudo, test it, and use root shell to restore the backup of the config if things go awry. Relevant sudoers(5) config lines would be along the lines of

# only with these args
yourloginnamehere ALL=(root) NOPASSWD: /usr/sbin/networksetup-setautoproxystate Wi-Fi off, /usr/sbin/networksetup-setautoproxystate Wi-Fi on

# any arguments to the command
yourloginnamehere ALL=(root) NOPASSWD: /usr/sbin/networksetup

# no password prompts at all from sudo ever
yourloginnamehere  ALL=(ALL) NOPASSWD: ALL

Also visudo may run a strange editor by default, so you might want to read up on the EDITOR and DISPLAY environment variables and related sudo questions and documentation before you get stuck in vi...