Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eriknomitch/99ad47b6b2d6c293c5412a3f7e5a9db6 to your computer and use it in GitHub Desktop.
Save eriknomitch/99ad47b6b2d6c293c5412a3f7e5a9db6 to your computer and use it in GitHub Desktop.
How to use sudo without a password

Sudo without a password (Linux and MacOS)

Get your username

$ whoami

Remeber this for later.

Edit the sudoers file (via visudo)

$ sudo visudo # This is a special command which edits that file.

Type your password if it prompts you. This will be the last time you have to.

Depending on your $EDITOR ENV variable, either vi or nano should have brought up /etc/sudoers.

Find this area of the file:

# root and users in group wheel can run anything on any machine as any user
root            ALL = (ALL) ALL
%admin          ALL = (ALL) ALL

Add a line after it (below). Rreplace <USERNAME> with your username you got from whoami:

# root and users in group wheel can run anything on any machine as any user
root            ALL = (ALL) ALL
%admin          ALL = (ALL) ALL
<USERNAME>      ALL = (ALL) NOPASSWD: ALL

Write the file and exit.

If editing in vim

<ESC>:wq<ENTER>

If editing in Nano

<Ctrl-O><ENTER>(For the confirmation)<Ctrl-X>

You should never be prompted for a sudo password again.

Warning: Never edit /etc/sudoers manually

You should never edit /etc/sudoers manually since you may (among other things) inadvertantly cause the file permissions to change. This renders sudo unusable and you will not be able to change the permissions back.

Is this secure?

In the vast majority of cases, the security impact is negligible. See this write up if you want to know why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment