macOS

Execute sudo without password

As you can see from the graphic above using sudo command makes the system obey any given order.

Sometimes can be exhausting having to use sudo all the time to complete tasks. For instance I use Apache for web development and to start the daemon on port 80 one needs to run it as root in order to Apache to be able to bind that port (in fact any port below 1024).

Before we start just be aware that depending on what binary you "expose" this little tweak can become a security risk. NEVER do this for commands such as mv, cp, vim etc.

Let's go! We need to edit the /etc/sudoers file and set the user and the binary that will be allowed to run as root. Open a terminal and type:

sudo nano /etc/sudoers

Find the section "User privilege specification" and below the last line of it insert your rules.

Before Editing:

The sudoers file:
    ##
    ## User privilege specification
    ##
    root ALL=(ALL) ALL
    %admin  ALL=(ALL) ALL
The Fail:
     mteam7 [~] $ sudo /usr/local/bin/apachectl start
    Password:

After Editing:

The sudoers file:
    ##
    ## User privilege specification
    ##
    root ALL=(ALL) ALL
    %admin  ALL=(ALL) ALL
    mteam7 ALL=(ALL) NOPASSWD:/usr/bin/dscacheutil
    mteam7 ALL=(ALL) NOPASSWD:/usr/local/bin/mkdocs
    mteam7 ALL=(ALL) NOPASSWD:/usr/local/bin/nginxctl
    mteam7 ALL=(ALL) NOPASSWD:/usr/local/bin/apachectl
The Win:
     mteam7 [~] $ sudo /usr/local/bin/apachectl start

No password question... it just ran!

Explanation:

    mteam7        ALL=       (ALL)      NOPASSWD:/usr/local/bin/apachectl
    ---------------------------------------------------------------------
     USER    |  TERMINAL | ACTING AS |  COMMAND

Edit the sudoers file as your needs require and press CTRL+O to write the file. CTRL+X to quit nano.

With the setup above I can now run any of these commands with sudo without having to type an admin password. Very handy!

 

© 2019 Mteam7 - All rights lorem ipsum dolor sit amet, consectetur adipiscing elit.*