Most server configuration lives in plain text files, so editing over SSH is a core skill. nano is the easiest editor and shows its shortcuts on screen; vim is installed almost everywhere and is fast once you know a handful of keys.
Before you start
- An SSH session (see connect with SSH).
- sudo rights to edit system files under
/etc. - Install the editors if missing:
sudo apt install nano vimorsudo dnf install nano vim.
Back up the file first
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bakEdit with nano
sudo nano /etc/hostsType to edit. The bottom bar lists shortcuts, where ^ means Ctrl:
| Keys | Action |
|---|---|
| Ctrl+O, Enter | Save |
| Ctrl+X | Exit (asks to save if changed) |
| Ctrl+W | Search |
| Ctrl+K / Ctrl+U | Cut line / paste |
| Ctrl+_ | Go to line number |
| Alt+U | Undo |
Edit with vim
sudo vim /etc/hostsvim opens in normal mode, where keys are commands. Press i to enter insert mode and type; press Esc to return to normal mode.
| Keys (normal mode) | Action |
|---|---|
:w Enter | Save |
:wq Enter | Save and quit |
:q! Enter | Quit without saving |
/word Enter, then n | Search, next match |
dd / p | Delete line / paste |
u / Ctrl+R | Undo / redo |
gg / G / :42 | Top / bottom / line 42 |
Tip: Stuck in vim? Press Esc twice, type :q! and Enter to leave without changes.
Pick your default editor
Commands such as crontab -e and visudo use your default editor. Set it with export EDITOR=nano in ~/.bashrc, or on Ubuntu run sudo update-alternatives --config editor.
After editing
Test the config before reloading the service, e.g. sudo nginx -t, sudo sshd -t or sudo apachectl configtest, then reload as shown in systemctl and journalctl.
Troubleshooting
- File is read-only / Permission denied on save: you opened it without sudo. In vim you can still save with
:w !sudo tee %. - Swap file found in vim: another session is editing the file or one crashed. Choose (R)ecover or delete the
.swpfile if nobody else is editing. - Pasted text gets auto-indented in vim: run
:set pastebefore pasting.
