Skip to content

Instantly share code, notes, and snippets.

@HariSekhon
Last active November 25, 2025 05:38
Show Gist options
  • Select an option

  • Save HariSekhon/89da0a64ab193692265b3dc3a4d6632b to your computer and use it in GitHub Desktop.

Select an option

Save HariSekhon/89da0a64ab193692265b3dc3a4d6632b to your computer and use it in GitHub Desktop.
sudo.md from HariSekhon/Knowledge-Base repo: https://github.com/HariSekhon/Knowledge-Base

Sudo

Elevate to root using the sudo command.

sudo <somecommand>

Elevate to a Root Shell

sudo su

The above is sometimes frowned upon if you want every elevated command logged for strict auditing.

Configure Which Users / Groups Can Sudo

Configure which users and groups can sudo and to which commands by editing the /etc/sudoers file (or sometimes /etc/sudoers.d/... include files).

Use the visudo command because it validates the changes before it allows saving them.

This command drop you into your $EDITOR if it's set (see IntelliJ page), or if not set then it'll open /etc/sudoers in the classic vi editor.

sudo visudo

If you want to add to another file under /etc/sudoers.d/ then:

sudo visudo -f /etc/sudoers.d/hari

Replace hari with your username.

The line you need to add is:

hari        ALL = (ALL) ALL

Ensure if you're create a new file /etc/sudoers.d/hari that you set correct permissions:

sudo chmod 440 /etc/sudoers.d/hari

Passwordless Sudo

To allow your user to use sudo without having to enter their password every 5 minutes, set the line to:

hari        ALL = (ALL) NOPASSWD: ALL

Ensure the above line comes after the following line found by default on macOS:

%admin      ALL = (ALL) ALL

as this %admin line requires a password for all members of the admin group which you will be in on your macOS.

Test Sudo

Test sudo permission by first invalidating your sudo cached credential:

sudo -k

and then retrying a basic command which should return successfully without a password prompt:

sudo -n echo success
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment