Commands.page Logo

How to Clear Cached Sudo Credentials Immediately in Ubuntu

When using the Ubuntu terminal, the sudo command remembers your password for a short period to avoid repeated entries. However, there are times when you need to revoke this access immediately for security reasons or before stepping away from your computer. This guide explains the specific command required to clear cached sudo credentials instantly, ensuring that subsequent sudo actions require fresh authentication.

Understanding Sudo Timestamps

By default, Ubuntu stores a timestamp file when you successfully enter your sudo password. This file allows you to run additional sudo commands without re-entering your password for a default period of 15 minutes. Clearing this timestamp forces the system to demand your password again for the next privileged command.

The Command to Clear Sudo Cache

To invalidate the cached credentials immediately, open your terminal and run the following command:

sudo -k

The -k flag stands for “kill.” It invalidates the user’s timestamp file without removing it. After executing this command, your current sudo session is terminated.

Verifying the Cache Clear

To confirm that the credentials have been cleared, run any command that requires sudo privileges. For example:

sudo ls /root

If the cache was successfully cleared, the terminal will prompt you to enter your password again. If it does not ask for a password, the timestamp is still active.

Removing the Timestamp File Completely

If you wish to remove the timestamp file entirely rather than just invalidating it, use the -K flag instead. This is useful if you want to ensure no trace of the timestamp remains on the system.

sudo -K

Like the -k command, this will force a password request on the next sudo attempt. Use this method when you want to reset the sudo state thoroughly.