ssh (2/2)
The second half of the ssh reference covers ssh_config keywords (also usable with -o): host key verification, crypto lists, forwarding, multiplexing, keepalive and QoS, session behaviour, logging, tokens and patterns. Every keyword is checked in a Docker lab with ssh -G (resolved config) and real output.
140.1 Where it runs and config order
- Every keyword works inside a Host block or on the command line with -o.
- Order: -o -> ~/.ssh/config -> /etc/ssh/ssh_config; the first value of each keyword wins, so specific Host blocks go on top.
- OpenSSH is built into Windows, Linux and macOS; multiplexing does not work on Windows.
- Lab: client 192.168.160.10 -> srv 192.168.160.20 (sshd :22); check resolved values with ssh -G.
140.2 Host key verification
- Step 1
1.client 192.168.160.10 connects to srv 192.168.160.20 on TCP 22, key exchange starts
- Step 2
2.srv sends its ED25519 host key
- Step 3
3.client looks it up in known_hosts (192.168.160.20 ssh-ed25519 AAAAC3Nza...)
- Step 4
4.New key: StrictHostKeyChecking decides (yes = refuse, accept-new = add, ask = prompt)
- Step 5
5.Changed key: big REMOTE HOST IDENTIFICATION HAS CHANGED warning, connection refused
ssh -F lab/config \ -o StrictHostKeyChecking=yes srv hostnamessh -F lab/config srv hostnamessh -F lab/config \ -o UserKnownHostsFile=~/.ssh/kh_hashed \ -o HashKnownHosts=yes srv truecut -c1-60 ~/.ssh/kh_hashedNo ED25519 host key is known for 192.168.160.20and you have requested strict checking.Host key verification failed. Warning: Permanently added '192.168.160.20' (ED25519)to the list of known hosts.srv |1|/6rHQNnDRsytwPChItjm52Kviks=|MqDCyM4j48O1dm9aVnmOeSZsw9M=- HashKnownHosts yes stores hashes, so a leaked known_hosts does not reveal server names.
- UpdateHostKeys yes: after login ssh learned the RSA and ECDSA keys too (Learned new hostkey), useful for key rotation.
- VerifyHostKeyDNS yes checks SSHFP DNS records; for an IP it logs skipped DNS lookup for numerical hostname.
# srv: new host keys (like a reinstall)rm -f /etc/ssh/ssh_host_*; ssh-keygen -A# client:ssh -F lab/config srv hostnamessh -F lab/config \ -o StrictHostKeyChecking=no \ -L 8090:127.0.0.1:8000 srv hostnamessh-keygen -R 192.168.160.20 -f ~/.ssh/kh_lab@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!The fingerprint for the ED25519 key sent by the remote host isSHA256:wE2OVl49waZqUAwP3CZXWTZzAUDr/IG44MMZs4mJwqE.Offending ED25519 key in /root/.ssh/kh_lab:1Host key verification failed. # with StrictHostKeyChecking=no:Password authentication is disabled to avoid man-in-the-middle attacks.Port forwarding is disabled to avoid man-in-the-middle attacks.srv # Host 192.168.160.20 found: line 1/root/.ssh/kh_lab updated.| Keyword | Meaning |
|---|---|
| StrictHostKeyChecking yes | accept-new | no | ask | Refuse unknown / add new, refuse changed / connect anyway / prompt (default) |
| UserKnownHostsFile / GlobalKnownHostsFile | User file (none = ignore) / system-wide file |
| HashKnownHosts yes | Hash host names in known_hosts |
| HostKeyAlias name | Save the key under a fixed name |
| HostKeyAlgorithms +ssh-rsa | Allowed host key algorithms (+ - ^) |
| CheckHostIP yes | Check the IP too |
| UpdateHostKeys yes | ask | no | Learn extra/rotated host keys |
| VerifyHostKeyDNS yes | ask | no | Verify with SSHFP DNS records |
| RevokedHostKeys file | Revoked keys / KRL |
140.3 Ciphers, MACs and key exchange
ssh -F lab/config -vv srv truessh -F lab/config -v -o Ciphers=aes256-ctr \ -o MACs=hmac-sha2-512-etm@openssh.com \ srv truessh -F lab/config -o \ KexAlgorithms=diffie-hellman-group1-sha1 \ srv truedebug1: kex: algorithm: sntrup761x25519-sha512@openssh.comdebug1: kex: host key algorithm: ssh-ed25519debug1: kex: server->client cipher: chacha20-poly1305 @openssh.com MAC: <implicit> compression: none debug1: kex: client->server cipher: aes256-ctr MAC: hmac-sha2-512-etm@openssh.com compression: none Unable to negotiate with 192.168.160.20 port 22:no matching key exchange method found. Their offer:sntrup761x25519-sha512@openssh.com,curve25519-sha256,…| Prefix | Effect | Example |
|---|---|---|
| (none) | Replace the whole list | Ciphers=aes256-ctr |
| - | Remove from the default list | Ciphers=-chacha20* |
| ^ | Move to the front | Ciphers=^aes256-gcm@openssh.com |
| + | Append a legacy algorithm at the end | KexAlgorithms=+diffie-hellman-group14-sha1 |
ssh -F lab/config -G srv | grep '^ciphers 'ssh -F lab/config -G -o 'Ciphers=-chacha20*' \ srv | grep '^ciphers 'ssh -F lab/config -G \ -o 'Ciphers=^aes256-gcm@openssh.com' \ srv | grep '^ciphers 'ciphers chacha20-poly1305@openssh.com,aes128-ctr, aes192-ctr,aes256-ctr,aes128-gcm@openssh.com, aes256-gcm@openssh.comciphers aes128-ctr,aes192-ctr,aes256-ctr, aes128-gcm@openssh.com,aes256-gcm@openssh.comciphers aes256-gcm@openssh.com,chacha20-poly1305@ openssh.com,aes128-ctr,aes192-ctr,aes256-ctr, aes128-gcm@openssh.com- CBC and 3DES ciphers are outside the default (legacy); -etm (encrypt-then-MAC) MACs are best.
- RekeyLimit='1G 1h' rekeys by data/time; Compression=yes is like -C.
140.4 Forwarding in the config
- Step 1
1.LocalForward 8080: request on client:8080 goes through the tunnel to srv, srv forwards to its web server :8000
- Step 2
2.RemoteForward 9000: srv:9000 reaches the dev app on client 127.0.0.1:3000
- Step 3
3.DynamicForward 1080: SOCKS proxy on the client, every request exits via srv
- Step 4
4.ExitOnForwardFailure yes: if a port is busy, ssh exits instead of running without the tunnel
Host srv-tun LocalForward 8080 192.168.160.20:8000 RemoteForward 9000 127.0.0.1:3000 DynamicForward 1080 ExitOnForwardFailure yes SessionType none Host srv* HostName 192.168.160.20 User dev IdentityFile ${HOME}/.ssh/id_lab IdentitiesOnly yes UserKnownHostsFile ~/.ssh/kh_lab StrictHostKeyChecking accept-newssh -F lab/config -f srv-tuncurl -sS http://127.0.0.1:8080/ssh -F lab/config srv-tun; echo exit=$?ssh -F lab/config -f -N \ -L 8081:192.168.160.20:8000 \ -o GatewayPorts=yes srvss -ltn | grep -E ':808[12] 'srv: web page on :8000[srv]$ curl -sS http://127.0.0.1:9000/client: dev app on 127.0.0.1:3000 bind [127.0.0.1]:8080: Address in usechannel_setup_fwd_listener_tcpip: cannot listen to port: 8080Could not request local forwarding.exit=255 LISTEN 0 128 127.0.0.1:8082 0.0.0.0:*LISTEN 0 128 0.0.0.0:8081 0.0.0.0:*- SessionType none = tunnel only, no shell (like -N); the common settings sit in Host srv* below because the first value wins.
- ExitOnForwardFailure no: the same error is only a warning and ssh keeps running without the tunnel.
- ClearAllForwardings yes clears every forward; GatewayPorts yes binds on 0.0.0.0 (default localhost).
- ForwardAgent yes gives srv an agent socket (sock=/tmp/ssh-XXXX.../agent.143) and ssh-add -l works there; root on srv can use it, so only for trusted servers.
- Others: ForwardX11 / ForwardX11Trusted (-X / -Y), Tunnel + TunnelDevice (tun/tap VPN), ChannelTimeout session=5m, PermitRemoteOpen.
140.5 Multiplexing, keepalive and QoS
- Step 1
1.Idle session: client sends an encrypted ServerAlive every 2 s, srv replies
- Step 2
2.srv hangs (docker pause n140-srv)
- Step 3
3.ServerAlive 1 and 2 get no reply
- Step 4
4.After ServerAliveCountMax=2 misses: Timeout, server 192.168.160.20 not responding.
- Step 5
5.ssh exits with 255 after about 10 s
# lab/config: Host srv-mux# ControlMaster auto# ControlPath ~/.ssh/cm-%r@%h:%p# ControlPersist 60ssh -F lab/config srv-mux hostname # firstssh -F lab/config -O check srv-muxssh -F lab/config srv-mux hostname # secondss -tn state established '( sport = :22 )' \ | grep -c 192.168.160.10 # on srvssh -F lab/config -O exit srv-muxcontrolpath /root/.ssh/cm-dev@192.168.160.20:22first: 1096 msMaster running (pid=334)second: 115 ms1Exit request sent.ssh -F lab/config -o ServerAliveInterval=2 \ -o ServerAliveCountMax=2 srv 'sleep 60' &docker pause n140-srv # on the hostssh -F lab/config -o TCPKeepAlive=no srv 'sleep 4' &ss -tno state established '( dport = :22 )'ssh -F lab/config -o IPQoS=ef srv 'sleep 1'Timeout, server 192.168.160.20 not responding.exit 255 after 10s # TCPKeepAlive yes (default)192.168.160.10:53362 192.168.160.20:22 timer:(keepalive,119min,0)# TCPKeepAlive=no192.168.160.10:57462 192.168.160.20:22 # ipqos af21 cs1 (default) 6 tos 0x48# -o IPQoS=ef 6 tos 0xb8| Keyword | Meaning |
|---|---|
| ControlMaster auto | yes | no | ask | Become master if none / always / reuse only / confirm |
| ControlPath ~/.ssh/cm-%C | none | Socket path (%C hash is best) / sharing off |
| ControlPersist 60 | 10m | yes | Keep master after last session closes |
| ServerAliveInterval / CountMax | Encrypted keepalive in ssh; default 3 misses |
| TCPKeepAlive yes | no | Kernel TCP keepalive timer |
| IPQoS af21 cs1 | ef | ... | DSCP mark for interactive / bulk traffic |
| ObscureKeystrokeTiming yes | Fake packets hide typing pattern |
140.6 Session behaviour and logging
ssh -F lab/config \ -o 'RemoteCommand=whoami; hostname' srvssh -F lab/config -o RequestTTY=force srv ttyLANG=en_IN.UTF-8 ssh -F lab/config \ -o SendEnv=LANG \ -o SetEnv='APP_ENV=lab DEBUG_X=1' srv \ 'echo $LANG $APP_ENV $DEBUG_X'ssh -F lab/config -o PermitLocalCommand=yes \ -o 'LocalCommand=echo local: connected to %h' \ srv hostnamedevsrv/dev/pts/0Connection to 192.168.160.20 closed.[srv] sshd drop-in: AcceptEnv LANG LC_* APP_ENVLANG=en_IN.UTF-8 APP_ENV=lab DEBUG_X=local: connected to 192.168.160.20srv- SendEnv/SetEnv reach the server only if its AcceptEnv allows them (DEBUG_X came back empty).
- LocalCommand runs on the client only with PermitLocalCommand yes.
- Config versions of flags: SessionType none (-N), subsystem (-s), StdinNull (-n), ForkAfterAuthentication (-f), EscapeChar (-e), RequestTTY (-t/-T).
- LogLevel: QUIET prints nothing (exit 255 only), VERBOSE shows auth method and bytes, DEBUG1 = 60 lines vs DEBUG3 = 197; -E writes to a file.
$ ssh -F lab/config -o LogLevel=VERBOSE srv trueAuthenticated to 192.168.160.20 ([192.168.160.20]:22) using "publickey".Transferred: sent 3500, received 4028 bytes, in 0.1 seconds140.7 Tokens, environment variables and patterns
ssh -F lab/config -o PermitLocalCommand=yes \ -o 'LocalCommand=echo h=%h p=%p r=%r u=%u' \ srv truessh -F lab/config -o PermitLocalCommand=yes \ -o 'LocalCommand=echo d=%d L=%L C=%C' \ srv truessh -F lab/config srv 'echo $SSH_CONNECTION'h=192.168.160.20 p=22 r=dev u=rootd=/root L=clientC=d4b0a0d83585a5899d24a24006f114da2f2e4ce2192.168.160.10 36166 192.168.160.20 22| Token / variable | Meaning |
|---|---|
| %h / %p / %r | Remote host / port / remote user |
| %u / %d / %L | Local user / home dir / client hostname |
| %C | Hash of all of them: best for ControlPath |
| %n / %j | Name as typed / ProxyJump value |
| ${HOME} | Env var in IdentityFile, ControlPath, UserKnownHostsFile (-G shows it as written, -v shows the real path) |
| SSH_CONNECTION / SSH_TTY | Client + server IP:port / session tty (on the server) |
| SSH_AUTH_SOCK / SSH_ASKPASS | Agent socket / passphrase program |
Host *.lab !bad.lab User dev Port 2222 Match host 192.168.160.* user ops IdentityFile ~/.ssh/id_ops Match exec "test -f /tmp/office-vpn-up" ProxyJump dev@192.168.160.20 Host 192.168.160.? Port 2200 Host * User guest- web.lab -> user dev, port 2222; bad.lab (negated) -> user guest, port 22.
- ops@192.168.160.20 -> identityfile ~/.ssh/id_ops (Match host + user both true).
- ProxyJump appears only after touch /tmp/office-vpn-up (Match exec exit 0 = match).
- 192.168.160.5 matches ? (exactly one character) -> port 2200; * = zero or more; a negated pattern alone never matches.
140.8 Common mistakes
- Host * User guest above Host srv User dev: ssh srv logs in as guest (first value wins, Host * matches srv too).
- Thinking StrictHostKeyChecking=no only removes a prompt: ssh connects even with a changed key; use accept-new and verify fingerprints on change.
- Tunnels silently missing because ExitOnForwardFailure is off.
- ForwardAgent yes on untrusted servers.
- Long ControlPath without %C hitting the Unix socket path length limit.
140.9 Interview tips
- Fast ssh: ControlMaster auto + ControlPath with %C + ControlPersist reuse one TCP connection (1096 ms -> 115 ms in the lab).
- Stable ssh: ServerAliveInterval + ServerAliveCountMax detect dead servers and keep NAT from dropping idle sessions.
- Safe host keys: StrictHostKeyChecking accept-new, HashKnownHosts yes, ssh-keygen -R only after verifying the new fingerprint.
- Algorithm lists take + (append), - (remove), ^ (move to front); verify with ssh -G.