Track1Agenda: a_exercise_ssh_key.html

File a_exercise_ssh_key.html, 12.5 KB (added by jon, 6 years ago)
Line 
1<h1>SSH with private/public key authentication</h1>
2
3<p>In this exercise we'll show how you can eliminate passwords by using ssh key
4authentication.</p>
5
6<p>Choose the version of the exercises depending on what OS you are running on
7your laptop.</p>
8
9<p>Remember: the <code>$</code> character before commands indicates that they are to be
10run as your normal login user, not as root.</p>
11
12<h1>For laptops running Windows</h1>
13
14<p>Download the following onto your desktop or into a downloads folder:</p>
15
16<ul>
17<li>putty.exe (you should already have this)</li>
18<li>psftp.exe</li>
19<li>pageant.exe</li>
20<li>puttygen.exe</li>
21</ul>
22
23<p>from <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html">http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html</a> 
24(Or you can try the installer bundle which gets them all)</p>
25
26<p>Alternately you can use <a href="http://noc.ws.nsrc.org/downloads/">http://noc.ws.nsrc.org/downloads/</a> to download these files if the
27public download site is not available or too slow.</p>
28
29
30<h2>1. Generate an ssh public/private key pair</h2>
31
32<p>Double-click on <code>puttygen.exe</code></p>
33
34<p>At the bottom of the dialog box, under "Parameters":</p>
35
36<ul>
37<li>Make sure the type of key to generate is "SSH-2 RSA"</li>
38<li>Set the number of bits to 2048</li>
39</ul>
40
41<p>Click on "Generate". Move the mouse randomly over the blank area until the
42progress bar reaches 100%</p>
43
44<pre><code>Key comment:        [Your Name &lt;your@email.address&gt;   ]
45Key passphrase:     [chooose a passphrase             ]
46Confirm passphrase: [choose same passphrase           ]
47</code></pre>
48
49<p>The passphrase is used to keep your private key encrypted on disk. It can be
50pretty much anything you want and as long as you want - including spaces -
51but if you forget it, your key becomes worthless.  For now pick something
52that you will easily remember.  You can change it at any time you want in
53the future.</p>
54
55<p>Click "Save public key". Give a filename of "id_rsa.pub" (please save
56files into the same directory as where the executables are)</p>
57
58<p>Click "Save private key". Give a filename of "id_rsa.ppk"</p>
59
60<p>Use the mouse to highlight all the text in the box "Public key for pasting
61into OpenSSH authorized_keys file", and copy it to the clipboard.</p>
62
63<p>Exit puttygen.</p>
64
65<p>NOTE: Key generation is a one-off exercise. The more you deploy your public
66key, the more work it to be if you were to lose it and have to start again
67with a new one.  I suggest you keep a secure backup of it somewhere, e.g.
68on a CD-ROM or a safe USB key that you lock away.</p>
69
70<h2>2. Copy the PUBLIC key onto your Unix server</h2>
71
72<p>You have two ways of doing this.</p>
73
74<h3>Copy-paste</h3>
75
76<p>Use putty.exe to make a normal ssh connection to your host as the 'sysadm'
77user.</p>
78
79<pre><code>$ cat &gt;&gt;.ssh/authorized_keys
80*** PASTE KEY FROM CLIPBOARD ***
81*** If the cursor is still at the end of the line, hit Enter ***
82*** hit ctrl-D ***
83</code></pre>
84
85<p>The key consists of one very long line, which looks like</p>
86
87<pre><code>ssh-rsa &lt;lots of base64 data&gt; &lt;comment&gt;
88</code></pre>
89
90<p>As a quick check that it hasn't been corrupted, count the lines in the file:</p>
91
92<pre><code>$ wc -l .ssh/authorized_keys
931 .ssh/authorized_keys
94</code></pre>
95
96<p>If you don't see "1", then you'll need to fix it (possibly with an editor,
97or else just rm the file and start again)</p>
98
99<p>Now logout.</p>
100
101<h3>Alternative way (if you're having problems with copy-paste)</h3>
102
103<p>Double-click on psftp.exe. Open a connection to your server, and upload
104your public key:</p>
105
106<pre><code>psftp&gt; open pcN.ws.nsrc.org
107login as: sysadm
108sysadm@pcN.ws.nsrc.org's password: &lt;usual one&gt;
109Remote working directory is /home/sysadm
110psftp&gt; put id_rsa.pub
111local:id_rsa.pub =&gt; remote:/home/sysadm/id_rsa.pub
112psftp&gt; quit
113</code></pre>
114
115<p>Unfortunately, this public key is not in the format which openssh requires,
116so now login again using putty.exe, and use the following command to convert
117it and put it in the right place.</p>
118
119<pre><code>$ ssh-keygen -i -f id_rsa.pub &gt;&gt;.ssh/authorized_keys
120</code></pre>
121
122<h2>3. Login using your private key</h2>
123
124<p>Start putty.exe again. Enter the hostname as usual, but before clicking
125Open, browse in the left hand column to Connection > SSH > Auth</p>
126
127<pre><code>[-] Connection
128     |
129    [-] SSH
130     |  |- Keyex
131     |  |- Auth    &lt;--- CLICK HERE
132</code></pre>
133
134<p>Next to "Private key for authentication", click Browse. Find your
135id_rsa.ppk file, open it, then click Open to start the connection.</p>
136
137<p>You should be prompted for your username as before (sysadm), but then
138instead of being prompted for a password, you are asked for the passphrase
139for your private key. Enter it, and you should be logged in.</p>
140
141<p>This is quite painful (both locating the private key and entering the
142passphrase), so as the final step of the exercise we're going automate it
143using an agent.</p>
144
145<h2>4. Use a passphrase agent</h2>
146
147<p>Run <code>pageant.exe</code></p>
148
149<p>It runs in the background, and adds an icon to your task tray (a PC with
150a black hat at a jaunty angle). You may need to select "Show hidden icons"
151to see it.</p>
152
153<p>Right-click on the icon, and select "Add Key". Browse to your id_rsa.ppk
154and open it. You will be prompted for the passphrase - enter it. (If you
155make a mistake, you'll be prompted again until you get it right)</p>
156
157<p>Now run putty.exe again, enter your hostname, click Open, and enter your
158username (sysadm).  You should be logged in immediately, with no prompt for
159either a password or a passphrase!</p>
160
161<p>Try logging in again. Also try using psftp.exe (when it runs, enter
162"open pcN.ws.nsrc.org" to start a connection). No passphrase is needed
163until you tell Pageant to forget the private key.</p>
164
165<h1>For laptops running Linux (or BSD or OSX)</h1>
166
167<h2>1. Generate an ssh public/private key pair</h2>
168
169<pre><code>$ ssh-keygen -t rsa -b 2048
170Generating public/private rsa key pair.
171Enter file in which to save the key (/home/sysadm/.ssh/id_rsa): &lt;HIT ENTER&gt;
172Created directory '/home/sysadm/.ssh'.
173Enter passphrase (empty for no passphrase): &lt;CHOOSE PASSPHRASE&gt;
174Enter same passphrase again: &lt;SAME PASSPHRASE&gt;
175Your identification has been saved in /home/sysadm/.ssh/id_rsa.
176Your public key has been saved in /home/sysadm/.ssh/id_rsa.pub.
177The key fingerprint is:
17832:2b:e3:0e:14:fb:60:38:a6:e2:73:95:53:9d:a8:0f sysadm@pcN.ws.nsrc.org
179</code></pre>
180
181<p>The passphrase is used to keep your private key encrypted on disk. It can be
182pretty much anything you want and as long as you want - including spaces -
183but if you forget it, your key becomes worthless.  For now pick something
184that you will easily remember.  You can change it at any time you want in
185the future (using <code>ssh-keygen -p</code>)</p>
186
187<p>NOTE: Key generation is a one-off exercise. The more you deploy your public
188key, the more work it to be if you were to lose it and have to start again
189with a new one.  I suggest you keep a secure backup of it somewhere, e.g.
190on a CD-ROM that you lock away.</p>
191
192<h2>2. Copy the PUBLIC key onto your Unix server</h2>
193
194<p>The simplest way to copy the public key is with scp:</p>
195
196<pre><code>$ scp .ssh/id_rsa.pub sysadm@pcN.ws.nsrc.org:.ssh/authorized_keys
197</code></pre>
198
199<p>Note that .ssh/authorized_keys can contain multiple keys, one per line,
200so on a shared system you might want to append your key instead:</p>
201
202<pre><code>$ cat .ssh/id_rsa.pub | ssh sysadm@pcN.ws.nsrc.org 'cat &gt;&gt;.ssh/authorized_keys'
203</code></pre>
204
205<h2>3. Login using your private key</h2>
206
207<p>Open an ssh connection to your server as normal:</p>
208
209<pre><code>$ ssh sysadm@pcN.ws.nsrc.org
210</code></pre>
211
212<p>This time, instead of being prompted for your password, you should be
213prompted for the passphrase on your private key. Enter it. You should be
214logged in.</p>
215
216<h2>4. Use a passphrase agent</h2>
217
218<p>Entering a passphrase every time you connect would be painful, but this
219isn't necessary if you have an agent which decrypts the private key and
220keeps it in memory.</p>
221
222<p>If you are running under a modern graphical environment like Gnome, you
223probably already got a dialog box prompting you for a passphrase, and this
224means you're already running an agent.  You should be able to logout and
225login to the remote server, without being prompted for your passphrase
226again.</p>
227
228<p>To see what identities (decrypted private keys) your agent has in memory:</p>
229
230<pre><code>$ ssh-add -l
231</code></pre>
232
233<p>To forget all identities:</p>
234
235<pre><code>$ ssh-add -d
236</code></pre>
237
238<p>If you don't have an agent, then you can start a new subshell with ssh-agent
239as its parent:</p>
240
241<pre><code>$ ssh-agent bash
242$ ssh-add
243... prompted for your passphrase
244$
245</code></pre>
246
247<p>Now the agent will handle future connections for you.</p>
248
249<p>If you are running an older graphical environment, and you normally start
250X using <code>startx</code>, then start it using <code>ssh-agent startx</code> instead. Then type
251'ssh-add' in an xterm.</p>
252
253<hr />
254
255<h1>Disable Password Access to your Machine</h1>
256
257<h2>Connect Only With SSH Keys</h2>
258
259<p>Only do this exercise if you have successfully copied your public key to your machine and you
260are being prompted for your ssh private key passphrase when you log in and not your password.</p>
261
262<p>Log in on your machine. Once logged in become the root user:</p>
263
264<pre><code>$ sudo bash</pre></code>
265
266<p>As the root user copy authorized_keys file to the directory /root/.ssh.</p>
267
268<pre><code># mkdir /root/.ssh
269# chmod 700 /root/.ssh
270# cp /home/sysadm/.ssh/authorized_keys /root/.ssh/.
271# chown root:root /root/.ssh/authorized_keys
272# chmod 644 /root/.ssh/authorized_keys
273</pre></code>
274
275<p>Now log out of your machine and try to log back in, but this time as the "root" user,
276not as the "sysadm" user. If you are prompted for your ssh private key passphrase and not a
277password, then you are ready to disable password access to your machine.</p>
278
279<p>Log in on your machine as the root user. Now we are going to edit the file /etc/ssh/sshd_config.</p>
280
281<pre><code># vi /etc/ssh/sshd_config</pre></code>
282
283<p>Insie the file look for the following line:</p>
284
285<code>#PasswordAuthentication yes</code>
286
287<p>Just after this line add a line that says:</p>
288
289<code>PasswordAuthentication no</code>
290
291<p>Save and exit from the file. The reload the ssh server:</p>
292
293<pre><code># service ssh reload</pre></code>
294
295<p>Before logging out we recommend you leave your current session open in case there are problems. You
296could lock yourself out of your machine. If you do let your instructor know. Now use ssh or putty on your laptop
297and try connecting to your server. You should get prompted for your ssh private key's passphrase and
298be able to log in. If you did, everthing is working. At this point all new users on your machine must
299use ssh keys to connect and not passwords. If you wish to verify this you can do the following:</p>
300
301<pre><code># adduser testuser</pre></code>
302
303<p>Answer the on-screen prompts. Once the user has been created and you have given it a new password of
304your choosing, try to open a new ssh session to your machine as that user. You should either be rejected
305or your password attempts will have not affect (depends on the ssh server version).</p>
306
307<p>To log in as this user you would need to copy your public ssh key to their account as we did for
308your sysadm user. This <b>greatly</b> enhances the security of your system.
309
310
311<hr />
312
313<h1>Additional information [not part of exercises]</h1>
314
315<h2>Agent forwarding</h2>
316
317<p>Using an agent, you can access across multiple ssh hops without having to
318copy your key or enter your passphrase anywhere.</p>
319
320<p>If you enable "agent forwarding" when you login to host X, you can then
321login from X to Y without any prompting (assuming Y has your public key in
322authorized_keys).  The request to authenticate is forwarded securely back
323along your original ssh session to the agent running on your workstation.</p>
324
325<p>Under Unix:</p>
326
327<pre><code>$ ssh -o ForwardAgent=yes user@host
328</code></pre>
329
330<p>If you do this frequently, it's easier to configure it in <code>.ssh/hosts</code></p>
331
332<pre><code>host foo
333hostname foo.example.com
334user sysadm
335ForwardAgent yes
336</code></pre>
337
338<p>Then you only have to type <code>ssh foo</code> to get a connection with those options.</p>
339
340<h2>Advanced: X11 forwarding and port forwarding</h2>
341
342<p>ssh can securely carry arbitrary forwarded TCP connections and X11 graphics.</p>
343
344<pre><code>$ ssh -L8080:some.where:80 user@remote.host
345... while ssh connection is open, a connection to 127.0.0.1 port 8080
346... will be tunneled, and the far end will open a connection to
347... some.where port 80
348
349$ ssh -X user@remote.host
350...
351$ xclock   # graphical output redirected back through ssh tunnel
352</code></pre>