SkillPilotAI

ssh (1/2)

A command reference for the ssh client: connection options, identity, session/terminal flags, tunnels, multiplexing, crypto selection, -Q queries, debugging, escape sequences and the first half of ssh_config. Knowing these flags turns ssh from a login tool into a troubleshooting and automation tool.

139.1 What it does and where it runs

  • Opens an encrypted login shell, runs a remote command, or builds tunnels.
  • Windows 10 1809+ / 11: built-in OpenSSH Client; Linux: openssh-client (apt) / openssh-clients (dnf); macOS: built in at /usr/bin/ssh.
  • Lab: Docker containers client 192.168.159.10, bastion 192.168.159.20 (sshd on :2222), web 192.168.159.30 (:22), OpenSSH 9.7p1.
ssh on the network: client -> bastion -> web
  1. 1.client 192.168.159.10 opens TCP to bastion 192.168.159.20:2222

  2. 2.The remote command runs inside that same encrypted connection

  3. 3.For web: ssh first connects to the bastion (ProxyJump)

  4. 4.Bastion forwards the stream with -W [192.168.159.30]:22

  5. 5.Second, end-to-end encrypted SSH session to web runs inside it

139.2 Connection basics

Terminal
ssh -4 -p 2222 -l dev -i ~/.ssh/id_lab \  192.168.159.20 \  'echo $SSH_CONNECTION; whoami; hostname'ssh -i ~/.ssh/id_lab dev@192.168.159.20 uptimessh -i ~/.ssh/id_lab \  ssh://dev@192.168.159.20:2222 \  ls -ld /home/devssh -o ConnectTimeout=3 \  dev@192.168.159.99 truessh -p 2299 dev@192.168.159.20 true
Output
192.168.159.10 49696 192.168.159.20 2222devbastion 04:31:06 up  4:04,  0 users,  load average: 1.61, 1.65, 1.63drwxr-sr-x    3 dev   dev   4096 Nov  9 04:30 /home/devssh: connect to host 192.168.159.99 port 22:  Operation timed outssh: connect to host 192.168.159.20 port 2299:  Connection refused
OptionMeaning
-p 2222 / -l adminPort (default 22) / login name
-i ~/.ssh/id_ed25519Private key (repeatable); -i key.pub = that key from the agent
-4 / -6IPv4 only / IPv6 only
-b 192.168.1.5 / -B eth0Source address / bind to interface
-J jump@bastion (-J j1,j2)Go via jump host(s), in order
-W host:portstdin/stdout -> host:port (used by ProxyJump)
-Cgzip compression for slow links
ssh host cmd argsRun a command, then exit
  • Operation timed out = no route / firewall; Connection refused = host reachable but no sshd on that port.

139.3 Authentication and identity

Terminal
ssh-add -lssh -F none -v -p 2222 -i ~/.ssh/id_lab \  dev@192.168.159.20 true 2>&1 |  grep -E 'Will attempt key|Offering|accepts'
Output
256 SHA256:zeVg4Za9eaGYE/8q9… other@n139 (ED25519)debug1: Will attempt key: other@n139 ED25519 SHA256:zeVg4Za9… agentdebug1: Will attempt key: /root/.ssh/id_lab ED25519 SHA256:Zefrr… explicitdebug1: Offering public key: other@n139 ED25519 SHA256:zeVg4Za9… agentdebug1: Offering public key: /root/.ssh/id_lab ED25519 SHA256:Zefrr… explicitdebug1: Server accepts key: /root/.ssh/id_lab ED25519 SHA256:Zefrr… explicitAuthenticated to 192.168.159.20 ([192.168.159.20]:2222)  using "publickey".
  • ssh offers agent keys first, then the explicit -i key; with many agent keys you can hit the server's MaxAuthTries (fix: IdentitiesOnly yes).
  • -A agent forwarding (careful: root on the remote can use your agent), -a off; prefer -J for jump hosts.
  • -K / -k: GSSAPI (Kerberos) delegation on / off; -I opensc-pkcs11.so: smartcard or hardware token.

139.4 Session, terminal and I/O

Terminal
ssh -T bastion ttyssh -t bastion ttyssh -tt bastion ttyecho hello-stdin | ssh bastion catecho hello-stdin | ssh -n bastion cat; \  echo '(nothing read)'
Output
$ ssh -T bastion ttynot a tty$ ssh -t bastion ttyPseudo-terminal will not be allocated because  stdin is not a terminal.not a tty$ ssh -tt bastion tty/dev/pts/0Connection to 192.168.159.20 closed.$ echo hello-stdin | ssh bastion cathello-stdin$ echo hello-stdin | ssh -n bastion cat; echo '(nothing read)'(nothing read)
OptionUse
-t / -ttForce a pty (top, vim) / pty even without a local tty
-TNo pty (git, scripts)
-nstdin = /dev/null (background jobs)
-sRequest a subsystem, like sftp
-e '^' / -e noneChange escape char (default ~) / escapes off, binary-safe
-X / -Y / -xX11 forwarding / trusted X11 / off

139.5 Port forwarding and tunnels

Terminal
ssh -f -N -L 8080:192.168.159.30:8000 bastioncurl -sS http://127.0.0.1:8080/ssh -f -N -R 9000:127.0.0.1:3000 bastioncurl -sS http://127.0.0.1:9000/  # on bastionssh -f -N -D 1080 bastioncurl -sS --socks5-hostname 127.0.0.1:1080 \  http://192.168.159.30:8000/ps -o pid,args | grep '[s]sh -f'
Output
web: internal page on :8000[bastion]$ curl -sS http://127.0.0.1:9000/client: dev app on 127.0.0.1:3000web: internal page on :8000  338 ssh -f -N -L 8080:192.168.159.30:8000 bastion  348 ssh -f -N -R 9000:127.0.0.1:3000 bastion  358 ssh -f -N -D 1080 bastion
OptionMeaning
-L 127.0.0.1:3306:db:3306Local port, localhost only (default)
-L '*:8080:host:80' / -gListen on all interfaces / allow remote hosts
-L 5432:/run/pg.sockLocal port -> remote Unix socket
-R 0.0.0.0:9000:...Remote port on all interfaces (needs GatewayPorts)
-R 0:localhost:3000Server picks a free port
-R 1080Remote-side SOCKS proxy
-D 127.0.0.1:1080Local SOCKS4/5 proxy
-f / -NBackground / no remote command
-w 0:1tun device VPN (root)

139.6 Connection sharing (multiplexing)

Multiplexing: one TCP, many sessions
  1. 1.ssh -M -S /tmp/ctl-%C -f -N bastion: master does TCP + full handshake once

  2. 2.Master creates the control socket /tmp/ctl-<hash>

  3. 3.Next ssh -S /tmp/ctl-%C bastion connects to the socket

  4. 4.New session opens inside the same TCP 2222 connection: no new handshake

  5. 5.Login time drops from ~1100 ms to ~30 ms

Terminal
ssh -M -S /tmp/ctl-%C -f -N bastionssh -S /tmp/ctl-%C -O check bastionfor i in 1 2 3; do  ms ssh -S none bastion true; donefor i in 1 2 3; do  ms ssh -S /tmp/ctl-%C bastion true; donessh -S /tmp/ctl-%C -O forward \  -L 8081:192.168.159.30:8000 bastionssh -S /tmp/ctl-%C -O exit bastion
Output
Master running (pid=387)1109 ms1278 ms1076 ms27 ms39 ms30 msExit request sent.
  • -O check / forward / cancel / exit / stop control the master; -S none turns sharing off; -MM asks to confirm each session (askpass).
  • -O forward adds a tunnel to a running master without a new login.

139.7 Crypto selection and -Q queries

Terminal
ssh -v -c aes256-gcm@openssh.com bastion \  true 2>&1 | grep 'kex: client->server'ssh -v -c aes128-ctr \  -m hmac-sha2-256-etm@openssh.com bastion \  true 2>&1 | grep 'kex: client->server'ssh -c aes128-cbc bastion truessh -Q cipher-authssh -Q kex
Output
debug1: kex: client->server cipher: aes256-gcm@openssh.com  MAC: <implicit> compression: nonedebug1: kex: client->server cipher: aes128-ctr  MAC: hmac-sha2-256-etm@openssh.com compression: noneUnable to negotiate with 192.168.159.20 port 2222:  no matching cipher found. Their offer:  chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,  aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com aes128-gcm@openssh.comaes256-gcm@openssh.comchacha20-poly1305@openssh.com diffie-hellman-group14-sha256diffie-hellman-group16-sha512diffie-hellman-group-exchange-sha256ecdh-sha2-nistp256curve25519-sha256curve25519-sha256@libssh.orgsntrup761x25519-sha512@openssh.com
  • -c picks the cipher, -m the MAC; GCM/chacha20 are AEAD so the MAC is implicit, -m matters with CTR.
  • A no matching cipher / MAC error prints the server's offer: pick from that list.
  • -Q terms: cipher (10 here), cipher-auth, mac (16), kex, key, key-plain, key-cert, sig, protocol-version (only 2), and config keywords like PubkeyAcceptedAlgorithms.

139.8 Configuration, logging and escape sequences

Terminal
ssh -Vssh -v bastion true 2>&1 | head -5ssh -E /tmp/ssh-debug.log -v bastion hostnamessh -vvv bastion true 2>&1 | wc -l
Output
OpenSSH_9.7p1, OpenSSL 3.3.0 9 Apr 2024debug1: Reading configuration data /root/.ssh/configdebug1: Reading configuration data /root/lab/conf.d/ops.confdebug1: /root/.ssh/config line 3: Applying options for bastiondebug1: /root/.ssh/config line 12: Applying options for *bastion235
Option / escapeMeaning
-F file / -F noneUse another config, skip system / read no config
-GPrint the resolved config and exit
-P tagSet a tag for Match tagged
-o Key=valueAny ssh_config option
-v -vv -vvv / -qDebug (72 vs 235 lines here) / quiet
-E file.log / -yAppend debug logs to a file / to syslog
~.Disconnect a hung session
~^Z / ~& / ~#Suspend / background at logout / list forwards
~Cssh> prompt to add -L/-R/-D or cancel -KL (EnableEscapeCommandline yes)
~? / ~~List escapes / send a literal ~
  • Escapes work only in a pty session and only right after a newline (Enter, then ~.).

139.9 ssh_config: blocks, Match and Include

Output
Include ~/lab/conf.d/*.confHost bastion  HostName 192.168.159.20  Port 2222  IdentitiesOnly yesHost web  HostName 192.168.159.30  ProxyJump bastionMatch tagged ci  BatchMode yesHost *  User dev  IdentityFile ~/.ssh/id_lab  ConnectTimeout 5
Terminal
ssh -F lab/config -G bastionssh -F lab/config -G -P ci bastionssh -F lab/config -G -o 'Match=user root' \  -o Port=2200 root@192.168.159.20ssh web 'hostname; echo $SSH_CONNECTION'ssh -v web true 2>&1 | grep ProxyCommand
Output
user devhostname 192.168.159.20port 2222identitiesonly yesidentityfile ~/.ssh/id_labconnecttimeout 5 batchmode yestag ci Host directive not supported as a command-line option web192.168.159.20 49980 192.168.159.30 22debug1: Setting implicit ProxyCommand from ProxyJump:  ssh -v -W '[%h]:%p' bastion
  • First value wins: the Included ops.conf set User ops before Host *, so ssh -G ops shows user ops. Keep specific Host blocks on top, Host * at the bottom.
  • Match criteria: host, originalhost, user, localuser, exec "cmd", localnetwork, tagged, canonical/final, all.
  • IgnoreUnknown UseKeychain at the top keeps macOS-only options from breaking on Linux.
  • ProxyCommand runs any command as the link (nc for an HTTP proxy, classic ssh -W %h:%p jump); Canonicalize* expands short names.

139.10 ssh_config: authentication keywords

Terminal
ssh ops whoamissh -F none -o IdentitiesOnly=yes \  -o PreferredAuthentications=publickey \  -i ~/.ssh/id_other -p 2222 \  dev@192.168.159.20 truessh -F none -o IdentityFile=none \  -o IdentityAgent=none -o BatchMode=yes \  -p 2222 dev@192.168.159.20 true
Output
ops@192.168.159.20's password:  ****opsdev@192.168.159.20: Permission denied  (publickey,password,keyboard-interactive).dev@192.168.159.20: Permission denied  (publickey,password,keyboard-interactive).
KeywordMeaning
IdentityFile / IdentitiesOnly yesKey to use / offer only configured keys
IdentityAgent none|pathAgent socket
AddKeysToAgent yes|ask|confirm|1hLoad the key into the agent on use
PreferredAuthenticationsOrder of methods (password first for ops)
PasswordAuthentication noTurn off password login
BatchMode yesNever prompt: fail fast in scripts
PubkeyAcceptedAlgorithms +ssh-rsaAllow old RSA signatures for old servers
GSSAPI*Kerberos authentication

139.11 Common mistakes

  • Putting Host * with User root at the top: it wins over the later Host bastion User dev.
  • Trying -o 'Match=...' or Host on the command line: not supported; write a small config and use -F.
  • Too many agent keys -> Too many authentication failures; use IdentitiesOnly yes.
  • Using -A on untrusted jump hosts instead of -J.
  • Scripts hanging on a password prompt: add BatchMode yes.

139.12 Interview tips

  • Debugging SSH: ssh -G host (final hostname, port, user, identityfile), then ssh -v (which key offered, which methods allowed), then read the error: timed out = network/firewall, refused = no sshd.
  • Multiplexing (-M -S, ControlMaster) cuts repeat logins from ~1 s to ~30 ms.
  • -T for scripts/git, -t for TUIs, -n for background jobs.
  • In ssh_config the first value found wins.