To install sudo you must first become root. The easiest way to switch to the root user is by using the su command – Substitute User. Using the command without any user id defaults to the root user. The dash will start the shell as a login shell. Issue the command to switch to the root and enter the password.

su -
Password:

Once you are the root user, let’s install sudo.

apt install -y sudo

Next, you will need to add your user to the sudo group with this command. Remember to change <username> to your username.

usermod -aG sudo <username>

You will now need to log out and back in for the changes to take effect.

Once you have logged back in, executing the following command will allow you to log in as root without a password.

sudo -i

Let’s edit the sudoer’s file and enable passwords for both root and sudo.

sudo visudo

Find these lines and edit.

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

Adding PASSWD before the last ALL in each line will require a password to execute the command.

# User privilege specification
root    ALL=(ALL:ALL) PASSWD:ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) PASSWD:ALL

Entering the following command again will require a password before being given root access.

sudo -i
[sudo] password for mainuser: 

I hope this page was helpful. Why not check out the man pages for more ways this command can be utilized.

Share this content: