How to add users to Sudoers in Ubuntu

sudo is a command line program that allows trusted users to run commands as root or other users.

We will show two ways to escalate sudo privileges for users. The first way is to add users to the sudoers file. This file contains the following information:

The second option is to add the user to the sudo user group in the sudoers file. By default, Debian-based distributions, such as Ubuntu, Linux Mint, and members of the sudo user group have sudo permissions.

1. Add users to the sudo user group##

On Ubuntu, the easiest way to grant sudo permissions to a user is to add the user to the "sudo" user group. Members of this group can execute any command as root, and use the sudo command to authenticate with their own password when prompted.

We assume that the user already exists, if you want to create a new user, check this Guide

To add a user to the user group, run the following command as root or other sudo user. Make sure you replace "username" with the username you want to authorize:

usermod -aG sudo username

Authorizing sudo permissions in this way is effective for most user scenarios.

To ensure that the user has sudo permissions, run the whoami command:

sudo whoami

You will be prompted for a password. If the user has sudo privileges, this command will print "root":

root

If you get any error message: "user is not in the sudoers file", it means this user does not have sudo permission.

2. Add users to the sudoers file##

The sudo permissions of users and user groups are defined in the file /etc/sudoers. Adding a user to this file allows you to customize access commands and configure custom security policies.

You can configure the user's sudo access rights by modifying the sudoers file or creating a configuration file in the /etc/sudoers.d directory. All files in the directory will be included in the sudoers file.

Always use visudo to edit the /etc/sudoers file. This command will detect the syntax error of the file when saving the file. If there are any errors, the file will not be saved. If you use a text editor to edit this file, a syntax error may cause you to lose sudo access.

Usually, visudo uses vim to open the /etc/sudoers file. If you haven't used Vim, and you want to use nano as the default editor to edit this file, run:

EDITOR=nano visudo

For example, you want to allow users to run sudo commands without prompting for a password. To do this, you can open the /etc/sudoers file:

visudo

Scroll to the end of the file and add the following:

username  ALL=(ALL) NOPASSWD:ALL

Save the file and exit the editor. Don't forget to change "username" to the username you want to authorize.

Another typical example is to only allow users to run specified commands through sudo. For example, to allow only mkdir and rmdir to run, you can do this: /etc/sudoers

username ALL=(ALL) NOPASSWD:/bin/mkdir,/bin/rmdir

Instead of editing the sudo file directly, you can also accomplish this by creating the same file in the /etc/sudoers.d folder. Add the same rules as added to the sudoer file:

echo "username  ALL=(ALL) NOPASSWD:ALL"| sudo tee /etc/sudoers.d/username

This approach will make sudo authority management better maintained. The name of the file is not important. The usual practice is that the file name is the same as the user name.

Three, summary##

Authorizing users with sudo permissions on Ubuntu is very simple, you only need to add users to the "sudo" user group.

Recommended Posts

How to add users to Sudoers in Ubuntu
How to add swap partition in Ubuntu
How to install Helm in Ubuntu
How to install mysql in Ubuntu 14.04
How to install mysql in Ubuntu 14.04
How to add swap space on Ubuntu 20.04
How to easily compile openJDK in Ubuntu
How to install cuda10.1 driver in Ubuntu
How to delete redundant kernels in Ubuntu
How to delete redundant kernels in Ubuntu
How to install ROS Noetic in Ubuntu20.04
How to set static IP in ubuntu14.04
How to add custom modules in Python
How to modify software source in Ubuntu 7.10
How to open root account in Ubuntu20.04
How to add background music in python
How to hide applications in Ubuntu Dash?
How to upgrade to Ubuntu 20.04
How to upgrade to Ubuntu 20.04
How to modify time zone and time in ubuntu
How to configure TensorFlow use environment in Ubuntu
How to install python in ubuntu server environment
How to use dpkg command in Ubuntu system
How to open the ubuntu system in win10
Explain how to add swap partition on Ubuntu 16.04
How to configure /var/log/messages in Ubuntu system log
How to add logging module to Nginx on Ubuntu 16.04
How to add and delete users on CentOS 8
How to install Hadoop in standalone mode on Ubuntu 18.04
How to add the gzip module to Nginx on Ubuntu 14.04
How to view detailed network routing table in Ubuntu
How to use Putty to log in to ubuntu installed in VirtualBox
How to modify time zone and time in ubuntu system
Example of how to modify ip address in Ubuntu20.04
How to create a Python virtual environment in Ubuntu 14.04
How to upgrade to Ubuntu 16.04 LTS
How to control the ubuntu system in win10 through ssh
How to install memcache and start it in ubuntu environment
How to compile and install PHP and Nginx in Ubuntu environment
How to map shared disk to window in linux under Ubuntu
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to wrap in python code
How to install PHP7.4 in CentOS
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to compile ijkplayer-android under ubuntu
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install Go on Ubuntu 20.04