Getting started

Edit files with nano: a beginner's guide

Open, edit, search and save files with the nano text editor on a Linux server, including cutting and pasting lines, showing line numbers and safely editing system config files with sudo.

BeginnerLinux server5 min read

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.
  • nano installed. It ships with Ubuntu and Debian. On a minimal AlmaLinux or Rocky Linux install, add it with:
Bash
sudo dnf install nano

On Ubuntu or Debian, if it is missing for some reason:

Bash
sudo apt install nano

Read the shortcut bar

The two lines at the bottom of the nano screen list common commands. In that list:

  • ^ means the Ctrl key. ^X is Ctrl + X.
  • M- means the Alt key (called Meta). M-U is 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:

Bash
nano notes.txt

If 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:

Bash
nano +42 /etc/nginx/nginx.conf

Edit 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.

  • 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:

Bash
nano -l /etc/ssh/sshd_config

Press 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 linenumbers

Edit system config files with sudo

Files in /etc belong to root. Open them with sudo so you can save your changes:

Bash
sudo nano /etc/ssh/sshd_config

A safe routine for any config change:

  1. Make a backup first:
Bash
   sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
  1. Edit and save with nano.
  2. Test the config if the service has a test command, such as sudo sshd -t for SSH or sudo nginx -t for Nginx.
  3. 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:

Bash
sudo nano -B /etc/hosts

Warning: 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:

Bash
nano ~/practice.txt

Type two lines, press Ctrl + O, Enter, then Ctrl + X. Confirm the text was saved:

Bash
cat ~/practice.txt

Troubleshooting

  • [ Error writing...: Permission denied ]: you opened a root-owned file without sudo. Press Ctrl + O and save to your home folder (for example ~/sshd_config.new), exit, then copy it into place with sudo cp. Next time start with sudo nano.
  • nano: command not found: install it with apt or dnf as 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 .swp or .save leftovers: nano saves filename.save when 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.
Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.