Streamlining two-factor authentication with ssh¶
Do you tire of responding to Duo pushes every single time you log in to NCAR's HPC resources? While two-factor authentication (2FA) is a hard requirement for user access, there are techniques for "sharing" authentication, allowing multiple ssh or scp processes to reuse a pre-established connection. This technique uses the ControlPath and ControlMaster functionality built in to ssh and requires some straightforward local configuration.
ControlPath and ControlMaster are ssh client options that work together along with an automatically generated socket file, allowing an initial ssh connection to be reused and optionally persist after the initial session has disconnected. In practice this means the first connection to a host will be authenticated normally, but subsequent connections will simply reuse the initial connection without requiring additional authentication.
Local configuration¶
Nothing special is required on the HPC systems to enable this functionality, however you will need to perform some local configuration of your ssh client. In the example below I'm using the default OpenSSH on my Macbook laptop, and all that is required is editing the config file in my ~/.ssh directory:
| ~/.ssh/config | |
|---|---|
The first two sections are simply convenience: they define aliases for the host names casper and derecho to their fully-qualified domain names. The idea then is I can ssh casper without needing to use the full host and domain name.
The final section (host *) contains the specific configuration of interest:
-
ControlPath ~/.ssh/controlpath-%r@%h:%pspecifies the path to a controling file "socket" used for connection sharing. This makes use of some special tokens:%r: username on the remote system,%h: the remote system host name, and%p: the remote port.
This allows for a unique
ControlPathfile to be placed in our~/.ssh/directory for each unique user/host/port combination. -
ControlMaster autoenables "opportunisticsshmultiplexing," meaningsshwill attempt to use an existing established connection if possible, and establish a new one if required. -
ControlPersist 12hspecifies that the controlling connection should remain open in the background (waiting for future client connections) after the initial client connection has been closed.Without this option, closing a controlling
sshsession will abruptly terminate any other active, shared authentication connections. -
The final two lines,
ServerAliveInterval 120sandServerAliveCountMax 5, are useful for maintainingsshconnections.When our client is idle, eventually we will be disconnected from the server. These settings send very minimal traffic at intervals even when we are not actively using
ssh, triggering the server to respond. If the server does not respond afterServerAliveCountMaxsteps, our client will finally give up and disconnect.See here for additional discussion.
Your ~/.ssh/config file supports many, many more options. See here for additional details.
Demonstration¶
To see how these pieces work together, consider the following examples:
Connecting to casper through a new ControlPath & examining the mechanics of the process
Detailed Discussion
- For demonstration purposes, we begin on a quiet client with no existing
ControlPathinstances, as shown in line 1. -
On line 4 we start a new
sshsession tocasperto execute a remote command (uptime). You can see from thencar-two-factor:prompt we are required to two-factor authenticate with Duo, as usual.Also, note that we were able to reference the short host name
caspersince our~/.ssh/confighas this aliased tocasper.hpc.ucar.edu. -
Lines 8-9 show that
sshhas now automatically created the~/.ssh/controlpath-benkirk@casper.hpc.ucar.edu:22socket for us, as specified by theControlPathconfiguration. - Lines 11-12 demonstrate the impact of the
ControlPersistoption. Even though we are not currently usingssh(our connection in step 2 has terminated), we still have ansshprocess active in the background referencing ourControlPathfile. This process keeps the connection active and ready to be reused by other processes, up to theControlPersisttimeout. - In line 14 we repeat the
ssh casper uptimecommand, and no 2FA is required!
The same process applies when adding a new connection to Derecho as seen in the following example.
Additionally connecting to derecho through another ControlPath
Detailed Discussion
- In lines 1-16 we similarly start a new
sshsession toderechoto execute a remote command (uname -a), using the short host name alias. - Lines 19-25 demonstrate that we now have a
ControlPathand persistent connection to Derecho as well. - Lines 27-28 show that subsequent
sshconnections do not require 2FA. Success!!
In the examples above we have used ssh to run a remote command, but the same functionality applies with terminal sessions, and extends to scp and sftp as well.
Happy ssh'ing!!