Commands.page Logo

Can I Use Nano Instead of Vi for Visudo in Ubuntu?

Yes, you can absolutely use nano instead of vi when running the visudo command on Ubuntu. This article confirms that possibility and provides a step-by-step guide on how to configure your system to default to nano for editing sudoers files. We will cover the specific command needed to switch editors and explain why this change might benefit users who find nano more intuitive than vi.

Default Editor Behavior

By default, Ubuntu configures the visudo command to open the sudoers file using the vi text editor. While vi is powerful, it has a steep learning curve for beginners. Nano offers a simpler interface with on-screen command shortcuts, making it a preferred choice for many users editing system configuration files.

Method 1: Temporary Change

If you only want to use nano for a single session without changing system settings, you can specify the editor directly in the command line. Run the following command in your terminal:

sudo EDITOR=nano visudo

This sets the EDITOR environment variable to nano just for this specific command. Once you exit visudo, the system will revert to using vi for future visudo commands.

Method 2: Permanent Change

To make nano the default editor for visudo permanently, you should update the system’s alternative editor configuration. Follow these steps:

  1. Open your terminal.

  2. Run the following command:

    sudo update-alternatives --config editor
  3. You will see a list of available editors with corresponding numbers.

  4. Type the number associated with /bin/nano and press Enter.

After completing these steps, any future use of the visudo command will automatically open the file in nano.

Verifying the Change

To ensure the configuration was successful, run the visudo command without any additional parameters:

sudo visudo

The sudoers file should now open in the nano interface. You will see the file content along with a list of keyboard shortcuts at the bottom of the terminal window, confirming that nano is active.

Safety Precautions

Regardless of which editor you choose, always exercise caution when editing the sudoers file. Syntax errors in this file can prevent you from using sudo commands, potentially locking you out of administrative privileges. Nano helps mitigate this risk by displaying commands clearly, but you should still verify changes before saving and exiting.