brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: sudo -k forgets your cached sudo password right away

Linux
~/tips/tip-security-sudo-k-reset-timestamp

sudo -k

Clears sudo's cached password so the next command asks for it again.

After you type your password once, sudo remembers it for a while. That is convenient until you leave a terminal open on a shared or unlocked machine.

You can drop the cache yourself:

sudo -k

According to the sudo manual, without a command it invalidates your cached credentials for the current session, and the next sudo asks for the password again. It does not need a password itself. The manual says it was added so a user could revoke sudo permissions from a .logout file. The capital version goes further:

sudo -K

That removes every cached credential for your user, regardless of terminal or parent process ID, and it cannot be combined with a command. The opposite exists as well. sudo -v updates the cached credentials and asks for the password if needed, which extends the timeout without running a command. That is handy before a long script that calls sudo several times.

Used with a command, -k does something different, and the manual spells it out: sudo ignores the cached credentials, asks for the password, and does not update the cache.

sudo -k ls /root

By default the sudoers policy caches credentials for 5 minutes, with a separate record for each terminal, so sudo -k in one terminal leaves the others alone. The timestamp_timeout and timestamp_type options in sudoers(5) control that, and your machine may be set up differently.

The caveat: this only affects sudo’s cache. A root shell you started earlier with sudo -i keeps running, so close that separately.

Further reading: sudo manual

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link