Skip to content

Sudoers

Add user to sudoers

From https://linuxize.com/post/how-to-add-user-to-sudoers-in-ubuntu/

Connect as root

Log in as root with root password

sh
su -

Add my-user to sudoers

INFO

Replace my-user with your username.

sh
visudo

Add a new line for my-user

sh
my-user  ALL=(ALL:ALL) ALL
/etc/sudoers

Disconnect and reconnect to apply changes

sh
exit

Now you can use sudo with my-user

sh
sudo apt update

If sudo don't ask for password

From /etc/sudoers

If you see ALL=(ALL) NOPASSWD:ALL, replace it:

diff
-my-user  ALL=(ALL) NOPASSWD:ALL
+my-user  ALL=(ALL) ALL
/etc/sudoers

If you see @includedir /etc/sudoers.d at the end of the file, check the files in /etc/sudoers.d/ directory.

From /etc/sudoers.d/

sh
sudo visudo -f /etc/sudoers.d/90-cloud-init-users

If you see ALL=(ALL) NOPASSWD:ALL, replace it:

diff
-my-user  ALL=(ALL) NOPASSWD:ALL
+my-user  ALL=(ALL) ALL
/etc/sudoers.d/90-cloud-init-users

For a Raspberry Pi

The file is /etc/sudoers.d/010_pi-nopasswd

sh
sudo visudo -f /etc/sudoers.d/010_pi-nopasswd

If you see ALL=(ALL) NOPASSWD:ALL, replace it:

diff
-my-user  ALL=(ALL) NOPASSWD:ALL
+my-user  ALL=(ALL) ALL
/etc/sudoers.d/010_pi-nopasswd

Check sudo command

sh
sudo -k     # Force sudo to ask for password
sudo ls     # Run a command with sudo

MIT License