How to use the ControlMaster feature of OpenSSH.
The ControlMaster feature of OpenSSH is a powerful tool that can significantly improve the efficiency of your SSH sessions. It allows you to reuse an existing SSH connection for multiple subsequent SSH connections to the same host, reducing the time it takes to establish new SSH connections and improving the overall performance of your SSH sessions.
To use the ControlMaster feature, you need to add a few lines to your SSH configuration file (~/.ssh/config). If you don't have a configuration file already, you can create one using the following command:
Once you have a configuration file, you need to add the following lines:
Host * ControlMaster auto ControlPath ~/.ssh/controlmasters/%r@%h:%p ControlPersist 600
These lines configure OpenSSH to use ControlMaster for all SSH connections, and to store the control sockets in the ~/.ssh/controlmasters directory.
Initiating the first connection
Once you've configured ControlMaster, you can initiate the first SSH connection as you normally would:
If this is the first time you've connected to the server, ControlMaster will automatically create a new SSH connection. If you've connected to the server before, ControlMaster will reuse the existing connection.
Reusing the existing connection
To reuse the existing SSH connection, you can simply run the same ssh command again:
This will use the existing SSH connection instead of establishing a new one, which should be much faster.
Terminating the connection
When you're finished with your SSH session, you can terminate the connection using the usual SSH command:
This will terminate the SSH connection and the ControlMaster process.
Using ControlMaster with scp and sftp
ControlMaster can also be used with the scp and sftp commands, by specifying the -o ControlMaster=auto option:
scp -o ControlMaster=auto myfile.txt email@example.com:/path/to/destination/
This will reuse the existing SSH connection instead of establishing a new one.
Using ControlMaster with Ansible
ControlMaster can be used with Ansible to improve the performance of your playbook runs. To enable ControlMaster for Ansible, you need to add the following lines to your ansible.cfg file:
[ssh_connection] ssh_args = -o ControlMaster=auto -o ControlPersist=600
These lines configure Ansible to use ControlMaster for SSH connections, and to store the control sockets for up to 600 seconds.
Once you've added these lines, you can run your Ansible playbooks as usual, and Ansible will automatically use ControlMaster to reuse existing SSH connections, improving the performance of your playbook runs.
The ControlMaster feature of OpenSSH is a powerful tool that can significantly improve the efficiency of your SSH sessions. By reusing existing SSH connections, you can reduce the time it takes to establish new connections and improve the overall performance of your SSH sessions. With a few simple configuration changes, you can start using ControlMaster today and enjoy faster, more efficient SSH sessions and Ansible playbook runs.