How to Set Up sudo on a User in Debian
Updated On: Oct. 31, 2024 Author: Kevin

How to Set Up sudo on a User in Debian

Granting sudo access to a user on a Debian system allows them to execute commands with superuser privileges, essential for performing administrative tasks. This guide will walk you through the steps to create a new user and configure sudo access.

 

Prerequisites

  • You must have root access on your Debian system. If sudo is not installed, switch to the root user with su and install it.

 

Step 1: Install sudo (if not installed)

If sudo is not installed, switch to the root user:

su

Then install sudo using the following command:

apt update && apt install sudo

 

Step 2: Create a New User

If you haven’t created a new user yet, use the following command, replacing username with your desired username:

sudo adduser username

You will be prompted to set a password and enter optional user information.

 

Step 3: Add the User to the sudo Group

To grant the new user sudo privileges, add them to the sudo group with:

sudo usermod -aG sudo username

The -aG option appends the user to the specified group without removing them from other groups.

 

Step 4: Verify sudo Access

To check if the user has been added to the sudo group, run:

groups username

You should see sudo listed among the user’s groups.

 

Step 5: Test sudo Access

Switch to the new user account:

su - username

Test sudo access by executing a command that requires superuser privileges:

sudo apt update

You will be prompted to enter the user's password. If set up correctly, the command should execute without errors.

 

Step 6: Configure sudoers File (Optional)

If you need to customize sudo permissions, edit the /etc/sudoers file using the visudo command, which checks for syntax errors:

sudo visudo

To grant the user full sudo privileges, add the following line:

username ALL=(ALL:ALL) ALL

Replace username with the actual username.