MacOS – prevent networksetup from asking for password

macosPROXYsudoterminal

I am trying to get rid of password when changing proxy with networksetup. I changed permissions to read and write of /private/etc folder and propagated change to subfolders.

I tried this:

sudo chmod u+s /usr/sbin/networksetup

and it says:

    `/etc/sudoers is world writable`
no valid sudoers sources found, quitting

Changed permission back to readonly, now it says

Unable to change file mode on /usr/sbin/networksetup: Operation not permitted

I also tried with visudo -f /etc/sudoers, but I don't know how to edit file from here. Can you help me out?

EDIT: after running visudo -f/etc/sudoers I get this error. Should I delete the swap file? No additional program is editing sudoers folder

E325: ATTENTION
Found a swap file by the name "/etc/.sudoers.tmp.swp"
          owned by: mainuser   dated: Sun May  1 16:28:25 2016
         file name: /private/etc/sudoers.tmp
          modified: YES
         user name: mainuser   host name: Mains-MacBook-Pro.local
        process ID: 39284
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
-- More --   dated: Sun May  1 16:23:40 2016

(1) Another program may be editing the same file.
    If this is the case, be careful not to end up with two
    different instances of the same file when making changes.
    Quit, or continue with caution.

(2) An edit session for this file crashed.
    If this is the case, use ":recover" or "vim -r /etc/sudoers.tmp"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file "/etc/.sudoers.tmp.swp"
    to avoid this message.

Swap file "/etc/.sudoers.tmp.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
-- More --

EDIT: For future reference, if you edit file manually, make sure to do this: https://discussions.apple.com/thread/7262040?start=0&tstart=0

Best Answer

Open Terminal and enter sudo visudo to modify the file /etc/sudoers.

Change the part:

## User privilege specification
##
root ALL=(ALL) ALL
%admin  ALL=(ALL) ALL

to

## User privilege specification
##
root ALL=(ALL) ALL
%admin  ALL=(ALL) ALL
your_username ALL=(ALL) NOPASSWD: /usr/sbin/networksetup

and save the file.

If you don't know vi: after entering sudo visudo you have to change to insert mode by hitting i. Enter the additional line as indicated above. To leave insert mode hit esc. Then enter :wq and the Enter key to write the modified file to disk and quit vi.

Using sudo networksetup ... shouldn't require a password anymore.