×


Change Sudo Password Timeout on Ubuntu 20.04 LTS - How to do it ?

Sudo privileges allow a user to perform the administrative task on a Linux OS.

When we execute a command as sudo, the system asks you to first enter the sudo password.

However, for other commands that you run soon after the first command, you are not asked for sudo passwords.
This privilege lasts for 15 minutes and within this time, you can use sudo without a password. After 15 minutes of inactivity, each command you run as sudo will ask for the sudo password.
This is good as you will not have to worry that someone will gain access and run any administrative command if you are away from your system.
However, there are scenarios when you need this timeout to be less or a little longer than the default value.
For example, you may want the system to prompt for a sudo password every time a sudo command is executed. Similarly, you may want the sudo timeout to be a little longer in the case where you are the only person who has physical access to your system.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Sudo and Linux related queries.
In this context, we shall look into how to change sudo password timeout in Ubuntu 20.04 LTS Linux OS.
Note: If you want to assign a user sudo privileges, visit our guide on How to Add a User to Sudoers on Ubuntu.

How to Change Sudo Password Timeout on Ubuntu ?

As earlier mentioned, the default time limit for a sudo session is 15 minutes.

You can change the default settings from the sudoers file.

The steps are as follows:
1. Edit the sudoers file using the command below in Terminal:

$ sudo visudo

Here, Enter sudo password.

If you are using Ubuntu OS, the sudoers file will open in Nano editor, for other OS, it may open in Vi.


2. Scroll down to the end of the file and append the below line:

Defaults timestamp_timeout=x

Where "x" is the number of minutes you want sudo to wait before asking you for your password.
This line will change the sudo password timeout for all users on the system.
To change the timeout value for a specific user, use the following command instead:

Defaults:user timestamp_timeout=x

If you use "-1" for x, you will not be prompted for a sudo password until you reboot.

If you use "0" for x, you will be prompted for a sudo password for each command you run.

How to Reset Sudo Password Timeout on Ubuntu Linux ?

You can also reset the sudo password timeout by clearing the cached credentials of the user.
The command is as follows:

$ sudo -k

By doing so, you will be prompted to enter password for the next sudo commands even before the time limit has been reached.

[Need urgent support in fixing missing packages on Ubuntu Linux OS? We can help you. ]


Conclusion

This article covers how you can easily change the sudo password timeout by adding a single entry in the sudoers file. If you are using Vi instead of Nano, follow the instructions described here.
You use sudo for some command in the terminal, it asks for the password.

You enter the password, the command runs.
For the subsequent commands, even if they need superuser privileges, you don't need to enter the password again.
After a certain amount of time, when you try to use sudo, it asks for the password again.
It happens because there is a default timeout in Ubuntu and other Linux system for Sudo. In Ubuntu, this default sudo timeout is 15 minutes.
If you think 15 minute is too short or too long you can even change the sudo password timeout in Ubuntu.

To Change sudo password timeout in Ubuntu:
1. Open a terminal (Ctrl+Alt+T) and use the following command:

$ sudo visudo

It will open a file in nano text editor that contains some settings related to sudo.
In here look for the following line:

Defaults        env_reset

Replace above line with this one:

Defaults        env_reset, timestamp_timeout=XX

Where XX is the new timeout value in minutes.
Of course you have to replace the XX with the desired timeout value.
2. Once you are done changing the default sudo password timeout, use Ctrl+X to exit the editor.
It will give you option to save or cancel the changes.

Use Y to save the changes.