|
| 1 | +# SSH key-based auth |
| 2 | + |
| 3 | +Instruments run an SSH server, which can be used to execute commands remotely. |
| 4 | + |
| 5 | +It is possible to access this SSH server using key-based authentication. Keys are associated with |
| 6 | +an individual, but are used to grant access to the instrument accounts. This means that keys |
| 7 | +for individuals no longer on the team can be easily revoked. |
| 8 | + |
| 9 | +## Key-pair generation |
| 10 | + |
| 11 | +:::{note} |
| 12 | +If you already have a suitable SSH key, which is encrypted using a passphrase, you may |
| 13 | +skip this step. |
| 14 | +::: |
| 15 | + |
| 16 | +Generate a key-pair using a strong algorithm, for example `ed25519`: |
| 17 | +``` |
| 18 | +ssh-keygen -t ed25519 |
| 19 | +``` |
| 20 | +**You must encrypt this key with a strong password when prompted.** |
| 21 | +Don't use an empty passphrase for these keys. This is not a shared |
| 22 | +password, it is a password for your personal key-pair; store it in your password |
| 23 | +manager. This will generate two files: `~\.ssh\id_ed25519` and `~\.ssh\id_ed25519.pub`. The file |
| 24 | +ending in `.pub` is a public key, the one without the `.pub` extension is a private key. It |
| 25 | +would be sensible to store copies of these two files in your password manager too. |
| 26 | + |
| 27 | +For the avoidance of doubt, the **public** key (`*.pub`) can be freely shared with everyone (for |
| 28 | +example, by being copied onto instruments). Do not share your **private** key. The private key |
| 29 | +is additionally encrypted using your selected password. |
| 30 | + |
| 31 | +## Setting up SSH agent |
| 32 | + |
| 33 | +In a powershell window, run the following commands: |
| 34 | +```powershell |
| 35 | +Get-Service ssh-agent | Set-Service -StartupType Automatic |
| 36 | +Start-Service ssh-agent |
| 37 | +``` |
| 38 | + |
| 39 | +## Deploying the public key |
| 40 | + |
| 41 | +:::{important} |
| 42 | +TODO: write helper script/process to get public keys onto every inst. |
| 43 | +::: |
| 44 | + |
| 45 | +For each instrument, append your **public** key (the one from the file ending `.pub`) to the |
| 46 | +following locations on each instrument: |
| 47 | +``` |
| 48 | +c:\Users\spudulike\.ssh\authorized_keys |
| 49 | +c:\ProgramData\ssh\administrators_authorized_keys |
| 50 | +``` |
| 51 | + |
| 52 | +The `administrators_authorized_keys` file must be set up with correct permissions. If the file |
| 53 | +was originally created by an administrator they should already be correct, otherwise run: |
| 54 | + |
| 55 | +``` |
| 56 | +icacls.exe "c:\ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F" |
| 57 | +``` |
| 58 | + |
| 59 | +## One-off usage |
| 60 | + |
| 61 | +To connect via SSH to an instrument, use: |
| 62 | + |
| 63 | +``` |
| 64 | +ssh spudulike@NDXINST |
| 65 | +``` |
| 66 | + |
| 67 | +This will prompt you on each connection for the passphrase to unlock your SSH key, this is the |
| 68 | +password you set earlier for your personal SSH key. You will not be prompted for an |
| 69 | +account password; your key is sufficient to grant you access. |
| 70 | + |
| 71 | +## Bulk usage |
| 72 | + |
| 73 | +Firstly, if you intend to run a command across many instruments, it is worth getting that command |
| 74 | +reviewed by another developer and running it together. This is **especially** true if you intend to |
| 75 | +run a command as a privileged user. |
| 76 | + |
| 77 | +Typing the password to unlock your SSH key for each instrument would be tedious. |
| 78 | +To avoid this, we can **temporarily** add the key to the SSH agent: |
| 79 | + |
| 80 | +``` |
| 81 | +ssh-add |
| 82 | +``` |
| 83 | +This will prompt for the passphrase to unlock your SSH key. You can check that your key is now in |
| 84 | +the SSH agent by running: |
| 85 | + |
| 86 | +``` |
| 87 | +ssh-add -l |
| 88 | +``` |
| 89 | + |
| 90 | +Once the key has been added to the agent, you can SSH to an instrument without any further prompts: |
| 91 | + |
| 92 | +``` |
| 93 | +ssh spudulike@NDXINST |
| 94 | +``` |
| 95 | + |
| 96 | +Commands can be executed like: |
| 97 | + |
| 98 | +``` |
| 99 | +ssh spudulike@NDXINST "dir /?" |
| 100 | +``` |
| 101 | + |
| 102 | +Since we no longer have any authentication prompts (having added our key to the SSH-agent), |
| 103 | +this command is suitable for automating in a loop over instruments - for example from python |
| 104 | +or a `.bat` script. |
| 105 | + |
| 106 | +Once you have finished with the administration task which needed SSH across multiple instruments, you |
| 107 | +should remove your key from the agent (and then verify that it has been removed): |
| 108 | + |
| 109 | +``` |
| 110 | +ssh-add -D |
| 111 | +ssh-add -l |
| 112 | +``` |
| 113 | + |
| 114 | +:::{important} |
| 115 | +Do not leave these keys permanently added to the SSH agent - having *immediate* SSH access to *every* |
| 116 | +instrument is an unnecessary risk most of the time (for example if your developer machine was compromised). |
| 117 | +Add the keys to the SSH agent only when needed, and remove them from the agent again when your administration |
| 118 | +task is complete. The usual sudo lecture applies: |
| 119 | +> We trust you have received the usual lecture from the local System |
| 120 | +> Administrator. It usually boils down to these three things: |
| 121 | +> 1) Respect the privacy of others. |
| 122 | +> 2) Think before you type. |
| 123 | +> 3) With great power comes great responsibility. |
| 124 | +::: |
0 commit comments