On Linux, sudo caches your password for a certain duration (usually 15 minutes by default) after you enter it. If you run another sudo command within that time window, it will not ask for the password again. However, commands like sleep (which run for a long time) will cause the cached credentials to expire before the next sudo command is executed.
You can extend the duration of the cached credentials only for the current terminal session by running a dummy sudo command just before the long-running command. This will refresh the cache and prevent the password prompt during the sleep period.
bashsudo true && \ sleep 1024 && \ sudo whoami
sudo true refreshes the cached credentials, extending the timeout.sleep command will now run within the extended timeout window, so the next sudo command will not prompt for a password.sudo true runs a command that does nothing, but it updates the cached credentials./etc/sudoers (default is 15 minutes).sleep runs quickly, the cache remains valid for the next sudo command.This approach only affects the current terminal session and does not require modifying system-wide sudo settings or disabling password prompts for all commands.
Would you like to know how to check or adjust the default timeout in /etc/sudoers?
Le Chat can make mistakes. Check answers. Learn more