How to change Sudo Session's Time Limit

How to change Sudo Session's Time Limit


The sudo command allows us to securely perform great administrative tasks. However, after some time running a command with sudo, will prompt for a password. Because of the 15 minutes grace period; your sudo session has by default. When the 15 minutes are out, you’ll be forced to reenter your password.

In some cases, you might want to change the duration limit for the sudo session.

Set Sudo Timeout Sessions Globally

If you wish to adjust the default timeout limit for the sudo session system-wide, simply edit the /etc/sudoers file.

$ sudo visudo

Ouput

# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
Defaults        insults

# Host alias specification
###
##
#

Note: Never edit the /etc/sudoers file with any text editor; instead use the visudo command described above. The reason is that, unlike other text editors, the visudo checks the syntax and errors in the file. This prevents you from making mistakes that might have catastrophic consequences.

... add timestamp_timeout=x to the end of the line. Where x is the timeout value for the sudo session. For instance, to set a timeout value for the sudo session to 8 minutes set the value of x to 8.

Defaults env_reset timestamp_timeout=x

Defaults env_reset,timestamp_timeout=8


Again, if you want the system to ask for a password every time you execute the sudo command, set the value of x to 0. Similarly, if you want the system to never ask for the sudo password, then set the value of x to -1.


Set sudo session to run until Terminal exits



Simply run the sudo command with an -s or the --shell option command to make your sudo session stay on until you close the terminal, regardless of how long the terminal is open. You will not be required a password after running this command for any command that requires sudo privileges:

$ sudo -s

The -s option allows you to run the shell as the target user who is running it.


Exit the sudo session

After entering the sudo password, you can exit the sudo session before the time limit defined in the /etc/sudoers file.
The command is as follows:

$ sudo -k


However, keep in mind that if you have already issued the sudo –s command; the -k option will not terminate the sessions. You must exit to end that session.

That’s it!