Breadcrumbs

Using sudo to allow non-root users to perform root level functions

By ValkaTR, 8 July, 2023

Situation

When administering a server, there may be some users who need limited root functionality to access certain, but not all, root functions.

Resolution

sudo (superuser do) allows you to configure non-root users to run root level commands without being root.  Access can be given by the root level administrator through configuration of the /etc/sudoers file.  Please note that making changes directly to the /etc/sudoers file is discouraged, and that the visudo utility should be used.

The syntax for using sudo is fairly simple, you specify the name of the command preceded by 'sudo'.  For example, if a user needed to run /sbin/chown, you could run it with the command:

sudo /sbin/chown <user_name> <file_name>

Which would then prompt you for the root password and then run the command.  You can configure sudo to allow access to files by using the root password, the user's own password, or with no password.

Configuring who's password to use

Here is how to configure each:


1.  For non-root user access by entering the root password:   
 

You need to do nothing, this is the default.


2.  For non-root user access by entering their own password:   
 

REM out the "Defaults targetpw" line, and the "ALL  ALL=(ALL) ALL" line in the /etc/sudoers file. (NOTE that if you rem out the "Defaults targetpw" line and not the other, all users will have all root level access to utilities by simply putting in their own password).   
 
Here is what it looks like in the /etc/sudoers file: 
# In the default (unconfigured) configuration, sudo asks for the root password.
# This allows use of an ordinary user account for administration of a freshly
# installed system. When configuring sudo, delete the two
# following lines:
# Defaults targetpw   # ask for the password of the target user i.e. root
# ALL     ALL=(ALL) ALL   # WARNING! Only use this together with 'Defaults targetpw'!
Notice that the two lines have been commented in this listing.  By commenting these lines, all users given permissions to utilities will now be asked for their own password, not the root password (giving permissions to utilities will be discussed in a minute).


3.  For non-root user access by entering no password:   
 

Edit the /etc/sudoers file to include the following:
# User privilege specification
root        ALL=(ALL) ALL
<user_name>    ALL=NOPASSWD: ALL
The commands above will allow the user named <user_name> to run root-level utilities without using a password.


Setting up permissions for non-root users

Now that you know how to change what password to use (or not to use), let's move on to permissions.

In the "User privilege specification" section, you can set privileges at the user or group levels.  Here is what it looks like by default:

# User privilege specification
root    ALL=(ALL) ALL
# Uncomment to allow people in group wheel to run all commands
# %wheel        ALL=(ALL) ALL
# Same thing without a password
# %wheel        ALL=(ALL) NOPASSWD: ALL
# Samples
# %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users  localhost=/sbin/shutdown -h now

To give privileges to users, add users to the area under the "User privilege specification".  As an example, to give a user named Joe privileges to run chown, you would add the following entry:

# User privilege specification
root    ALL=(ALL) ALL
Joe    ALL=PASSWD:    /bin/chown

Using this syntax, all options for chown will be available to Joe.  If you want to limit what parameters can be used by Joe, list them in the entry like this:

# User privilege specification
root    ALL=(ALL) ALL
Joe     ALL=PASSWD:   /bin/chown -R,
                      /bin/chown -V,
                      /bin/chown -R-V

Notice that there are three separate entries.  One that allows -R, one for -V, and one for -R-Vsudo allows will only allow multiple parameters if you add them specifically (and only in the order that you specify), thus the -R-V in addition to the individual parameter listings.

You can also allow some commands to be used with no password while others require a password.  Here is an example:

Joe    ALL=NOPASSWD: \
                    /bin/chown -R,
           PASSWD:
                    /bin/chown -V,
                    /bin/chown -R-V

With the above configuration, the /bin/chown -R command will not require a password, /bin/chown -V and /bin/chown -R-V will require a password to run, but they will be allowed.

The same thing can be done for user groups, you just need to put it in the following syntax:

# Uncomment to allow people in group wheel to run all commands
# %wheel        ALL=(ALL) ALL
# Same thing without a password
# %wheel        ALL=(ALL) NOPASSWD: ALL
# Samples
# %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users  localhost=/sbin/shutdown -h now

You just need to specify the group name with a % in front of it.  The same syntax for users applies to groups.

Additional Information

With resent sudo versions the /etc/sudoers include the following syntax:

## Read drop-in files from /etc/sudoers.d
@includedir /etc/sudoers.d

With above syntax, new configuration could be added without updating /etc/sudoers. For full information, kindly check the manual pages: man sudoers 

Tags

Comments