Skip to content

Commit 1c7f686

Browse files
committed
generate SSH keys & use agent as a group
1 parent a1db11d commit 1c7f686

File tree

2 files changed

+237
-195
lines changed

2 files changed

+237
-195
lines changed

_episodes/12-cluster.md

Lines changed: 237 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -86,46 +86,233 @@ something like `ssh userName@hostname`, where the argument is just like an
8686
email address: the "@" symbol is used to separate the personal ID from the
8787
address of the shared resource.
8888

89-
> ## Setup Revisited
89+
When logging in to a laptop, tablet, or other personal device, a username,
90+
password, or pattern are normally required to prevent unauthorized access. In
91+
these situations, the likelihood of somebody else intercepting your password is
92+
low, since logging your keystrokes requires a malicious exploit or physical
93+
access. For systems like {{ site.remote.host }} running an SSH server, anybody
94+
on the network can log in, or try to. Since usernames are often public or easy
95+
to guess, your password is often the weakest link in the security chain. Many
96+
clusters therefore forbid password-based login, requiring instead that you
97+
generate and configure a public-private key pair with a much stronger password.
98+
Even if your cluster does not require it, the next section will guide you
99+
through the use of SSH keys and an SSH agent to both strengthen your security
100+
*and* make it more convenient to log in to remote systems.
101+
102+
### Better Security With SSH Keys
103+
104+
The [Lesson Setup]({{ page.root }}/setup) provides instructions for
105+
installing a [shell application][setup-shell] with [SSH][setup-ssh].
106+
If you have not done so already, please open that shell application with a
107+
Unix-like command line interface to your system.
108+
109+
SSH keys are an alternative method for authentication to obtain access to
110+
remote computing systems. They can also be used for authentication when
111+
transferring files or for accessing version control systems. In this section
112+
you will create a pair of SSH keys:
113+
114+
* a private key which you keep on your own computer, and
115+
* a public key which is placed on any remote system you will access.
116+
117+
> ## Private keys are your secure digital passport
90118
>
91-
> The [Lesson Setup]({{ page.root }}/setup) provides instructions for
92-
> installing a [shell application][setup-shell] with [SSH][setup-ssh],
93-
> generating your [public-private key pair][setup-keys], and
94-
> installing the [SSH agent][setup-agent].
119+
> A private key that is visible to anyone but you should be considered
120+
> compromised, and must be destroyed. This includes having improper permissions
121+
> on the directory it (or a copy) is stored in, traversing any network that is
122+
> not secure (encrypted), attachment on unencrypted email, and even displaying
123+
> the key on your terminal window.
95124
>
96-
> If you have not completed Setup already, please do the following;
97-
> skip any steps you have already completed:
98-
>
99-
> 1. Open your terminal application.
100-
> 2. Generate a public-private key pair using the 255-bit EdDSA
101-
> algorithm and 100 rounds of hashing:
102-
> ```
103-
> {{ site.local.prompt }} ssh-keygen -t ed25519 -a 100
104-
> # accept default location; enter a strong password
105-
> ```
106-
> {: .language-bash}
107-
> 3. Check the SSH Agent by attempting to list the keys it knows about:
108-
> ```
109-
> {{ site.local.prompt }} ssh-add -l
110-
> ```
111-
> {: .language-bash}
112-
> 1. *Iff* this returns an error because the agent cannot be
113-
> found, launch the SSH Agent as a background process:
114-
> ```
115-
> {{ site.local.prompt }} eval $(ssh-agent)
116-
> ```
117-
> {: .language-bash}
118-
> 4. Register your private key (assuming it resides at `~/.ssh/id_ed25519`)
119-
> with the agent:
120-
> ```
121-
> {{ site.local.prompt }} ssh-add -t 8h ~/.ssh/id_ed25519
122-
> ```
123-
> {: .language-bash}
125+
> Protect this key as if it unlocks your front door. In many ways, it does.
126+
{: .caution}
127+
128+
#### SSH Keys on Linux, Mac, MobaXterm, and Windows Subsystem for Linux
129+
130+
Once you have opened a terminal, check for existing SSH keys and filenames
131+
since existing SSH keys are overwritten.
132+
133+
```
134+
{{ site.local.prompt }} ls ~/.ssh/
135+
```
136+
{: .language-bash}
137+
138+
If `~/.ssh/id_ed25519` already exists, you will need to specify
139+
choose a different name for the new key-pair.
140+
141+
Generate a new public-private key pair using the following command, which will
142+
produce a stronger key than the `ssh-keygen` default by invoking these flags:
143+
144+
* `-a` (default is 16): number of rounds of passphrase derivation; increase to
145+
slow down brute force attacks.
146+
* `-t` (default is [rsa][wiki-rsa]): specify the "type" or cryptographic
147+
algorithm. `ed25519` specifies [EdDSA][wiki-dsa] with a 256-bit key;
148+
it is faster than RSA with a comparable strength.
149+
* `-f` (default is /home/user/.ssh/id_algorithm): filename to store your
150+
private key. The public key will be identical, with a `.pub` extension added.
151+
152+
```
153+
{{ site.local.prompt }} ssh-keygen -a 100 -f ~/.ssh/id_ed25519 -t ed25519
154+
```
155+
{: .language-bash}
156+
157+
When prompted, enter a strong password that you will remember. There are two
158+
common approaches to this:
159+
160+
1. Create a memorable passphrase with some punctuation and number-for-letter
161+
substitutions, 32 characters or longer. Street addresses work well; just be
162+
careful of social engineering or public records attacks.
163+
2. Use a password manager and its built-in password generator with all
164+
character classes, 25 characters or longer. KeePass and BitWarden are two
165+
good options.
166+
167+
Note that the terminal will not appear to change while you type the password:
168+
this is deliberate, for your security. You will be prompted to type it again,
169+
so don't worry too much about typos.
170+
171+
Take a look in `~/.ssh` (use `ls ~/.ssh`). You should see two new files:
172+
173+
* your private key (`~/.ssh/id_ed25519`): *do not share with anyone!*
174+
* the shareable public key (`~/.ssh/id_ed25519.pub`): if a system administrator
175+
asks for a key, this is the one to send. It is also safe to upload to
176+
websites such as GitHub: it is meant to be seen.
177+
178+
> ## No Empty Passwords
124179
>
125-
> You can now use your strong SSH key to log in to remote machines and, for the
126-
> next 8 hours, you will not have to type your password to do so: the SSH Agent
127-
> handles it for you.
128-
{: .callout}
180+
> Nothing is *less* secure than a private key with no password. If you skipped
181+
> password entry by accident, go back and generate a new key pair *with* a
182+
> strong password.
183+
{: .error}
184+
185+
##### Use RSA for Older Systems
186+
187+
If key generation failed because ed25519 is not available, try using the older
188+
(but still strong and trustworthy) [RSA][wiki-rsa] cryptosystem. Again, first
189+
check for an existing key:
190+
191+
```
192+
{{ site.local.prompt }} ls ~/.ssh/
193+
```
194+
{: .language-bash}
195+
196+
If `~/.ssh/id_rsa` already exists, you will need to specify choose a different
197+
name for the new key-pair. Generate it as above, with the following extra flags:
198+
199+
* `-b` sets the number of bits in the key. The default is 2048.
200+
EdDSA uses a fixed key length, so this flag would have no effect.
201+
* `-o` (no default): use the OpenSSH key format,
202+
rather than PEM.
203+
204+
```
205+
{{ site.local.prompt }} ssh-keygen -a 100 -b 4096 -f ~/.ssh/id_rsa -o -t rsa
206+
```
207+
{: .language-bash}
208+
209+
When prompted, enter a strong password that you will remember. There are two
210+
common approaches to this:
211+
212+
1. Create a memorable passphrase with some punctuation and number-for-letter
213+
substitutions, 32 characters or longer. Street addresses work well; just be
214+
careful of social engineering or public records attacks.
215+
2. Use a password manager and its built-in password generator with all
216+
character classes, 25 characters or longer. KeePass and BitWarden are two
217+
good options.
218+
219+
Take a look in `~/.ssh` (use `ls ~/.ssh`). You should see two new files:
220+
221+
* your private key (`~/.ssh/id_ed25519`): *do not share with anyone!*
222+
* the shareable public key (`~/.ssh/id_ed25519.pub`): if a system administrator
223+
asks for a key, this is the one to send. It is also safe to upload to
224+
websites such as GitHub: it is meant to be seen.
225+
226+
#### SSH Keys on PuTTY
227+
228+
If you are using PuTTY on Windows, download and use `puttygen` to generate the
229+
key pair. See the [PuTTY documentation][putty-gen] for details.
230+
231+
* Select `EdDSA` as the key type.
232+
* Select `255` as the key size or strength.
233+
* Click on the "Generate" button.
234+
* You do not need to enter a comment.
235+
* When prompted, enter a strong password that you will remember. There are two
236+
common approaches to this:
237+
238+
1. Create a memorable passphrase with some punctuation and number-for-letter
239+
substitutions, 32 characters or longer. Street addresses work well; just be
240+
careful of social engineering or public records attacks.
241+
2. Use a password manager and its built-in password generator with all
242+
character classes, 25 characters or longer. KeePass and BitWarden are two
243+
good options.
244+
245+
* Save the keys in a folder no other users of the system can read.
246+
247+
Take a look in the folder you specified. You should see two new files:
248+
249+
* your private key (`id_ed25519`): *do not share with anyone!*
250+
* the shareable public key (`id_ed25519.pub`): if a system administrator
251+
asks for a key, this is the one to send. It is also safe to upload to
252+
websites such as GitHub: it is meant to be seen.
253+
254+
## SSH Agent for Easier Key Handling
255+
256+
An SSH key is only as strong as the password used to unlock it, but on the
257+
other hand, typing out a complex password every time you connect to a machine
258+
is tedious and gets old very fast. This is where the [SSH Agent][ssh-agent]
259+
comes in.
260+
261+
Using an SSH Agent, you can type your password for the private key once, then
262+
have the Agent remember for some number of hours or until you log off. Unless
263+
some nefarious actor has physical access to your machine, this keeps the
264+
password safe, and removes the tedium of entering the password multiple times.
265+
266+
Just remember your password, because once it expires in the Agent, you have to
267+
type it in again.
268+
269+
### SSH Agents on Linux, macOS, and Windows
270+
271+
Open your terminal application and check if an agent is running:
272+
273+
```
274+
{{ site.local.prompt }} ssh-add -l
275+
```
276+
{: .language-bash}
277+
278+
* If you get an error like this one,
279+
280+
```
281+
Error connecting to agent: No such file or directory
282+
```
283+
{: .error}
284+
285+
... then you need to launch the agent *as a background process*.
286+
287+
```
288+
{{ site.local.prompt }} eval $(ssh-agent)
289+
```
290+
{: .language-bash}
291+
292+
* Otherwise, your agent is already running: don't mess with it.
293+
294+
Add your key to the agent, with session expiration after 8 hours:
295+
296+
```
297+
{{ site.local.prompt }} ssh-add -t 8h ~/.ssh/id_ed25519
298+
```
299+
{: .language-bash}
300+
```
301+
Enter passphrase for .ssh/id_ed25519:
302+
Identity added: .ssh/id_ed25519
303+
Lifetime set to 86400 seconds
304+
```
305+
{: .output}
306+
307+
For the duration (8 hours), whenever you use that key, the SSH Agent will
308+
provide the key on your behalf without you having to type a single keystroke.
309+
310+
### SSH Agent on PuTTY
311+
312+
If you are using PuTTY on Windows, download and use `pageant` as the SSH agent.
313+
See the [PuTTY documentation][putty-agent].
314+
315+
### Log in
129316

130317
Go ahead and open your terminal or graphical SSH client, then log in to the
131318
cluster using your username and the remote computer you can reach from the
@@ -144,13 +331,13 @@ Normal output will resume once you press `Enter`.
144331
You may have noticed that the prompt changed when you logged into the remote
145332
system using the terminal (if you logged in using PuTTY this will not apply
146333
because it does not offer a local terminal). This change is important because
147-
it can help you distinguish on which system the commands you type will be run when you
148-
pass them into the terminal. This change is also a small complication that we
149-
will need to navigate throughout the workshop. Exactly what is reported before
150-
the `$` in the terminal when it is connected to the local system and the remote
151-
system will typically be different for every user. We still need to indicate
152-
which system we are entering commands on though so we will adopt the following
153-
convention:
334+
it can help you distinguish on which system the commands you type will be run
335+
when you pass them into the terminal. This change is also a small complication
336+
that we will need to navigate throughout the workshop. Exactly what is reported
337+
before the `$` in the terminal when it is connected to the local system and the
338+
remote system will typically be different for every user. We still need to
339+
indicate which system we are entering commands on though so we will adopt the
340+
following convention:
154341

155342
- `{{ site.local.prompt }}` when the command is to be entered on a terminal
156343
connected to your local computer
@@ -476,7 +663,9 @@ scheduler, and use it to start running our scripts and programs!
476663
{% include links.md %}
477664
478665
[fshs]: https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard
479-
[setup-shell]: {{ page.root }}/setup#where-to-type-commands-how-to-open-a-new-shell
480-
[setup-ssh]: {{ page.root }}/setup#ssh-for-secure-connections
481-
[setup-keys]: {{ page.root }}/setup#public-private-key-pair-for-ssh
482-
[setup-agent]: {{ page.root }}/setup#ssh-agent-for-easier-key-handling
666+
[putty-gen]: https://tartarus.org/~simon/putty-prerel-snapshots/htmldoc/Chapter8.html#pubkey-puttygen
667+
[putty-agent]: https://tartarus.org/~simon/putty-prerel-snapshots/htmldoc/Chapter9.html#pageant
668+
[ssh-agent]: https://www.ssh.com/academy/ssh/agent
669+
[ssh-flags]: https://stribika.github.io/2015/01/04/secure-secure-shell.html
670+
[wiki-rsa]: https://en.wikipedia.org/wiki/RSA_(cryptosystem)
671+
[wiki-dsa]: https://en.wikipedia.org/wiki/EdDSA

0 commit comments

Comments
 (0)