SSH Connection Sharing

Preface

Sometime it is really handy to use SSH protocol for sharing one SSH connection between more processes. SSH protocol is providing the possibility to open one master SSH connection between SSH Client and SSH Server site and provide a Unix Socket on SSH Client site for next SSH connection to same SSH Server.

It is working like this:
1.) SSH Client will open TCP session to SSH Server
2.) The Authentication and Authorization between SSH Client and SSH Server will be done.
3.) SSH Client will create a local Unix Socket and connect to the SSH Server
4.) In the case that there will be next SSH process on SSH Client site it is possible to use Unix Socket mentioned in point 3. for opening new connection to SSH Server skipping step 1. & 2. .

Master SSH Connection

Description:“ ssh -S <path_to_socket> -M user@SSH_Server ”

In this way you will create a SSH connection between SSH Client and SSH Server. After your connection will be created SSH Client will create Unix Socket that is possible to use for next SSH connection to same server with same user. Mentioned Unix Socket will be active until master SSH connection will be opened or any SSH slave connection will be still active. According to this you can use SSH Escape Characters - Session Control Command to force the closing of the SSH master session.

Example

[user@SSH_Client ~]$ ssh -S ~/.ssh/ssh_ux_socket_ssh_server -M user@SSH_Server          # Open SSH connection and Master UX Socket for SSH
user@ssh_server's password:
Last login: Sat Feb  1 18:02:28 2014 from SSH_Client
[user@SSH_Server ~]$

Slave SSH Connection

Description:” ssh -S

Until we have Unix Socket active on SSH Client site it is possible to connect to SSH Server with using mentioned Unix Socket.

In this way we will skip User Authentication and Authorization on SSH Server site. According to this please do not use it on systems where someone else can access mentioned Unix Socket.

Example

[user@SSH_Client ~]$ ssh -S ~/.ssh/ssh_ux_socket_ssh_server  aaa        # As you can see I have user random string "aaa" but the Master UX Socket
Last login: Sat Feb  1 18:16:07 2014 from ssh_client                    # AS you can see SSH is asking for password / passphrase
[user@SSH_Server ~]$                                                    # We have successful connected to SSH Server

Configure SSH Client use it automatically

It is possible to use this kind of SSH Connection multiplexing automatically any time when you are starting a new SSH Connection.
In the case that we do not have active master SSH Unix Socket for the new SSH connection it will be created.
In the case that we do have already an active master SSH Unix Socket any next SSH connection will use mentioned master SSH Unix Socket.

Where

“ /etc/ssh/ssh_config “                                # To change any option in this file will affect all users of the server
“ ~/.ssh/ssh_config “                                  # To change the option in this file will affect just particular user

What

" ControlMaster auto "                                 # Open the Unix Socket automaticly
" ControlPath ~/.ssh/controlsock-%h-%p-%r "            # Location of the Unix Scocekt (%h=host, %p=poprt, %r=user)
" ControlPersist 30 "                                  # After log out of last sesion keep 30s the session open
Navigation
Print/export
QR Code
QR Code wiki:infrastructure_tools:ssh:ssh-connection-sharing (generated for current page)