SSH Configuration (~/.ssh/config) with ControlMaster

ControlMaster allows you to multiplex multiple SSH sessions over a single network connection. The first successful connection establishes a "master" connection, and subsequent connections to the same host can reuse this connection without re-authentication.

How to use ControlMaster:

  • Edit your ~/.ssh/config file: If it doesn't exist, create it in ~/.ssh/. Add or modify the following lines for the host(s) you frequently connect to:

    Host your_remote_host
        HostName your_remote_host_ip_or_hostname
        User your_username
        ControlMaster auto
        ControlPath ~/.ssh/control-%h-%p-%r
        ControlPersist 5m  # Keep the master connection alive for 5 minutes after the last session closes
    
    • Host your_remote_host: A nickname you use for this connection.
    • HostName your_remote_host_ip_or_hostname: The actual IP address or hostname of the remote server.
    • User your_username: Your username on the remote server.
    • ControlMaster auto: Enables the master connection feature. It will try to use an existing master connection and create one if it doesn't exist. auto will also try to establish a connection if it doesn't exist. You can also use yes to always try to create a master connection.
    • ControlPath ~/.ssh/control-%h-%p-%r: Specifies the path to a Unix domain socket used for multiplexing. The placeholders %h (hostname), %p (port), and %r (remote user) ensure unique paths for different connections.
    • ControlPersist 5m: Keeps the master connection alive in the background for the specified time (e.g., 5 minutes) after the last client connection closes. This allows you to quickly reconnect without re-authenticating within this time window. You can adjust the duration (e.g., 1h for one hour) or set it to 0 to close the master connection immediately after the last client disconnects.
  • Connect for the first time: The first time you ssh your_remote_host, you will be prompted for your passphrase (if your key isn't already in the SSH agent).

  • Subsequent connections: Subsequent ssh your_remote_host commands in other terminal windows will reuse the existing master connection and will not ask for your passphrase again (as long as the master connection is still alive).

Comments