How-To: Passwordless Sudo

Published on .

This one is as much for you as it is for me. Every time I install a new system, I forget how to set the /etc/sudoers file so that I can execute certain applications as root using sudo without having to enter my password. This is very useful for scripts, or for harmless commands that you need te execute a lot – like vpnc – but still require root permissions.

Start by opening the editor of the sudoers-file:

$> sudo visudo

This will open the file in vim, which you might not be familiar with. No problem, just type i which puts it in insert-mode and it’ll work as any other editor.

Append a line in the following format at the end:

username ALL=NOPASSWD:command1, command2, ...

For example, mine:

david ALL=NOPASSWD:/usr/sbin/vpnc, /usr/sbin/vpnc-disconnect

As you can see, you need to use the full path for every binary. If you don’t know them by heart, find out using the whereis command:

$> whereis -b vpnc
vpnc: /usr/sbin/vpnc /etc/vpnc.conf /etc/vpnc /usr/share/vpnc

Usually the first result is the correct one.

Save the sudoers file by pressing ESC and typing :wq return. Now you can use sudo on the commands you entered without having to type your password. Have fun!

PS: There’s also a way to allow passwordless sudo for all commands, but I’m not telling how since that really isn’t safe. You’ve run away from Windows for a reason, remember? This is one of them ;) .

David Verhasselt

Senior full-stack engineer with 5 years of experience building web applications for clients all over the world.

Interested in working together?

Find out what I can do for you or get in touch!

Like this? Sign up to get regular updates