Lesson 6 of 7

SSH and Remote Machines

Because a session is just a stream of , it works over a network connection too. Learn to use SSH to log in to and manage remote machines.

Bash
ssh user@server.example.com
# Encrypted connection; the server starts a shell for you;
# your keys flow there, its output flows back. Your
# terminal now controls the remote machine.

# Set up key-based login (no passwords, more secure):
ssh-keygen -t ed25519            # once: makes a keypair
ssh-copy-id user@server          # install public key on server
ssh user@server                  # now logs in with the key

# Run one command instead of a session:
ssh user@server 'df -h'

# Copy files over the same protocol:
scp report.pdf user@server:/tmp/
rsync -av ./site/ user@server:/var/www/   # sync, resumable

Everything from earlier lessons applies unchanged over SSH — , , , escape codes, vim. The doesn't know it's remote; the terminal doesn't know either. This is one reason the terminal is still the standard way to manage servers: sending text over a network is cheap and reliable.

Bash
# The one problem: hang up, and your processes die
# (they get SIGHUP when the TTY disappears).

# Solution: terminal multiplexers — tmux (or screen):
tmux                     # start a session on the server
long_running_job.sh      # kick something off
# Ctrl+B then D          # detach — job keeps running
# ...close laptop, fly home, reconnect...
tmux attach              # session exactly as you left it

# tmux also splits panes and windows inside one SSH
# connection — many terminals through one pipe.
Finished reading?Mark it done to track your progress.

Tip: use the ← and → keys to move between lessons.