Opscraft
DevOps from zero · Lesson 4 of 6 · 6 min read

The command line from zero

Terminal, shell and prompt

Servers have no desktop, so you control them by typing commands.

  • The terminal is the window you type in (Terminal on macOS, Windows Terminal, or an SSH session).
  • The shell is the program that reads your commands and runs them. On Linux it is usually bash (or zsh on macOS).
  • The prompt is the text the shell shows when it is ready, such as deploy@web-1:~/app$.
Enlarge diagramdeployweb-1~/app$ls -lyour user@the machine:current folder$ normal user, # rootwhat you type
Figure 1. Reading a shell prompt. It tells you who you are, which machine you are on and where you are.
Swipe sideways to see all of it, or tap to open it full screen.

Files and paths

Linux has one tree of folders (called directories) starting at /, the root. A path says where a file is:

  • An absolute path starts from the root: /var/log/nginx/access.log. It means the same thing wherever you are.
  • A relative path starts from the directory you are in now (the working directory): logs/app.log.
  • ~ is your home directory, . is the current directory, and .. is the directory above.
Enlarge diagramAbsolute path/home/deploy/app/logs/app.logRelative path (from app/)logs/app.logGo up one levelcd .. → /home/deployGo home from anywherecd ~ or just cd/home/deploy/← ~ (your home)app/← you are herelogs/app.logvar/log/nginx/access.log
Figure 2. Absolute paths start at / and work from anywhere. Relative paths start from the folder you are in.
Swipe sideways to see all of it, or tap to open it full screen.

Anatomy of a command

Enlarge diagramls-la/var/logtail-fapp.loggrep-itimeoutapp.logcommandwhat to runoptionshow to run itargumentwhat to run it onMore examples
Figure 3. Command, options, arguments: almost every command follows this shape.
Swipe sideways to see all of it, or tap to open it full screen.

Most commands follow the same shape: the command, then options that change its behaviour (starting with - or --), then arguments — the things it works on. ls -l -a /var/log can also be written ls -la /var/log.

Your first commands

Command What it does Example
pwd Print the working directory: where am I? pwd
ls List files (-l details, -a include hidden files) ls -la
cd Change directory cd /var/log, cd .., cd ~
mkdir Make a directory (-p creates parents too) mkdir -p app/config
cp / mv Copy / move or rename mv old.txt new.txt
rm Remove files (-r for directories). There is no recycle bin! rm notes.txt
cat / less Print a file / page through it (press q to quit) less app.log
head / tail First / last lines; tail -f keeps following new lines tail -f app.log
grep Find lines containing text grep ERROR app.log
man / --help Read the manual for a command man ls

Two habits that save beginners hours: press Tab to auto-complete names, and press the Up arrow to bring back earlier commands.

Watch out: rm -rf deletes a folder and everything inside it without asking, and nothing can bring it back. Double-check the path, especially when it contains a variable or a *.

Permissions and sudo

Every file has an owner and permissions saying who may read, change or run it. Normal users can't change system files. To run one command as the all-powerful root user, put sudo in front: sudo systemctl restart nginx. Use it only when needed — root can break anything.

Editing files on a server

Servers rarely have graphical editors. nano is the friendliest (nano file.txt, save with Ctrl-O, exit with Ctrl-X). vim is everywhere and worth learning; the survival kit is: press i to start typing, Esc to stop, then :wq and Enter to save and quit, or :q! to quit without saving.

Connecting to a server with SSH

SSH (Secure Shell) gives you an encrypted terminal on a remote machine. It normally uses a key pair instead of a password:

Enlarge diagramYour laptopholds the private keyServerhas your public key1. connect as user ubuntu2. prove you hold the key3. signs with theprivate key4. signature5. checks it with the publickey in authorized_keys6. shell opens
Figure 4. How SSH key login works. The private key never leaves your laptop; the server only needs the public half.
Swipe sideways to see all of it, or tap to open it full screen.
ssh-keygen -t ed25519            # create a key pair (once)
ssh -i ~/.ssh/mykey [email protected]   # log in as user "ubuntu"
exit                             # leave the server

The private key never leaves your machine; the server only stores the public key. Anyone holding your private key can log in as you, so protect it like a password.