How to Make sudo Remember the Password for Longer

Here’s how to make sudo remember your password for longer so that you don’t have to keep typing it repeatedly.

Probably you execute quite a lot of sudo commands because you don’t want to sudo su and execute commands as root all the time. However, it can be not very pleasant having to keep entering the sudo password. So here’s how you can deal with it.

You noticed that if you execute one and then another one within a few minutes, the 2nd time, you don’t get the message like: [sudo] password for your_username. However, you get it when there is more time between the execution of the two commands.

How Can I Make sudo Last Longer?

The behavior of sudo is configured in the “/etc/sudoers” file, and by default timeout of the sudo command is 15 minutes.

The “/etc/sudoers” file has a timestamp_timeout option responsible for reprompting the user for a password after a specific amount of time.

The good news is that you can increase this number to a larger one (in minutes) by adding a string in the “/etc/sudoers” file. After that time, sudo will ask for a password again.

The sudo command doesn’t remember your password, but when you first authorize it, a session is created which lasts for timestamp_timeout. It stores timestamp under the “/var/run/sudo/ts/” directory.

It’s essential to make sure you edit your sudoers file using visudo, which checks your syntax and will not leave you with the wrong configuration and inaccessible sudo.

In other words, running sudo visudo instead of editing the file directly causes the system to validate the “/etc/sudoers” file before it commits the changes.

To make the sudo command last longer, run the following command in terminal:

sudo visudo

Find the lines starting with “Defaults” and add “Defaults timestamp_timeout=x” where “x” is the number of minutes you want between reprompts. In our case, we set this value to 60.

Make sudo Remember the Password for Longer

That’s it. Save the file and exit. The sudo password prompt will time out after an hour (60 minutes) once sudo is invoked by a user.

In addition, if you specify 0, you will always be asked for the password. Keep in mind that if you set a negative value, for example, -1, the timeout will never expire.

Of course, specifying a negative value is not recommended and should be treated as a bad security practice.

Please find out more about various options in sudoers on its command line manual page.

If you have any questions or feedback, feel free to leave a comment.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 69%