Nano is the first editor most Linux users encounter — it comes pre-installed on Ubuntu, Debian, and most server distributions. Opening a file is easy. Saving is too, but only if you know the right key combinations. Without them, you can lose your changes or end up stuck in the editor with no idea how to get out.
Basic Save: Ctrl+O
Ctrl+O ("Write Out") saves the current file without closing the editor. After pressing it, nano shows a prompt at the bottom of the screen:
File Name to Write: /etc/nginx/nginx.conf
Leave the name as-is and press Enter — the file saves to the same location. Or change the path to write the changes to a new file, leaving the original untouched.
After saving, nano stays open and editing can continue.
Save and Exit: Ctrl+X
Ctrl+X closes the editor. If there are unsaved changes, nano asks:
Save modified buffer? (Answering "No" will DISCARD changes.)
Y Yes
N No ^C Cancel
Y saves and exits. N exits without saving (all changes are lost). Ctrl+C cancels and returns to editing.
After pressing Y, a filename prompt appears — same as with Ctrl+O. Press Enter to confirm.
Saving Without Permissions: sudo
A common server situation: you open a system config with plain nano, make changes, press Ctrl+O and get:
Error writing /etc/nginx/nginx.conf: Permission denied
The file belongs to root. Three practical solutions.
Option 1 — close and reopen with sudo:
nano /etc/nginx/nginx.conf
# Ctrl+O gives Permission denied
# Ctrl+X to exit without saving
sudo nano /etc/nginx/nginx.conf
Downside: the changes need to be made again.
Option 2 — save a copy to an accessible location. At the filename prompt change the path:
File Name to Write: /tmp/nginx.conf
Then copy with sudo:
sudo cp /tmp/nginx.conf /etc/nginx/nginx.conf
Option 3 — write via tee directly from nano. Press Ctrl+O and enter:
File Name to Write: | sudo tee /etc/nginx/nginx.conf
The pipe character activates pipe writing — nano passes the content to tee, which writes the file with sudo privileges. Does not work in every nano configuration, but frequently saves the day.
Save a Backup Before Making Changes
A good habit when editing system files:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sudo nano /etc/ssh/sshd_config
If something goes wrong, restoring takes one command:
sudo cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config
Why the Save Fails: Diagnosing the Problem
Permission denied — the file belongs to root or another user. Use sudo nano when opening, or pipe through tee as described above.
No space left on device — the disk is full. Check with df -h and free space before retrying.
File open in another process — some files get locked by other programs. Check:
lsof /etc/nginx/nginx.conf
Nano configuration corrupted — rare but possible. Reset with:
mv ~/.nanorc ~/.nanorc.bak
Nano will fall back to defaults on next launch.
nano Keyboard Shortcut Reference
| Action | Keys |
|---|---|
| Save without closing | Ctrl+O, Enter |
| Save to a different file | Ctrl+O, change name, Enter |
| Exit | Ctrl+X |
| Save and exit | Ctrl+X, Y, Enter |
| Exit without saving | Ctrl+X, N |
| Cancel exit | Ctrl+X, Ctrl+C |
| Open with root privileges | sudo nano filename |