# SSH and remote access: reach servers safely

Authenticate with SSH keys, keep host identity checks, simplify connections with ssh_config, and forward a private port through a trusted jump host.

Course: [Networking for DevOps](https://devops.rajeev.pro/courses/networking), lesson 10. About 24 minutes, beginner. Source: https://devops.rajeev.pro/courses/networking/10-ssh-and-remote-access

## What you will learn

- Tell a user key from a server host key
- Use ssh-agent and a host alias without copying private keys to servers
- Explain local port forwarding and ProxyJump
- Keep host-key verification enabled

> **Lab status.** **Verified lab:** generated an Ed25519 key pair, trusted the temporary server's host key, and authenticated to an OpenSSH server inside one `alpine:3.22` container. The fingerprint and `root` output are real. The container removes both keys when it exits. <br />**Cloud lab:** none. No remote or cloud account was accessed.

Before SSH, engineers often exposed a shared password or copied credentials between servers. That made access hard
to audit and leaks hard to contain. SSH encrypts the session and uses keys to prove client and server identity.
Config aliases and tunnels reduce repeated typing without moving the private service onto the public internet. In
this lesson, you will inspect a key login, configure a host, and trace a tunnel.

## Keys and agents

> **Figure.** An SSH client signs a challenge with its private key; the server checks the matching public key in authorized_keys, while the server's host key proves the server identity.

SSH uses two different key pairs. Your **user key** authenticates you: the private half stays on your client, and
the public half goes in the server account's `~/.ssh/authorized_keys`. The server's **host key** identifies the
server to you. Check and retain its fingerprint through a trusted channel before accepting a first connection.

`ssh-agent` holds unlocked private keys in memory for your login session. `ssh-add` loads a key so you do not enter
its passphrase for every connection. The agent does not send your private key over the network. Avoid agent
forwarding to machines you do not trust; someone with access to the forwarded socket may ask the agent to sign.

Inspect a public-key fingerprint before you install it:

```sh
ssh-keygen -lf /tmp/lesson-key.pub
```

The disposable lab runs this command and prints the real fingerprint.

## SSH config

> **Figure.** An SSH host alias selects HostName, User, IdentityFile and an optional ProxyJump before the client connects to the final server.

`~/.ssh/config` stores connection options under a host alias. SSH matches the command-line name to `Host`, then
uses its `HostName`, `User`, and key settings. `ProxyJump` opens a connection through a bastion without copying
your user key to that host.

```sshconfig
Host web-prod
  HostName 203.0.113.10
  User deploy
  IdentityFile ~/.ssh/id_ed25519
  IdentitiesOnly yes
  ProxyJump bastion
```

`203.0.113.10` is a documentation-only example address. Replace it with a server you control. Keep
`StrictHostKeyChecking` enabled. Turning it off hides unexpected host-key changes instead of fixing them.

## Tunnels and port forwarding

> **Figure.** An app connects to localhost port 15432; SSH forwards those bytes through an encrypted session to a private database on port 5432.

Local forwarding makes a local port lead to a destination reachable from the SSH server:

```sh
ssh -N -L 15432:db.internal:5432 bastion
```

Here `15432` is on your machine; `db.internal:5432` is reached from the bastion. `-N` asks SSH not to start a
remote shell. Your database client then connects to `localhost:15432`. The tunnel encrypts the path to the
bastion, but database authentication and TLS still matter.

## Bastions and VPNs

A **bastion** is a tightly controlled host used as a jump point into a private network. A **VPN** creates a routed
network path from a client or site to that private network. Use a bastion for a narrow administrative hop; use a
VPN when approved users need broader access to multiple internal services.

Do not expose a database port to make SSH forwarding work. Restrict the bastion's inbound sources, keep access
logged, and patch it. A jump host is still a server and a security boundary, not a reason to trust every machine
behind it.

## Mission: Prove key authentication

Generate a temporary key, add its public half to a disposable OpenSSH server, verify the host key, then connect. The container removes all temporary key material.

- You need: Docker
- You'll have: A real key fingerprint and the authenticated account name

```sh
bash devops-academy-tools/networking-lab/10-capture.sh
```

Read the script before running it. It generates both host and user keys inside the temporary container and uses a
known-hosts file instead of disabling host verification.

## Try it

- Create a temporary Ed25519 key pair with `ssh-keygen` and inspect the public-key fingerprint.
- Add a host alias to a local SSH config and verify the resolved options with `ssh -G`.
- Draw the path for `ssh -L 15432:db.internal:5432 bastion`.
- Run the mini mission in its disposable Alpine container; its keys disappear with the container.

## Quiz

1. Which key should be installed in a server user's authorized_keys file?
   - The private key
   - **The public key** (answer)
   - The host private key
   - The agent socket
   Why: The server stores the user's public key and verifies that the client can sign with its matching private key.

2. What does LocalForward do in SSH?
   - **Copies a local port connection through SSH to a remote destination** (answer)
   - Copies a private key to the server
   - Changes the server's default route
   - Disables host-key checking
   Why: Local forwarding listens on the client side and carries connections over SSH to a host and port reachable from the server side.

3. Why should you keep StrictHostKeyChecking enabled?
   - It encrypts the private key on disk
   - **It helps detect an unexpected server host key** (answer)
   - It makes the connection faster
   - It automatically installs your public key
   Why: Host-key checking helps catch a changed or untrusted server identity instead of silently accepting it.

## Resources

- [ssh(1)](https://man.openbsd.org/ssh) (OpenBSD manual pages)
- [ssh_config(5)](https://man.openbsd.org/ssh_config) (OpenBSD manual pages)
- [ssh-agent(1)](https://man.openbsd.org/ssh-agent) (OpenBSD manual pages)
