SUSE Support

Here When You Need Us

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

This document (7002705) is provided subject to the disclaimer at the end of this document.

Environment

SUSE Linux Enterprise Server 15
SUSE Linux Enterprise Server 12
SUSE Linux Enterprise Server 11

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:  making changes directly to the /etc/sudoers file is discouraged, and the visudo utility should be used instead.  Also see Additional Information below regarding /etc/sudoers.d.

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>

This will then prompt you for the root password first 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 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, for 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 run.

The same thing can be done for user groups, simply use 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 recent sudo versions the /etc/sudoers includes the following syntax: 
 
## Read drop-in files from /etc/sudoers.d
@includedir /etc/sudoers.d

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

 

Disclaimer

This Support Knowledgebase provides a valuable tool for SUSE customers and parties interested in our products and solutions to acquire information, ideas and learn from one another. Materials are provided for informational, personal or non-commercial use within your organization and are presented "AS IS" WITHOUT WARRANTY OF ANY KIND.

  • Document ID:7002705
  • Creation Date: 19-Feb-2009
  • Modified Date:18-Jul-2023
    • SUSE Linux Enterprise Server

< Back to Support Search

For questions or concerns with the SUSE Knowledgebase please contact: tidfeedback[at]suse.com

tick icon

SUSE Support Forums

Get your questions answered by experienced Sys Ops or interact with other SUSE community experts.

tick icon

Support Resources

Learn how to get the most from the technical support you receive with your SUSE Subscription, Premium Support, Academic Program, or Partner Program.

tick icon

Open an Incident

Open an incident with SUSE Technical Support, manage your subscriptions, download patches, or manage user access.