Weight: 4
Goal: Use public-key tools to secure data and network traffic. Use SSH, OpenSSH, GPG, and basic features of SSL/TLS-based connections.
This is the largest security objective on the exam. Master SSH thoroughly — much of the material is about it.
| Type | Same key for encrypt and decrypt? | Examples |
|---|---|---|
| Symmetric | Yes — both sides share one secret. | AES, ChaCha20. |
| Asymmetric (public key) | No — a public key encrypts, a private key decrypts (and vice versa for signatures). | RSA, DSA, ECDSA, Ed25519. |
Asymmetric crypto solves the key-distribution problem: you can publish your public key freely. Anyone can encrypt to you, but only your private key can decrypt.
The two main uses:
In practice SSH and TLS combine the two: asymmetric crypto authenticates the connection and exchanges a fresh symmetric key, then symmetric crypto encrypts the bulk traffic.
SSH (Secure Shell) provides encrypted remote login, file copy, and tunneling. OpenSSH is the implementation on every Linux distribution. Default port: TCP 22.
Two halves:
sshd — the server, listens on the
remote machine.ssh — the client, runs on your
machine.ssh user@host # log in as user@host
ssh -p 2222 user@host # non-default port
ssh -i ~/.ssh/id_special user@host # use a specific private key
ssh -v user@host # verbose (debugging)The first time you connect to a host, the client shows the
host key fingerprint and asks you to accept it. The
fingerprint is then stored in ~/.ssh/known_hosts. On
subsequent connections, the client refuses to continue if the key
changes (the dreaded “REMOTE HOST IDENTIFICATION HAS CHANGED”
warning).
sshd runs)| File | Purpose |
|---|---|
/etc/ssh/sshd_config |
The server’s configuration. |
/etc/ssh/ssh_host_*_key |
Server private host keys (one per algorithm: rsa, ecdsa, ed25519). Mode 600, owned by root. |
/etc/ssh/ssh_host_*_key.pub |
Server public host keys. World-readable. |
Important sshd_config directives for the exam:
Port 22
PermitRootLogin no # disallow direct root SSH login
PasswordAuthentication yes # set to "no" once keys work
PubkeyAuthentication yes
PermitEmptyPasswords no
AllowUsers alice bob # whitelist
AllowGroups admins
X11Forwarding yes # required for ssh -X
After editing, reload the daemon:
systemctl reload sshd.
| File | Purpose |
|---|---|
/etc/ssh/ssh_config |
System-wide client defaults. |
~/.ssh/config |
Per-user client configuration. |
~/.ssh/known_hosts |
Public host keys of servers you’ve previously connected to. |
~/.ssh/authorized_keys |
List of public keys that may log in to this user’s account. (Lives on the server side, in the target user’s home.) |
~/.ssh/id_rsa, id_ecdsa,
id_ed25519 |
The user’s private keys. Mode 600. |
~/.ssh/id_*.pub |
The user’s public keys. Mode 644. |
~/.ssh itself must be mode 700, or the SSH server will
refuse to use the files in it (“strict modes”).
~/.ssh/config shortcutsHost work
HostName srv.example.com
User alice
Port 2222
IdentityFile ~/.ssh/work_key
Host *
ServerAliveInterval 60
Now ssh work does the right thing automatically.
The recommended way to authenticate — no passwords on the wire.
ssh-keygen # interactive, default RSA 3072 (modern openssh)
ssh-keygen -t ed25519 # modern, short, fast
ssh-keygen -t rsa -b 4096 # large RSA
ssh-keygen -f ~/.ssh/work_key # custom location
ssh-keygen -p -f ~/.ssh/id_ed25519 # change/remove the passphrase laterOutput: a private key (id_ed25519) and a public key
(id_ed25519.pub).
Always set a passphrase on private keys. The passphrase encrypts the key on disk so a stolen file is useless without it.
ssh-copy-id user@host # easiest way
ssh-copy-id -i ~/.ssh/work_key.pub user@host # specific keyssh-copy-id appends the public key to
~/.ssh/authorized_keys on the server and fixes permissions.
You can do the same manually:
cat ~/.ssh/id_ed25519.pub | ssh user@host \
'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'ssh user@hostIf the key has a passphrase, you’ll be prompted for it on each use —
solved by ssh-agent (next section).
~/.ssh/authorized_keys
optionsYou can prefix a key with options to restrict what it can do:
no-port-forwarding,no-X11-forwarding,command="/usr/local/bin/backup-only" ssh-ed25519 AAAAC3... user@host
The exam doesn’t drill into this, but recognize the file’s format: one public key per line, optionally with options.
ssh-agent and
ssh-addRe-typing the passphrase for every connection is annoying.
ssh-agent holds your decrypted private
keys in memory for the duration of a session.
eval "$(ssh-agent -s)" # start an agent, export env vars
ssh-add ~/.ssh/id_ed25519 # add a key (prompts once for passphrase)
ssh-add -l # list keys held by the agent
ssh-add -D # delete all keys from the agent
ssh-add -t 3600 ~/.ssh/key # cache for one hour, then forgetTwo important variables ssh-agent exports:
SSH_AGENT_PID — the agent’s PID.SSH_AUTH_SOCK — path to the socket the
client uses to talk to the agent.When SSH starts a session, it consults these variables. Agent forwarding lets a session on a remote host use your local agent:
ssh -A user@host # forward the agentThis is convenient but risky on untrusted servers (whoever controls the remote machine can use your keys via the socket).
known_hostsEvery SSH server has long-lived host keys in
/etc/ssh/ssh_host_*. When you connect, the server presents
its public host key; the client verifies that it matches the one
previously stored in ~/.ssh/known_hosts.
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub # fingerprint of a key
ssh-keygen -F host # search known_hosts
ssh-keygen -R host # REMOVE a host's entryUse ssh-keygen -R when you know the host’s keys
legitimately changed (rebuilt server, new VM) and you need to clear the
old fingerprint.
Tools that ride on top of SSH:
| Command | Use |
|---|---|
ssh |
Remote shell or command execution. |
scp |
Copy files over SSH. scp file user@host:/path,
scp -r dir user@host:. |
sftp |
Interactive file transfer over SSH (like FTP). |
ssh-copy-id |
Install a public key on a remote server. |
ssh-keygen |
Generate, manage, and inspect SSH keys. |
ssh-keyscan |
Fetch a host’s public keys (e.g. to pre-populate
known_hosts). |
SSH can forward arbitrary TCP traffic through its encrypted connection. There are three forms.
-LMake a local port reach a service through the SSH server.
ssh -L 8080:db.internal:5432 user@gatewayNow a program connecting to localhost:8080 on your
machine actually talks to db.internal:5432, through
gateway.
Format: -L LOCAL_PORT:TARGET_HOST:TARGET_PORT.
TARGET_HOST is resolved from the SSH server’s point
of view.
-RMake a port on the remote server reach a service on your local side.
ssh -R 9000:localhost:80 user@public-hostNow anyone connecting to public-host:9000 is routed to
your local localhost:80.
-Dssh -D 1080 user@gatewayThis starts a local SOCKS proxy on port 1080. Configure a browser to
use it, and your traffic is tunneled through gateway.
Already covered in 106.1, repeated here because it shows up in 110.3:
ssh -X user@host # X11 forwarding (untrusted mode)
ssh -Y user@host # X11 forwarding (trusted mode)Server side must allow it: X11Forwarding yes in
sshd_config.
GPG (GNU Privacy Guard) is an implementation of the OpenPGP standard for email and file encryption / signatures using public-key cryptography.
GPG and SSH solve different problems. Don’t confuse them.
| SSH | GPG | |
|---|---|---|
| Purpose | Encrypt live connections (interactive sessions, file transfer). | Encrypt and sign data at rest (files, email). |
| Keys live in | ~/.ssh/ |
~/.gnupg/ |
| Identity | A key per host/user, no central trust model. | Keys with user IDs (name + email), trust web. |
gpg --gen-key # interactive key generation
gpg --full-generate-key # detailed key generation
gpg --list-keys # public keyring (~/.gnupg/pubring.kbx)
gpg --list-secret-keys # private keys
gpg --export -a alice > alice.pub.asc # export a public key
gpg --import alice.pub.asc # import someone's key
gpg --delete-key alice
gpg --delete-secret-key alice
gpg --fingerprint alice # show the fingerprint# Encrypt for a recipient (using THEIR public key)
gpg -e -r alice@example.com file.txt
# Produces file.txt.gpg
# Decrypt (using YOUR private key)
gpg -d file.txt.gpg > file.txt
# Symmetric encryption with a passphrase (no key pair involved)
gpg -c file.txt
# Produces file.txt.gpg
# ASCII-armored output (text-safe, e.g. for email)
gpg -e -a -r alice file.txt
# Produces file.txt.ascgpg --sign file.txt # binary signed file (.gpg)
gpg --clearsign file.txt # readable text + signature block (.asc)
gpg --detach-sign file.txt # signature in a separate file (.sig)
gpg --verify file.txt.sig file.txt # verify a detached signature
gpg --verify file.txt.gpg # verify (and decrypt if applicable)Combining encryption and signing:
gpg -s -e -r alice file.txt # sign AND encrypt~/.gnupg/pubring.kbx (or pubring.gpg on older
versions) — public keyring.private-keys-v1.d/ — private keys.trustdb.gpg — trust database (who you’ve decided to
trust).gpg.conf — user configuration.gpg-agentLike ssh-agent, but for GPG. Caches your unlocked
private-key passphrase so you don’t retype it for every operation.
Started automatically by modern GPG.
Full TLS isn’t tested deeply at 102 level, but you should recognize:
openssl
command).A few openssl recipes that come up:
openssl s_client -connect host:443 # talk TLS to a server, inspect cert
openssl x509 -in cert.pem -text -noout # show certificate details
openssl genrsa -out key.pem 4096 # generate an RSA key
openssl req -new -key key.pem -out req.csr # certificate signing request
openssl dgst -sha256 file # SHA-256 hash of a fileYou should recognize that openssl exists and is the
standard tool for handling TLS certificates and keys.
SSH files:
/etc/ssh/sshd_config,
/etc/ssh/ssh_config/etc/ssh/ssh_host_*_key (and .pub)~/.ssh/config, ~/.ssh/known_hosts,
~/.ssh/authorized_keys~/.ssh/id_rsa, id_ecdsa,
id_ed25519 (and .pub)SSH commands:
ssh, scp, sftpssh-keygen, ssh-copy-id,
ssh-keyscanssh-agent, ssh-add-L, -R,
-D-X, -Y-ASSH environment variables:
SSH_AGENT_PIDSSH_AUTH_SOCKGPG:
gpg with -e (encrypt), -d
(decrypt), -s (sign), --clearsign,
--verify, -a (armor), -c
(symmetric)gpg --list-keys, --gen-key,
--export, --import~/.gnupg/gpg-agentConcepts:
What port does SSH use by default, and over which protocol? TCP port 22.
What is the difference between
/etc/ssh/sshd_config and
/etc/ssh/ssh_config? sshd_config
configures the SSH server. ssh_config is
the system-wide default for the SSH client.
Where on the server does a user’s list of accepted public
keys live? In the target user’s home directory:
~/.ssh/authorized_keys.
Where on the client are public keys of previously
connected servers stored?
~/.ssh/known_hosts.
What command generates a new SSH key pair?
ssh-keygen (optionally -t ed25519 to choose
the algorithm).
What command copies your public key to a remote server’s
authorized_keys?
ssh-copy-id user@host.
What is ssh-agent and what does
ssh-add do? ssh-agent holds decrypted
private keys in memory during a session so you don’t retype the
passphrase. ssh-add adds keys to the running
agent.
What two environment variables does
ssh-agent set? SSH_AGENT_PID and
SSH_AUTH_SOCK.
What does
ssh -L 8080:internal:80 user@gateway do? Forwards
your local port 8080 through gateway to
internal:80. Connecting to localhost:8080
reaches the internal service.
What does ssh -A user@host do?
Forwards your local ssh-agent connection to the remote
host, so commands there can use your keys.
Where does GPG store its keyrings? In
~/.gnupg/ (e.g. pubring.kbx,
private-keys-v1.d/, trustdb.gpg).
What gpg command encrypts a file for a
specific recipient?
gpg -e -r alice@example.com file.txt.
What is the difference between gpg --sign,
gpg --clearsign, and
gpg --detach-sign? --sign produces a
binary signed file. --clearsign keeps the original text
readable with a signature block appended. --detach-sign
produces a separate .sig file containing only the
signature.
What gpg command verifies a detached
signature? gpg --verify file.sig file.
What is the practical difference between SSH host keys
and SSH user keys? Host keys identify the
server to clients (/etc/ssh/ssh_host_*).
User keys identify the user to servers
(~/.ssh/id_*). Both are public/private pairs but serve
different sides of the trust relationship.
You connect to a server you’ve used before and SSH
refuses with “REMOTE HOST IDENTIFICATION HAS CHANGED”. What does this
mean and how do you resolve it (assuming you know the change is
legitimate)? The server’s host key in
~/.ssh/known_hosts no longer matches what the server
presents — possibly a reinstall, possibly an attack. If you know it’s
legitimate, run ssh-keygen -R hostname to remove the old
entry and reconnect.
What is the standard command-line tool to manage TLS
certificates and inspect HTTPS endpoints?
openssl.