In my previous post on SSH multiplexing, I gave the following to
add to your ~/.ssh/config
file without explaining what it actually means:
Host * ControlMaster auto ControlPath ~/.ssh/connections/%r_%h_%p
The documentation for ~/.ssh/config
can be found at
ssh_config(5)
. Four options are relevant to this post:
Host
- The
config
file is broken up in to sections based on which hosts the configuration options apply to.Host *
means these options apply to all connections. If you wanted the options to apply only when connecting toexample.com
, you could change that line toHost example.com
. - Actually, you can also limit configuration options by things other
than just the host using the
Match
directive. For example, this configuration has options for connecting with the usernamegit
, presumably due to having multiplegit
servers that use that username. ControlMaster
- Tells
ssh
to use multiplexing. Specifically, the default isno
, which means it will look for an already open master connection. To actually open a master connection,yes
orask
can be used, the latter means that a password prompt will appear when connecting to that master connection. The more useful options for a config file are theauto
andautoask
options which will use an already open connection if exists, but fall back to acting likeyes
andask
respectively otherwise. ControlPath
- In order to connect to the master connection,
ssh
needs a way to communicate with it. This is handled by the master creating aUnix socket
which futuressh
instances look for. Unix sockets are an IPC mechanism which allows two processes on the same machine to communicate via a connection initiated by one process creating a socket identified by a filename and another using that special file to connect. In comparison with TCP, every server needs its own port number that the client needs to know and any client can connect as long as it knows the port number. - Unix sockets are identified by filenames and
ControlPath
specifies the filename to use for the socket The%r
,%h
,%p
parts mean the filename should include the remote username, hostname, and port number in order to identify whichssh
session is which. - This should usually be enough, but if your home
directory is shared among multiple computers, as is common in some
university and other large organization setups, then you will also
need
%l
to identify which host you are connecting from. Otherwisessh
may get confused by master connections created by a different host. Luckily,ssh
provides a shortcut, which is the%C
option which is a hash of all 4 (although it is not available on older versions ofssh
):ControlPath ~/.ssh/connections/%C
-
or, if you are a disto which does not have
%C
yet like the latest Ubuntu LTS:ControlPath ~/.ssh/connections/%L_%h_%p_%r
-
I used
%L
for the short version of the local hostname (for example, if%l
isfoo.example.com
,%L
would be justfoo
) because when I used%l
, my system complained the filename was too long. -
Keep in mind that the socket file is security critical because it is used to piggyback on your existing
ssh
sessions without authenticating (unless you use theask
orautoask
options forControlMaster
), so make sure your~/.ssh/connections/
directory is readable only by you:chmod 700 ~/.ssh/connections
ControlPersist
- Not used above, the
ControlPersist
option lets you control when the master connection actually closed. When set tono
, it closes with the initial connect. When set toyes
it stays open until explicitly closed withssh -O exit
. It can also be set to a length of time to stay open after the last connection is closed. - While the default of
ControlPersist
is not clearly stated in the documentation, I checked the source code to confirm it does default tono
: the default value is set to 0 here if it is still set to its initial value of -1, which is the same value it is given if the configuration file says
.no