Ssh – How to make SSH’s `ControlPath` distinguish between IPv4 and IPv6

opensshssh

I'm using

ControlMaster   auto
ControlPath     ~/.ssh/tmp/%l_%r@%h:%p

in my ~/.ssh/config to open multiple sessions using the same connection when accessing a host via SSH.

That works fine for most use cases, but doesn't allow me to connect to the same hostname via IPv4 and IPv6 at the same time; all additional connections use the control socket created by the first connection because the ControlPath doesn't allow to distinguish between IPv4 and IPv6 connections.

Is there a way to have separate control sockets for IPv4 and IPv6 connections (perhaps by finding a way to use the remote IP rather then the remote hostname in the socket path)?


EDIT #1

I just remembered the Match option available in ssh_config and sshd_config files and tried:

Match AddressFamily inet
    ControlPath     ~/.ssh/tmp/%l_%r@%h:%p.inet

Match AddressFamily inet6
    ControlPath     ~/.ssh/tmp/%l_%r@%h:%p.inet6

Unfortunately, that fails with "Unsupported Match attribute AddressFamily" when I try to connect to any host, so I'm back to square one…

Best Answer

This is not typical use case so there is no direct way of doing so. But, you can workaround this using user configuration file as such, if you don't require too many remote hosts:

Host hostname-4
    Hostname hostname
    AddressFamily inet
    ControlPath ~/.ssh/master4-%l%h%p%r
Host hostname-6
    Hostname hostname
    AddressFamily inet6
    ControlPath ~/.ssh/master6-%l%h%p%r
Related Question