nano Editor in Linux: Edit, Save, and Exit Files

Aidin

Pencil illustration for a nano editor guide

Hi everyone. Sooner or later every Linux tutorial asks you to edit a configuration file, and on Ubuntu the editor that is always there and easiest to start with is nano. In this guide I use nano 8.7.1 on Ubuntu 26.04 to change a setting in a small config file, save it, leave without saving when I change my mind, and deal with system files that nano opens but refuses to save.

The main thing to understand about nano is the shortcut bar at the bottom. Once you can read it, you never get stuck inside the editor.

If you have not read it yet, the previous article on useradd and adduser is part of the same series on everyday Linux commands.

Distro : Ubuntu 26.04.1 LTS (Desktop)
Editor : GNU nano 8.7.1
File   : ~/nano-lab/app.conf

Opening a File in nano

nano app.conf

Opening app.conf in nano with the shortcut bar

I created a small file with three settings and opened it.

mkdir -p ~/nano-lab && cd ~/nano-lab
printf '%s\n' server_name=lab port=8080 log_level=info > app.conf
nano app.conf

The top bar shows the nano version (number 1) and the file name. The status line confirms Read 3 lines (number 2). The two rows at the bottom are the shortcut list (number 3). ^ means the Ctrl key and M- means the Alt key, so ^X Exit is Ctrl+X and M-U Undo is Alt+U.

Note: If the file you name does not exist yet, nano opens an empty buffer and shows New File. The file is created only when you save it.

Editing and Saving

Change a value and save with Ctrl+O

Changing the port and saving with Ctrl+O

To jump to the port line I pressed Ctrl+W (Where Is), typed port, and pressed Enter. Then I pressed End, deleted 8080 with Backspace, and typed 9090. You can also simply move with the arrow keys, since nano has no separate typing mode.

As soon as the text changes, an asterisk appears after the file name (number 1). The new value is port=9090 (number 2). Pressing Ctrl+O (Write Out) asks for the file name with Write to File: app.conf (number 3). Press Enter to keep the same name.

Wrote 3 lines

nano confirming Wrote 3 lines

nano confirms the save with Wrote 3 lines (number 1), and the asterisk is gone. I closed the editor with Ctrl+X.

Check the result

The file after saving in nano
cat app.conf

cat shows that the file on disk now has port=9090 (number 1).

Leaving Without Saving

nano -l and the Save modified buffer question

nano -l and the Save modified buffer prompt
nano -l app.conf

The -l option shows line numbers on the left (number 1), which helps when an error message says something like line 4. I moved to the end and added debug=true (number 2), then decided I did not want it and pressed Ctrl+X. nano asks Save modified buffer? (number 3). Y saves, N throws the changes away, and Ctrl+C returns to the editor. I pressed N.

The file is unchanged

The file unchanged after exiting without saving
cat app.conf

The file still has only the three original lines (number 1). Nothing was written because I answered N.

Editing System Files

nano /etc/hosts without sudo

Unwritable warning when opening /etc/hosts
nano /etc/hosts

nano opens the file without complaining, but the status line warns in red that the file is unwritable (number 1). You can read and even change the text, but Ctrl+O will fail with Permission denied. Watch for this warning before you start typing, otherwise your edits are lost.

sudo nano

Opening /etc/hosts with sudo nano
sudo nano /etc/hosts

With sudo, the terminal title shows sudo nano /etc/hosts and the terminal header turns red as a reminder that this window is running as root (number 1). The warning is gone and nano simply reports Read 9 lines (number 2). I pressed Ctrl+X without changing anything.

Warning: Only use sudo nano for files that really need root, such as those in /etc. Opening your own files with sudo can make root the owner of new files in your home folder.

Common Problems and How to Fix Them

I cannot get out of nano

Press Ctrl+X. If nano asks Save modified buffer?, answer Y or N.

Error writing file: Permission denied

The file belongs to root. Exit, then open it again with sudo nano.

I deleted a line by accident

Press Alt+U to undo. Ctrl+K cuts the whole line, and Ctrl+U pastes it back.

The file has no line numbers

Start nano with nano -l, or press Alt+N inside the editor to turn them on.

That covers nano for everyday editing: reading the shortcut bar, searching with Ctrl+W, saving with Ctrl+O, exiting with Ctrl+X, and editing protected files with sudo. Next in the series is systemctl for starting, stopping, and checking services. If nano shows a message I did not cover, paste it in the comments.

Leave a Comment