How to specify a time limit for a Sudo session

While working with the sudo command to perform administrative tasks on Linux, you may have noticed that even if you provided the sudo password a while ago, you will be asked to enter it again after a while. This is due to the time limit of your sudo session, which is set to 15 minutes by default. If you enter the sudo command after those 15 minutes, even in the same terminal session, you will be asked to enter your password again. As a Linux administrator, you can lengthen or shorten your sudo session from the default fifteen minutes.

This tutorial describes how to make very simple changes to the / etc / sudoers file to specify a time limit for the sudo session. The commands mentioned in this article were executed on Ubuntu 18; however, they work the same on older versions of Ubuntu.

Specify X time for Sudo session

In this example, we will change the time limit of our sudo session to 10 minutes. Follow these steps to change the time limit for the sudo session to whatever you want:

Open your Ubuntu terminal by clicking Ctrl + Alt + T or via Ubuntu Dash.

Since you need to edit the sudoers file located in the etc folders, enter the following command:

$ sudo visudo

visudo team

You will be prompted for a password for the sudo user.

You might be wondering why we don’t open the sudoers file like other text files. The answer is that unlike other text editors, visudo checks the syntax of the text you enter into the file. This will save you from making the wrong changes that could have serious consequences. For example, making erroneous changes to the sudoers file could cost you the inability to log in as a root user to perform any of the elevated functions.

Enter your password and login. The sudoers file will open in the Nano editor as it is the default text editor for Ubuntu 18.04.

Sudoers file

In the image above, you can see the following line:

Defaults env_reset

This line is responsible for limiting the time of your sudo session. The following changes must be made to this line: Advertising

Defaults env_reset, timestamp_timeout=x

Here x is the time in minutes that you can specify to set the required time limit. Pay attention to the following points when setting this timeout:

If you specify a timeout of 0, your session will only last 0 minutes. This means that you will be prompted for passwords for each of your sudo commands.

If you set this time to less than zero (negative), the sudo command will not work as expected.

In this example, I am reducing the default time from 15 minutes to 10 minutes with the following changes in my sudoers file:

Change the sudo timeout from 15 to 10 minutes

Click Ctrl + X to exit the file after making the necessary changes. You will be asked if you want to save the modified file. Click Y to save changes.

Save modification to sudoers file

You will also be prompted for a filename to save. Please hit Enter as we don’t want to change the filename here.

Do not include file name when saving file

Your changes will be saved and your sudo session will last up to the specified minutes whenever you use the sudo command.

Install last Sudo session before closing terminal

With a simple command, you can let the sudo session last until you close the terminal, no matter how long the terminal stays open. You will not be prompted for a password for any command that requires sudo permission after running this command:

$ sudo -s

End sudo session

Once you have provided a password for sudo, you can end your sudo session even before the expiration date specified in the sudoers file with the following simple command:

$ sudo -k

Note that this command will not end the session if you used “sudo -s” during a terminal session.

So, here’s how you can shorten or lengthen the duration of your sudo session by making a one-line change in your / etc / sudoers. You can also use the other commands mentioned in this article to end your sudo session, or prolong it until your terminal session ends.

How to specify a time limit for a Sudo session

Sidebar