20.5. Organizing Remote Connections

Real work touches the same handful of hosts all day, through relays, with the same keys, moving files back and forth. The client configuration file is where that repetition belongs; multiplexing removes its per-connection cost; the jump host pattern makes the relay safe; and two file-transfer tools built on the same plumbing finish the toolkit.

20.5.1. Per-Host Client Configuration and Connection Multiplexing

The sshserver block now gains the multiplexing settings used below. Edit ~/.ssh/config and add the three Control lines to it; the whole file as it should read afterwards:

$ cat ~/.ssh/config
Host sshjump
    HostName 192.168.220.30
    User john
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes

Host sshserver
    HostName 192.168.221.20
    User john
    ProxyJump sshjump
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes
    ControlMaster auto
    ControlPath ~/.ssh/cm-%r@%h-%p
    ControlPersist 5m

How the file is read: for each option, the first value that applies wins - options given on the command line come before the file, and within the file the first matching Host block supplies its settings. Patterns are allowed (Host *.example), and Include files work as they do on the server side. HostName maps the short name to an address, User removes the john@ prefix, IdentityFile names the key, and IdentitiesOnly yes keeps the agent from offering other keys alongside it.

To see what ssh will actually do with all of that - without connecting - print the resolved configuration. Selected output; the full listing runs to dozens of lines:

$ ssh -G sshserver
host sshserver
user john
hostname 192.168.221.20
port 22
controlmaster auto
identitiesonly yes
stricthostkeychecking ask
controlpath /home/john/.ssh/cm-john@192.168.221.20-22
identityfile ~/.ssh/id_ed25519
userknownhostsfile /home/john/.ssh/known_hosts /home/john/.ssh/known_hosts2
controlpersist 300
proxyjump sshjump

ssh -G is the client-side sibling of sshd -T: it flattens files, defaults, and command-line options into the values a connection would really use. Note the %r@%h-%p tokens in ControlPath already expanded to cm-john@192.168.221.20-22 - user, host, and port, so different destinations get different sockets.

The three Control* settings are multiplexing. The first connection to a destination becomes a master and leaves a control socket at ControlPath; later connections to the same destination reuse the existing tunnel instead of repeating the whole key exchange and authentication; and ControlPersist 5m keeps the master alive for five minutes after the last client goes away. Watch the lifecycle:

$ ssh sshserver hostname
sshserver

That was the first connection, so it authenticated and became the master. Ask the master directly:

$ ssh -O check sshserver
Master running (pid=24432)

A second connection now rides the existing session - no new handshake, no new authentication, no visible cost:

$ ssh sshserver hostname
sshserver

Close it deliberately when finished:

$ ssh -O exit sshserver
Exit request sent.

Two cautions. The socket belongs to john's account, so anything running as john can use it - on a shared workstation that is the usual trust boundary, but it is worth knowing. And a cached master hides changes: while it lives, new connections skip authentication entirely, so when testing key rotation or server policy (Sections 20.2.4 and 20.4.4), close the master first or the test exercises the cache instead of the credential.

20.5.2. Jump Hosts with ProxyJump

The practice network was drawn to make this section necessary: sshadmin has no path into the internal network. A direct connection to sshserver leaves by the default route, and the gateway beyond it - the administration network's router, which has no leg on that isolated segment - refuses the attempt at once rather than letting it time out:

$ ssh -o ConnectTimeout=3 john@192.168.221.20 hostname
ssh: connect to host 192.168.221.20 port 22: Connection refused

Through the jump, the same destination works, and has worked all chapter:

$ ssh sshserver hostname
sshserver

What ProxyJump actually does is small and worth seeing once. In the recorded verbose session of Section 20.3.3, two lines show the mechanism:

debug1: Setting implicit ProxyCommand from ProxyJump: ssh -vvv -W '[%h]:%p' sshjump
debug1: Executing proxy command: exec ssh -vvv -W '[192.168.221.20]:2222' sshjump

ssh opened an ordinary connection to sshjump and asked it to relay bytes to the destination's address and port (-W - the same stdio forwarding used explicitly in Section 20.6.2). The destination's SSH protocol then runs end to end: john's client exchanges keys and authenticates with sshserver directly; the jump relays ciphertext and cannot read or alter it. Both ends are verified separately - the jump's own host key when the first leg connects, sshserver's when the tunnelled exchange starts. For a one-off route, the same thing works without any configuration: ssh -J john@192.168.220.30 john@192.168.221.20.

Method How it works Key material exposed to the jump When to use
ProxyJump / -J Client relays the destination's SSH stream through the jump and authenticates end-to-end None - the jump sees ciphertext only The default choice whenever the destination is reachable from the jump
Agent forwarding (-A) Log in to the jump, then ssh onward; the jump can ask john's agent to sign The agent socket, usable by the jump's root while the session lives Only when the connection must originate on the jump (source-address checks, tools that exist only there)
ProxyCommand ssh -W The explicit form of ProxyJump None Legacy configurations predating ProxyJump

That table is the security argument of this whole chapter in one row each: relaying bytes is safe; forwarding the ability to sign is a trust decision. A jump host should also be worth trusting - minimal software, no unneeded accounts, and the same hardened sshd policy as Section 20.4.1 - because everyone's traffic passes through it, even though no one's keys do.

20.5.3. SFTP and rsync over SSH

Both standard transfer tools use the same client configuration, keys, agent, and jump route as interactive sessions - nothing extra to configure. Create a sample file and a place to put it (both write silently; the checksum confirms the content):

$ mkdir -p ~/debian-handbook/ssh/upload
$ printf "OpenSSH transfer exercise\n" > ~/debian-handbook/ssh/notes.txt
$ sha256sum ~/debian-handbook/ssh/notes.txt
aafde43e1437704536b92b7fe98b3f2b80ff36785a28e40c592a7bf46daea9c7  /home/john/debian-handbook/ssh/notes.txt

SFTP is the interactive one. From the workspace on sshadmin, it opens a session, presents an sftp> prompt, and takes its own small command set:

$ sftp sshserver
Connected to sshserver.
sftp> put notes.txt debian-handbook/ssh/upload/notes-sftp.txt
Uploading notes.txt to /home/john/debian-handbook/ssh/upload/notes-sftp.txt
sftp> ls -l debian-handbook/ssh/upload/notes-sftp.txt
-rw-rw-r--    ? john     john           26 Nov 21 23:09 debian-handbook/ssh/upload/notes-sftp.txt
sftp> bye

put uploads, ls -l lists the remote side (lls lists the local one), and bye - or quit - leaves the session. Remote paths are relative to the remote home directory, which is why the upload landed under debian-handbook/ssh/upload/. Verify from the other end, on sshserver as john:

$ cat ~/debian-handbook/ssh/upload/notes-sftp.txt
OpenSSH transfer exercise

rsync is the workhorse: it transfers differences, preserves metadata, and is scriptable. From the workspace on sshadmin:

$ rsync -av notes.txt sshserver:debian-handbook/ssh/upload/notes-rsync.txt
sending incremental file list
notes.txt

sent 133 bytes  received 35 bytes  336.00 bytes/sec
total size is 26  speedup is 0.15
rsync option Description
-a Archive mode: recurse and preserve modes, times, ownership, and links.
-v Name the files transferred.
-n (--dry-run) Show what would transfer without moving anything - use it before any unfamiliar run.
--delete Remove destination files that no longer exist at the source. Combined with -a this makes a mirror, which destroys as well as copies - always dry-run it first.
-e command The ssh command to use, when it needs arguments beyond the configuration.

The host:path form addresses the remote side relative to the remote home. Compare the two uploads from the server's perspective, on sshserver as john:

$ cmp ~/debian-handbook/ssh/upload/notes-sftp.txt ~/debian-handbook/ssh/upload/notes-rsync.txt && echo identical
identical

cmp prints nothing when files match and exits zero, so the && echo turns silence into the confirmation. For directories, -a with a trailing slash convention (src/ copies contents, src copies the directory itself) and --delete under dry-run first are the daily habits; interrupted transfers resume efficiently because only differences move. Chapter 15 builds backup jobs on exactly these properties, and Chapter 21 covers sharing that is continuous rather than point-in-time.