Commands.page Logo

How to Comment Out a Line in Sudoers File Ubuntu

This guide explains how to safely comment out a line in the sudoers file on Ubuntu. You will learn the proper command to edit the file, the specific syntax used for comments, and why using the correct tool is critical to avoid locking yourself out of sudo privileges.

Use the visudo Command

Never edit the /etc/sudoers file directly with a standard text editor like nano or vim. Doing so can introduce syntax errors that prevent you from using sudo commands, potentially locking you out of administrative access. Instead, always use the visudo command, which checks for syntax errors before saving changes.

Open the terminal and run the following command:

sudo visudo

Add the Comment Symbol

Once the file opens in your default editor, locate the specific line you wish to disable. To comment out the line, place a hash symbol (#) at the very beginning of the line.

For example, change this:

username ALL=(ALL:ALL) ALL

To this:

# username ALL=(ALL:ALL) ALL

Any text following the # symbol on that line will be ignored by the system.

Save and Exit

After adding the hash symbol, save the file and exit the editor.

If visudo detects any syntax errors during the save process, it will warn you and prevent the broken file from being written, ensuring your system remains secure and functional.