10 Useful Sudoers Configurations for Setting ‘sudo’ in Linux

[‘

n

In Linux and other Unix-like operating systems, only the root user can run all commands and perform certain critical operations on the system such as install and update, remove packages, create users and groups, modify important system configuration files and so on.

n

However, a system administrator who assumes the role of the root user can permit other normal system users with the help of sudo command and a few configurations to run some commands as well as carry out a number of vital system operations including the ones mentioned above.

n

Alternatively, the system administrator can share the root user password (which is not a recommended method) so that normal system users have access to the root user account via su command.

n

sudo allows a permitted user to execute a command as root (or another user), as specified by the security policy:

n

    n

  1. It reads and parses /etc/sudoers, looks up the invoking user and its permissions,
  2. n

  3. then prompts the invoking user for a password (normally the user’s password, but it can as well be the target user’s password. Or it can be skipped with NOPASSWD tag),
  4. n

  5. after that, sudo creates a child process in which it calls setuid() to switch to the target user
  6. n

  7. next, it executes a shell or the command given as arguments in the child process above.
  8. n

n

Below are ten /etc/sudoers file configurations to modify the behavior of sudo command using Defaults entries.

n

$ sudo cat /etc/sudoersrn

n

/etc/sudoers File

n

#rn# This file MUST be edited with the 'visudo' command as root.rn#rn# Please consider adding local content in /etc/sudoers.d/ instead ofrn# directly modifying this file.rn#rn# See the man page for details on how to write a sudoers file.rn#rnDefaultstenv_resetrnDefaultstmail_badpassrnDefaultstsecure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"rnDefaultstlogfile="/var/log/sudo.log"rnDefaultstlecture="always"rnDefaultstbadpass_message="Password is wrong, please try again"rnDefaultstpasswd_tries=5rnDefaultstinsultsrnDefaultstlog_input,log_outputrn

n

Types of Defaults Entries

n

Defaults                parameter,   parameter_list     #affect all users on any hostrn[emailxa0protected]_List      parameter,   parameter_list     #affects all users on a specific hostrnDefaults:User_List      parameter,   parameter_list     #affects a specific userrnDefaults!Cmnd_List      parameter,   parameter_list     #affects  a specific command rnDefaults>Runas_List     parameter,   parameter_list     #affects commands being run as a specific userrn

n

For the scope of this guide, we will zero down to the first type of Defaults in the forms below. Parameters may be flags, integer values, strings, or lists.

n

You should note that flags are implicitly boolean and can be turned off using the '!' operator, and lists have two additional assignment operators, += (add to list) and -= (remove from list).

n

Defaults     parameterrnORrnDefaults     parameter=valuernORrnDefaults     parameter -=value   rnDefaults     parameter +=value  rnORrnDefaults     !parameter       rn

n

1. Set a Secure PATH

n

This is the path used for every command run with sudo, it has two importances:

n

    n

  1. Used when a system administrator does not trust sudo users to have a secure PATH environment variable
  2. n

  3. To separate “root path” and “user path”, only users defined by exempt_group are not affected by this setting.
  4. n

n

To set it, add the line:

n

Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"rn

n

2. Enable sudo on TTY User Login Session

n

To enable sudo to be invoked from a real tty but not through methods such as cron or cgi-bin scripts, add the line:

n

Defaults  requiretty   rn

n

3. Run Sudo Command Using a pty

n

A few times, attackers can run a malicious program (such as a virus or malware) using sudo, which would again fork a background process that remains on the user’s terminal device even when the main program has finished executing.

n

To avoid such a scenario, you can configure sudo to run other commands only from a psuedo-pty using the use_pty parameter, whether I/O logging is turned on or not as follows:

n

Defaults  use_ptyrn

n

4. Create a Sudo Log File

n

By default, sudo logs through syslog(3). However, to specify a custom log file, use the logfile parameter like so:

n

Defaults  logfile="/var/log/sudo.log"rn

n

To log hostname and the four-digit year in the custom log file, use log_host and log_year parameters respectively as follows:

n

Defaults  log_host, log_year, logfile="/var/log/sudo.log"rn

n

Below is an example of a custom sudo log file:

n

Create Custom Sudo Log File
Create Custom Sudo Log File

n

5. Log Sudo Command Input/Output

n

The log_input and log_output parameters enable sudo to run a command in pseudo-tty and log all user input and all output sent to the screen receptively.

n

The default I/O log directory is /var/log/sudo-io, and if there is a session sequence number, it is stored in this directory. You can specify a custom directory through the iolog_dir parameter.

n

Defaults   log_input, log_outputrn

n

There are some escape sequences are supported such as %{seq} which expands to a monotonically increasing base-36 sequence number, such as 000001, where every two digits are used to form a new directory, e.g. 00/00/01 as in the example below:

n

$ cd /var/log/sudo-io/rn$ lsrn$ cd  00/00/01rn$ lsrn$ cat logrn

n

Log sudo Input Output
Log sudo Input Output

n

You can view the rest of the files in that directory using the cat command.

n

6. Lecture Sudo Users

n

To lecture sudo users about password usage on the system, use the lecture parameter as below.

n

It has 3 possible values:

n

    n

  1. always – always lecture a user.
  2. n

  3. once – only lecture a user the first time they execute sudo command (this is used when no value is specified)
  4. n

  5. never – never lecture the user.
  6. n

n

 rnDefaults  lecture="always"rn

n

Additionally, you can set a custom lecture file with the lecture_file parameter, type the appropriate message in the file:

n

Defaults  lecture_file="/path/to/file"rn

n

Lecture Sudo Users
Lecture Sudo Users

n

7. Show Custom Message When You Enter Wrong sudo Password

n

When a user enters a wrong password, a certain message is displayed on the command line. The default message is “sorry, try again”, you can modify the message using the badpass_message parameter as follows:

n

Defaults  badpass_message="Password is wrong, please try again"rn

n

8. Increase sudo Password Tries Limit

n

The parameter passwd_tries is used to specify the number of times a user can try to enter a password.

n

The default value is 3:

n

Defaults   passwd_tries=5 rn

n

Increase Sudo Password Attempts
Increase Sudo Password Attempts

n

To set a password timeout (default is 5 minutes) using passwd_timeout parameter, add the line below:

n

Defaults   passwd_timeout=2rn

n

9. Let Sudo Insult You When You Enter Wrong Password

n

In case a user types a wrong password, sudo will display insults on the terminal with the insults parameter. This will automatically turn off the badpass_message parameter.

n

Defaults  insults rn

n

Let's Sudo Insult You When Enter Wrong Password
Let’s Sudo Insult You When Enter Wrong Password

n

Read More: Let Sudo Insult You When You Enter Incorrect Password

n

10. Learn More Sudo Configurations

n

Additionally, you can learn more sudo command configurations by reading: Difference Between su and sudo and How to Configure sudo in Linux.

n

That’s it! You can share other useful sudo command configurations or tricks and tips with Linux users out there via the comment section below.

n

‘]