How to Reset Sudo Environment to Default in Ubuntu
This article provides a step-by-step guide on resetting the sudo environment to its default settings in Ubuntu. It covers clearing cached credentials, reverting configuration changes in the sudoers file, and resetting environment variables. Follow these instructions to restore standard sudo behavior and security policies on your system.
Clear Cached Sudo Credentials
To force Ubuntu to request a password again for the next sudo command, you need to clear the timestamp. This effectively resets your current sudo session. Open your terminal and run the following command:
sudo -kThis invalidates your cached credentials immediately. The next time you run a command with sudo, you will be prompted to enter your password.
Revert Sudoers Configuration
If you have modified the sudo configuration, you must revert changes
to /etc/sudoers to return to the default state. Always use
the visudo command to edit this file safely, as it checks
for syntax errors before saving.
- Run the command
sudo visudo. - Remove any custom lines added during your session.
- Ensure the default line
%sudo ALL=(ALL:ALL) ALLexists for user groups. - Save and exit the editor.
If you cannot run sudo because of a configuration error, you must boot into recovery mode to fix the file using the root shell.
Reset Environment Variables
Sudo may preserve certain environment variables based on your
configuration. To reset this behavior, edit the sudoers file again using
visudo. Look for Defaults env_keep lines and
remove custom entries. To restore strict defaults, ensure no unwanted
variables are listed in the env_keep or
env_check directives. Save the file to apply changes and
ensure the environment is clean upon sudo execution.