nano is the friendliest text editor on a Linux server. It works like a basic notepad: you type, and the main shortcuts are listed at the bottom of the screen. This guide shows how to open, edit, save and close a file, plus the handful of shortcuts that make editing config files quick and safe.
Before you start
- SSH access to your Linux VPS. See Connect to your Linux VPS with SSH.
nanoinstalled. It ships with Ubuntu and Debian. On a minimal AlmaLinux or Rocky Linux install, add it with:
sudo dnf install nanoOn Ubuntu or Debian, if it is missing for some reason:
sudo apt install nanoRead the shortcut bar
The two lines at the bottom of the nano screen list common commands. In that list:
^means the Ctrl key.^Xis Ctrl + X.M-means the Alt key (called Meta).M-Uis Alt + U. On a Mac terminal, you may need to enable "Use Option as Meta key" in the terminal settings, or press Esc and then the letter.
Press Ctrl + G at any time for the full help screen.
Open a file
Give nano the file name:
nano notes.txtIf the file does not exist, nano opens an empty buffer and creates the file when you save.
To jump to a specific line, for example line 42 from an error message, add +42:
nano +42 /etc/nginx/nginx.confEdit and move around
Type to insert text where the cursor is. Use the arrow keys, Home, End, Page Up and Page Down to move. A few extra keys:
- Ctrl + A / Ctrl + E: start / end of the line.
- Ctrl + _ (Ctrl + Shift + minus) or Alt + G: go to a line number.
- Alt + \\ / Alt + /: jump to the first / last line of the file.
Save and exit
- Ctrl + O writes the file ("Write Out"). Nano shows the file name at the bottom; press Enter to confirm.
- Ctrl + X exits. If there are unsaved changes, nano asks Save modified buffer? Press Y to save, N to discard, or Ctrl + C to cancel and keep editing.
Recent nano versions also accept Ctrl + S to save without a prompt.
Search and replace
- Ctrl + W ("Where Is"): type a word and press Enter. Press Alt + W to find the next match.
- Ctrl + \\: search and replace. Type the text to find, press Enter, type the replacement, press Enter, then Y to replace this match, N to skip it, or A to replace all.
Cut, copy and paste lines
Nano works on whole lines by default:
- Ctrl + K: cut the current line. Press it several times to cut several lines; they are kept together.
- Alt + 6: copy the current line without cutting it.
- Ctrl + U: paste ("Uncut") the cut or copied lines at the cursor.
To select part of the text, press Alt + A to set a mark, move the cursor to extend the selection, then use Ctrl + K or Alt + 6.
Undo is Alt + U and redo is Alt + E.
Tip: To paste text from your own computer, use your SSH client's paste (right-click in PuTTY, Ctrl + Shift + V in many Linux terminals, Cmd + V on a Mac). Nano receives it as typed text.
Show line numbers
Press Alt + N to toggle line numbers, or start nano with -l:
nano -l /etc/ssh/sshd_configPress Alt + C to toggle a constant cursor position display at the bottom, which shows the current line and column.
To make line numbers the default, add this line to ~/.nanorc:
set linenumbersEdit system config files with sudo
Files in /etc belong to root. Open them with sudo so you can save your changes:
sudo nano /etc/ssh/sshd_configA safe routine for any config change:
- Make a backup first:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak- Edit and save with nano.
- Test the config if the service has a test command, such as
sudo sshd -tfor SSH orsudo nginx -tfor Nginx. - Only then reload or restart the service.
nano can also create its own backup when it saves. Start it with -B, and it keeps the previous version with a ~ suffix:
sudo nano -B /etc/hostsWarning: A mistake in sshd_config, firewall rules or network settings can lock you out. Keep your current SSH session open while you test from a second window. If you are locked out, use the browser console in your VPS provider's control panel.
Check it works
Practise on a scratch file:
nano ~/practice.txtType two lines, press Ctrl + O, Enter, then Ctrl + X. Confirm the text was saved:
cat ~/practice.txtTroubleshooting
[ Error writing...: Permission denied ]: you opened a root-owned file withoutsudo. Press Ctrl + O and save to your home folder (for example~/sshd_config.new), exit, then copy it into place withsudo cp. Next time start withsudo nano.nano: command not found: install it withaptordnfas shown above.- Long lines wrap or are cut off: that is display only. Press Alt + S to toggle soft wrapping. The file itself is unchanged.
- Alt shortcuts do nothing on a Mac: enable the Option-as-Meta setting in Terminal or iTerm2, or press Esc followed by the letter.
- A file shows
.swpor.saveleftovers: nano savesfilename.savewhen it is killed with unsaved changes. Compare it with the original, then delete it.
Next steps
- Learn the editor that is on every server: Vim basics.
- Keep a copy before every edit: Back up config files before editing.
- Copy and paste between your PC and the server: Copy and paste in an SSH terminal.
