Agenda: exercise-ssh-key.txt

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