How to change the Time Limit for a Sudo Session

While performing some administrative tasks on the command line with sudo privileges, you might have noticed this situation that if you enter a sudo password, the command runs normally. For subsequent commands that run shortly after the first sudo command,  password is not required. However, after waiting for some time if you again run the command with sudo, it will ask for a password. This all happens because of the timeout limit of the sudo session that is by default 15 minutes. This time limit means if you have entered the sudo command with a password, your sudo privileges will remain for 15 minutes. So you will not be asked to enter the password again for the subsequent commands. After the 15 minutes, you will have to enter the sudo password again for any sudo command you try to run.

As a regular system user or administrator, you might want to extend or reduce this default timeout limit for the sudo session. In this article, we will learn how to change the default time limit for the sudo session. To do so, we will have to make changes in the sudoers file. Please note that we have performed the procedure on a Debian 10 system but it will work on other Linux Distributions like Ubuntu as well. The same procedure can be followed in older versions of Debian too.

Specify time for a sudo session

First, open the Terminal In your Debian OS. Go to the Activities tab in the top left corner of your desktop. Then in the search bar, type the keyword terminal. When the search result appears, click on the Terminal icon.

In the Terminal, enter the following command to edit the sudoers file.

$ sudo visudo

Remember, do not edit the sudoers file with any text editors. Instead, use the above method for this purpose.

Visudo

When prompted for a password, enter the password for the sudo user. Sudoers file will by default open in the nano editor as shown in the following screenshot. Now look for the following line in the sudoers file:

Defaults env_reset

Defaults env_reset

Edit the above line by adding timestamp_timeout=x to its end. It should like this:

Defaults env_reset timestamp_timeout=x

Where x is the timeout value for which it will wait before asking again for the sudo password. If you want the system to ask for a password every time you execute the sudo command, set the value of x to 0. If you want the system to never ask for the sudo password, then set the value of x to -1.

Here, we want to reduce the timeout value from 15 minutes to 5 minutes for the sudo prompt. For that we have replaced the x with 5 as follows:

Defaults env_reset,timestamp_timeout=5

15 minute sudo timeout

Once done, press Ctrl+o and Ctrl+x to save and exit the file simultaneously.

Set sudo session to last till terminal closes

Using a single command, you can allow your Terminal session to last until you close the Terminal regardless of how long the Terminal remains open. After executing the following command, you will not be prompted for the password for the sudo commands.

$ sudo -s

Terminate the sudo session

Once you entered the password for sudo, you can suspend the sudo session even before the timeout limit defined in the sudoers file. To do so, use the following command:

$ sudo –k

Please note that the above command will not be going to terminate the session if you have run the “sudo –s” command during the Terminal session.

That is all there is to it! I hope it will be helpful whenever you need to change the time limit for the sudo session. All you require is to just add a single line in the sudoers file and there you go.