Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 37 ยท Networking and Firewalls

Remote Access with SSH: Keys, Config and Tunnels

Set up and use SSH like an administrator: key pairs, authorized_keys, the client config file, jump hosts, port forwarding, scp and rsync, and the sshd_config basics.

SSH (Secure Shell) is the door through which you administer almost every Linux server. It encrypts the session, authenticates both sides, and can carry files and other network traffic. A good SSH setup makes daily work fast and safe; a sloppy one is the most common way servers get broken into. This lesson covers the client and server basics, and the next chapter's SSH hardening lesson tightens them.

Because the site cannot open network connections to real machines, commands are read-only snippets; the Python stand-ins parse and check configuration so you can practise the logic.

Key-based authentication

A key pair has a private key (stays on your computer, never shared) and a public key (placed on servers). The server proves it knows your public key by asking you to sign a challenge; the private key never travels.

Terminal
ssh-keygen -t ed25519 -C "alice@laptop"        # creates ~/.ssh/id_ed25519 and .pub
ssh-copy-id -i ~/.ssh/id_ed25519.pub alice@server
ssh alice@server
ssh-keygen -lf ~/.ssh/id_ed25519.pub            # fingerprint of a key

Prefer Ed25519 (short, fast, modern). RSA is still fine at 3072 bits or more. Protect the private key with a passphrase and load it once per session with ssh-agent (eval "$(ssh-agent)", then ssh-add), so you type the passphrase once.

On the server, the public keys that may log in as a user are listed one per line in ~/.ssh/authorized_keys. Permissions matter because sshd refuses keys in files that others can write:

Terminal
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

The first time you connect, SSH shows the server's host key fingerprint and records it in ~/.ssh/known_hosts. Verify it against a trusted source (cloud console, the admin). If the fingerprint later changes, SSH prints a loud warning: either the server was reinstalled, or someone is intercepting. Never delete the warning blindly.

Python Inspect an authorized_keys file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Options before the key restrict what that key may do: command="..." forces one command (ideal for backup jobs), from="203.0.113.0/24" limits source addresses, no-port-forwarding, no-pty and restrict switch off features.

The client config file

~/.ssh/config turns long commands into short names and applies settings per host:

TEXT
Host web
    HostName 203.0.113.10
    User alice
    Port 2222
    IdentityFile ~/.ssh/id_ed25519

Host db
    HostName 10.0.0.20
    User alice
    ProxyJump bastion

Host bastion
    HostName bastion.example.org
    User alice

Host *
    ServerAliveInterval 30
    AddKeysToAgent yes

Now ssh web or ssh db just works; ProxyJump bastion automatically passes through the jump host (equivalent to ssh -J bastion db). The first matching value for each setting wins, so put specific hosts before Host *. Keep the file mode 600.

Copying files: scp, sftp and rsync

Terminal
scp report.pdf web:/tmp/                  # copy to the server
scp -r web:/var/log/nginx ./logs          # copy a directory back
sftp web                                  # interactive file transfer
rsync -avz --progress ./site/ web:/var/www/site/
rsync -avz --delete -e "ssh -p 2222" ./site/ web:/var/www/site/

rsync sends only changes, resumes well and is the better choice for anything repeated. A trailing slash on the source (./site/) means "the contents of", which is the most frequent rsync surprise. Add -n (dry run) before using --delete.

Port forwarding and tunnels

SSH can carry other TCP connections inside its encrypted channel.

Terminal
# Local forward: your port 5433 reaches the database port 5432 behind the server
ssh -L 5433:127.0.0.1:5432 web
# Reach a host that only the server can see
ssh -L 8080:intranet.internal:80 web

# Remote forward: server's port 9000 reaches a service on your laptop
ssh -R 9000:127.0.0.1:3000 web

# Dynamic forward: a SOCKS5 proxy on localhost:1080
ssh -D 1080 web

# Background tunnel only, no shell
ssh -N -f -L 5433:127.0.0.1:5432 web

Think of -L as "bring a remote service here" and -R as "send a local service there". A tunnel to a database port means the database does not have to listen on a public address at all, which is a strong, simple security pattern.

Python Describe a forward in plain words

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The server side: sshd

The daemon (sshd) reads /etc/ssh/sshd_config (and drop-ins in /etc/ssh/sshd_config.d/). A few essentials:

TEXT
Port 22
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
AllowUsers alice bob

Always check the file before reloading, and keep your current session open while you test a new login from a second terminal:

Terminal
sudo sshd -t                       # syntax check
sudo systemctl reload ssh          # service is "sshd" on RHEL-family systems
sudo sshd -T | grep -i passwordauth   # effective value after includes

Troubleshoot a failing login with ssh -v alice@server on the client (add more v for more detail) and sudo journalctl -u ssh (or -u sshd) on the server. The usual culprits are file permissions on ~/.ssh, a home directory writable by others, SELinux contexts on authorized_keys, and an AllowUsers list that does not contain you.

SSH in four jobs

Log in Keys, not passwords.
Copy scp, sftp, rsync.
Tunnel -L, -R and -D.
Jump ProxyJump via a bastion.
1 / 2

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file lists the public keys allowed to log in as a user?

    authorized_keys on the server holds the accepted public keys; known_hosts on the client records server fingerprints.
  2. 2. What does ssh -L 5433:127.0.0.1:5432 web do?

    -L is a local forward: a local listening port is connected to the destination as seen from the server.
  3. 3. SSH warns that the remote host identification has changed. What is the right first reaction?

    A changed key can mean a man-in-the-middle; confirm out of band before trusting it.
  4. 4. Why run sshd -t before reloading sshd?

    A syntax error could stop the daemon from starting; the check is a free safety net.

Summary

  • Use Ed25519 key pairs with a passphrase and ssh-agent; install public keys in ~/.ssh/authorized_keys with strict permissions.
  • Verify host key fingerprints and never ignore a changed-key warning.
  • ~/.ssh/config provides short names, jump hosts (ProxyJump) and defaults.
  • scp and sftp copy files; rsync is best for repeated or large transfers.
  • -L, -R and -D forward ports; tunnelling lets internal services stay off public addresses.
  • Edit sshd_config carefully: sshd -t, reload, test from a second session.

Next: Reducing the Attack Surface