Text Editing: nano & vim
Edit configs without fear: nano first, vim modes and keystrokes, root-owned files through sudoedit, backups, and testing with sshd -t and nginx -t before anything restarts.
An interactive Linux lesson: 21 steps, about 30 minutes, on a live simulation in your browser.
You are alice on web01, and the ticket has two config edits. First, password logins over SSH, the Secure Shell (the encrypted way you log in from across the network), must go off: the sshd_config file holds PasswordAuthentication yes, and it should read no. Second, the shop needs its name in the nginx web server config.
The program that accepts your SSH logins is sshd, the Secure Shell daemon (a daemon is a program that runs in the background serving something). Edit its config badly and your next login fails. So this lesson runs one rule for every edit: read the file, copy it, change it, then prove the change before anything restarts.
What you will learn
Look before you edit
- A ticket with two config edits: Read the file, copy it, change it, then prove the change. In that order, every time.
- Who may write to /etc/ssh?: System files belong to root. Your keystrokes borrow root one command at a time through sudo.
Nano: no learning curve
- Nano shows its help at the bottom: Nano has no modes: what you type is what goes into the file, and the help stays on screen.
- Write out with Ctrl+O, exit with Ctrl+X
- Quit without saving: what survives?: Until you write out, your edits live only in the editor's memory. The disk file changes on save, never on typing.
Vim: a language of keys
- Vim opens in normal mode: In vim you are always in a mode: normal to command, insert to type, command-line after : to save and quit.
- i enters insert, Esc leaves it: i enters insert mode, where keys become text again. Esc hands the keyboard back to normal mode.
- Three colons: :w :q :wq
- Move without the mouse: hjkl
Vim: change text fast
- Search with /: / searches forward from the cursor. n repeats the hunt, N turns it around.
- u undoes, Ctrl+R redoes: u undoes the last change and Ctrl+R redoes it. Undo is a safety net, not a backup.
- Break it: vim says E212
- Cut, copy, paste: dd, yy, p
Root-owned files, safely
- Never sudo vim: use sudoedit: Edit as yourself, save as root. sudoedit keeps a root-powered editor off your machine.
- Install nginx, meet edit two: Back up before you touch it: a .bak copy turns any mistake into one cp away from fixed.
- Drill: back up before you touch it
Prove it before you restart
- Break it: one brace too few: A config test reads the file without starting anything. Broken file, running server: that order is the whole point.
- Restore, re-apply, re-test
- Drill: clean up the practice copy
Recap & playground
- Cheat sheet
- Playground