{"id":13227,"date":"2019-04-01T08:37:55","date_gmt":"2019-04-01T08:37:55","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13227"},"modified":"2019-04-01T08:37:55","modified_gmt":"2019-04-01T08:37:55","slug":"10-useful-sudoers-configurations-for-setting-sudo-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/10-useful-sudoers-configurations-for-setting-sudo-in-linux\/","title":{"rendered":"10 Useful Sudoers Configurations for Setting \u2018sudo\u2019 in Linux"},"content":{"rendered":"<p>In Linux and other Unix-like operating systems, only the\u00a0<strong>root<\/strong>\u00a0user can run all commands and perform certain critical operations on the system such as install and update, remove packages,\u00a0<a href=\"https:\/\/www.tecmint.com\/add-users-in-linux\/\" target=\"_blank\" rel=\"noopener\">create users and groups<\/a>, modify important system configuration files and so on.<\/p>\n<p>However, a system administrator who assumes the role of the root user can permit other normal system users with the help of\u00a0<a href=\"https:\/\/www.tecmint.com\/su-vs-sudo-and-how-to-configure-sudo-in-linux\/\" target=\"_blank\" rel=\"noopener\">sudo command<\/a>\u00a0and a few configurations to run some commands as well as carry out a number of vital system operations including the ones mentioned above.<\/p>\n<p>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\u00a0<strong>su<\/strong>\u00a0command.<\/p>\n<p><strong>sudo<\/strong>\u00a0allows a permitted user to execute a command as root (or another user), as specified by the security policy:<\/p>\n<ol>\n<li>It reads and parses\u00a0<strong>\/etc\/sudoers<\/strong>, looks up the invoking user and its permissions,<\/li>\n<li>then prompts the invoking user for a password (normally the user\u2019s password, but it can as well be the target user\u2019s password. Or it can be skipped with NOPASSWD tag),<\/li>\n<li>after that, sudo creates a child process in which it calls\u00a0<strong>setuid()<\/strong>\u00a0to switch to the target user<\/li>\n<li>next, it executes a shell or the command given as arguments in the child process above.<\/li>\n<\/ol>\n<p>Below are ten\u00a0<strong>\/etc\/sudoers<\/strong>\u00a0file configurations to modify the behavior of\u00a0<strong>sudo<\/strong>\u00a0command using\u00a0<strong>Defaults<\/strong>\u00a0entries.<\/p>\n<pre>$ sudo cat \/etc\/sudoers\r\n<\/pre>\n<div class=\"code-label\">\/etc\/sudoers File<\/div>\n<pre>#\r\n# This file MUST be edited with the 'visudo' command as root.\r\n#\r\n# Please consider adding local content in \/etc\/sudoers.d\/ instead of\r\n# directly modifying this file.\r\n#\r\n# See the man page for details on how to write a sudoers file.\r\n#\r\n<strong>Defaults\tenv_reset\r\nDefaults\tmail_badpass\r\nDefaults\tsecure_path=\"\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin\"\r\nDefaults\tlogfile=\"\/var\/log\/sudo.log\"\r\nDefaults\tlecture=\"always\"\r\nDefaults\tbadpass_message=\"Password is wrong, please try again\"\r\nDefaults\tpasswd_tries=5\r\nDefaults\tinsults\r\nDefaults\tlog_input,log_output<\/strong>\r\n<\/pre>\n<h4>Types of Defaults Entries<\/h4>\n<pre>Defaults                parameter,   parameter_list     #affect all users on any host\r\nDefaults@Host_List      parameter,   parameter_list     #affects all users on a specific host\r\nDefaults:User_List      parameter,   parameter_list     #affects a specific user\r\nDefaults!Cmnd_List      parameter,   parameter_list     #affects  a specific command \r\nDefaults&gt;Runas_List     parameter,   parameter_list     #affects commands being run as a specific user\r\n<\/pre>\n<p>For the scope of this guide, we will zero down to the first type of\u00a0<strong>Defaults<\/strong>\u00a0in the forms below. Parameters may be flags, integer values, strings, or lists.<\/p>\n<p>You should note that flags are implicitly boolean and can be turned off using the\u00a0<code>'!'<\/code>\u00a0operator, and lists have two additional assignment operators,\u00a0<code>+=<\/code>\u00a0(add to list) and\u00a0<code>-=<\/code>\u00a0(remove from list).<\/p>\n<pre>Defaults     parameter\r\nOR\r\nDefaults     parameter=value\r\nOR\r\nDefaults     parameter -=value   \r\nDefaults     parameter +=value  \r\nOR\r\nDefaults     !parameter       \r\n<\/pre>\n<h3>1. Set a Secure PATH<\/h3>\n<p>This is the path used for every command run with sudo, it has two importances:<\/p>\n<ol>\n<li>Used when a system administrator does not trust sudo users to have a secure PATH environment variable<\/li>\n<li>To separate \u201croot path\u201d and \u201cuser path\u201d, only users defined by\u00a0<strong>exempt_group<\/strong>\u00a0are not affected by this setting.<\/li>\n<\/ol>\n<p>To set it, add the line:<\/p>\n<pre>Defaults secure_path=\"\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin:\/snap\/bin\"\r\n<\/pre>\n<h3>2. Enable sudo on TTY User Login Session<\/h3>\n<p>To enable sudo to be invoked from a real\u00a0<strong>tty<\/strong>\u00a0but not through methods such as\u00a0<strong>cron<\/strong>\u00a0or\u00a0<strong>cgi-bin<\/strong>\u00a0scripts, add the line:<\/p>\n<pre>Defaults  requiretty   \r\n<\/pre>\n<h3>3. Run Sudo Command Using a pty<\/h3>\n<p>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\u2019s terminal device even when the main program has finished executing.<\/p>\n<p>To avoid such a scenario, you can configure sudo to run other commands only from a\u00a0<strong>psuedo-pty<\/strong>\u00a0using the\u00a0<code>use_pty<\/code>\u00a0parameter, whether I\/O logging is turned on or not as follows:<\/p>\n<pre>Defaults  use_pty\r\n<\/pre>\n<h3>4. Create a Sudo Log File<\/h3>\n<p>By default, sudo logs through syslog(3). However, to specify a custom log file, use the logfile parameter like so:<\/p>\n<pre>Defaults  logfile=\"\/var\/log\/sudo.log\"\r\n<\/pre>\n<p>To log hostname and the four-digit year in the custom log file, use\u00a0<strong>log_host<\/strong>\u00a0and\u00a0<strong>log_year<\/strong>\u00a0parameters respectively as follows:<\/p>\n<pre>Defaults  log_host, log_year, logfile=\"\/var\/log\/sudo.log\"\r\n<\/pre>\n<p>Below is an example of a custom sudo log file:<\/p>\n<div id=\"attachment_24131\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Create-Sudo-Log-File.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24131\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Create-Sudo-Log-File.png\" sizes=\"auto, (max-width: 774px) 100vw, 774px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Create-Sudo-Log-File.png 774w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Create-Sudo-Log-File-768x341.png 768w\" alt=\"Create Custom Sudo Log File\" width=\"774\" height=\"344\" aria-describedby=\"caption-attachment-24131\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24131\" class=\"wp-caption-text\">Create Custom Sudo Log File<\/p>\n<\/div>\n<h3>5. Log Sudo Command Input\/Output<\/h3>\n<p>The\u00a0<strong>log_input<\/strong>\u00a0and\u00a0<strong>log_output<\/strong>\u00a0parameters enable sudo to run a command in pseudo-tty and log all user input and all output sent to the screen receptively.<\/p>\n<p>The default I\/O log directory is\u00a0<strong>\/var\/log\/sudo-io<\/strong>, and if there is a session sequence number, it is stored in this directory. You can specify a custom directory through the\u00a0<strong>iolog_dir<\/strong>\u00a0parameter.<\/p>\n<pre>Defaults   log_input, log_output\r\n<\/pre>\n<p>There are some escape sequences are supported such as\u00a0<code>%{seq}<\/code>\u00a0which 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.\u00a0<strong>00\/00\/01<\/strong>\u00a0as in the example below:<\/p>\n<pre>$ cd \/var\/log\/sudo-io\/\r\n$ ls\r\n$ cd  00\/00\/01\r\n$ ls\r\n$ cat log\r\n<\/pre>\n<div id=\"attachment_24132\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Log-sudo-Input-Output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24132\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Log-sudo-Input-Output.png\" alt=\"Log sudo Input Output\" width=\"545\" height=\"211\" aria-describedby=\"caption-attachment-24132\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24132\" class=\"wp-caption-text\">Log sudo Input Output<\/p>\n<\/div>\n<p>You can view the rest of the files in that directory using the\u00a0<a href=\"https:\/\/www.tecmint.com\/13-basic-cat-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cat command<\/a>.<\/p>\n<h3>6. Lecture Sudo Users<\/h3>\n<p>To lecture sudo users about password usage on the system, use the\u00a0<strong>lecture<\/strong>\u00a0parameter as below.<\/p>\n<p>It has 3 possible values:<\/p>\n<ol>\n<li>always \u2013 always lecture a user.<\/li>\n<li>once \u2013 only lecture a user the first time they execute sudo command (this is used when no value is specified)<\/li>\n<li>never \u2013 never lecture the user.<\/li>\n<\/ol>\n<pre> \r\nDefaults  lecture=\"always\"\r\n<\/pre>\n<p>Additionally, you can set a custom lecture file with the\u00a0<strong>lecture_file<\/strong>\u00a0parameter, type the appropriate message in the file:<\/p>\n<pre>Defaults  lecture_file=\"\/path\/to\/file\"\r\n<\/pre>\n<div id=\"attachment_24133\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Lecture-Sudo-Users.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24133\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Lecture-Sudo-Users.png\" alt=\"Lecture Sudo Users\" width=\"666\" height=\"192\" aria-describedby=\"caption-attachment-24133\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24133\" class=\"wp-caption-text\">Lecture Sudo Users<\/p>\n<\/div>\n<h3>7. Show Custom Message When You Enter Wrong sudo Password<\/h3>\n<p>When a user enters a wrong password, a certain message is displayed on the command line. The default message is \u201c<strong>sorry, try again<\/strong>\u201d, you can modify the message using the\u00a0<strong>badpass_message<\/strong>\u00a0parameter as follows:<\/p>\n<pre>Defaults  badpass_message=\"Password is wrong, please try again\"\r\n<\/pre>\n<h3>8. Increase sudo Password Tries Limit<\/h3>\n<p>The parameter\u00a0<strong>passwd_tries<\/strong>\u00a0is used to specify the number of times a user can try to enter a password.<\/p>\n<p>The default value is 3:<\/p>\n<pre>Defaults   passwd_tries=5 \r\n<\/pre>\n<div id=\"attachment_24134\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Increase-Sudo-Password-Attempts.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24134\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Increase-Sudo-Password-Attempts.png\" alt=\"Increase Sudo Password Attempts\" width=\"666\" height=\"382\" aria-describedby=\"caption-attachment-24134\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24134\" class=\"wp-caption-text\">Increase Sudo Password Attempts<\/p>\n<\/div>\n<p>To set a password timeout (default is 5 minutes) using\u00a0<strong>passwd_timeout<\/strong>\u00a0parameter, add the line below:<\/p>\n<pre>Defaults   passwd_timeout=2\r\n<\/pre>\n<h3>9. Let Sudo Insult You When You Enter Wrong Password<\/h3>\n<p>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\u00a0<strong>badpass_message<\/strong>\u00a0parameter.<\/p>\n<pre>Defaults  insults \r\n<\/pre>\n<div id=\"attachment_24135\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Sudo-Insult-Message.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24135\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Sudo-Insult-Message.png\" alt=\"Let's Sudo Insult You When Enter Wrong Password\" width=\"669\" height=\"325\" aria-describedby=\"caption-attachment-24135\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24135\" class=\"wp-caption-text\">Let\u2019s Sudo Insult You When Enter Wrong Password<\/p>\n<\/div>\n<p><strong>Read More<\/strong>:\u00a0<a href=\"https:\/\/www.tecmint.com\/sudo-insult-when-enter-wrong-password\/\" target=\"_blank\" rel=\"noopener\">Let Sudo Insult You When You Enter Incorrect Password<\/a><\/p>\n<h3>10. Learn More Sudo Configurations<\/h3>\n<p>Additionally, you can learn more\u00a0<strong>sudo<\/strong>\u00a0command configurations by reading:\u00a0<a href=\"https:\/\/www.tecmint.com\/su-vs-sudo-and-how-to-configure-sudo-in-linux\/\" target=\"_blank\" rel=\"noopener\">Difference Between su and sudo and How to Configure sudo in Linux<\/a>.<\/p>\n<p>That\u2019s it! You can share other useful sudo command configurations or\u00a0<a href=\"https:\/\/www.tecmint.com\/tag\/linux-tricks\/\" target=\"_blank\" rel=\"noopener\">tricks and tips with Linux<\/a>\u00a0users out there via the comment section below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/sudoers-configurations-for-setting-sudo-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Linux and other Unix-like operating systems, only the\u00a0root\u00a0user can run all commands and perform certain critical operations on the system such as install and update, remove packages,\u00a0create users and groups, modify important system configuration files and so on. However, a system administrator who assumes the role of the root user can permit other normal &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/10-useful-sudoers-configurations-for-setting-sudo-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;10 Useful Sudoers Configurations for Setting \u2018sudo\u2019 in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13227","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13227"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13227\/revisions"}],"predecessor-version":[{"id":13228,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13227\/revisions\/13228"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}