How to Add User to Sudoers (on Linux)?

Starting#

In order to add user to sudoers on Linux (Ubuntu, Debian, CentOS), first we want to login as necessary, and create a new sudo user, as follows:

1. Login as administrator

$ ssh root@some-vm

2. Create a new sudo user

$ adduser UserName

Replace UserName with the actual username for your new user.

Next create a password for your new user by running:

$ passwd UserName

If successful, the system should respond with “all authentication tokens updated successfully”.

Add Users to Sudo Group#

1: Open the Sudoers File in an Editor

In the terminal, run the following command:

$ visudo

This will open the /etc/sudoers file in a text editor.

2: Add the New User to file

Scroll down to find the following section:

## Allow root to run any commands anywhere

root ALL=(ALL) ALL

Right after this entry, add the following text:

UserName ALL=(ALL) ALL

Replace UserName with the username you created previously. This section should look like the following:

## Allow root to run any commands anywhere

root ALL=(ALL) ALL

UserName ALL=(ALL) ALL

Save the file and exit.

3: Test Sudo Privileges for the User Account

Switch user accounts with the su (switch user) command:

$ su — UserName

Enter the password for the account, if prompted. The terminal prompt should change to include UserName.

List the contents of the /root directory:

$ sudo ls —la /root

Enter the password for this user when prompted. The terminal should display a list of all the directories in the /root directory.

comments powered by Disqus