1 | SSH with private/public key authentication |
---|
2 | ========================================== |
---|
3 | |
---|
4 | In this exercise we'll show how you can eliminate passwords by using ssh key |
---|
5 | authentication. |
---|
6 | |
---|
7 | Choose the version of the exercises depending on what OS you are running on |
---|
8 | your laptop. |
---|
9 | |
---|
10 | Remember: the `$` character before commands indicates that they are to be |
---|
11 | run as your normal login user, not as root. |
---|
12 | |
---|
13 | For laptops running Windows |
---|
14 | =========================== |
---|
15 | |
---|
16 | Download 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 | |
---|
23 | from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html |
---|
24 | (Or you can try the installer bundle which gets them all) |
---|
25 | |
---|
26 | 1. Generate an ssh public/private key pair |
---|
27 | ------------------------------------------ |
---|
28 | |
---|
29 | Double-click on `puttygen.exe` |
---|
30 | |
---|
31 | At 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 | |
---|
36 | Click on "Generate". Move the mouse randomly over the blank area until the |
---|
37 | progress 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 | |
---|
43 | The passphrase is used to keep your private key encrypted on disk. It can be |
---|
44 | pretty much anything you want and as long as you want - including spaces - |
---|
45 | but if you forget it, your key becomes worthless. For now pick something |
---|
46 | that you will easily remember. You can change it at any time you want in |
---|
47 | the future. |
---|
48 | |
---|
49 | Click "Save public key". Give a filename of "id_rsa.pub" (please save |
---|
50 | files into the same directory as where the executables are) |
---|
51 | |
---|
52 | Click "Save private key". Give a filename of "id_rsa.ppk" |
---|
53 | |
---|
54 | Use the mouse to highlight all the text in the box "Public key for pasting |
---|
55 | into OpenSSH authorized_keys file", and copy it to the clipboard. |
---|
56 | |
---|
57 | Exit puttygen. |
---|
58 | |
---|
59 | NOTE: Key generation is a one-off exercise. The more you deploy your public |
---|
60 | key, the more work it to be if you were to lose it and have to start again |
---|
61 | with a new one. I suggest you keep a secure backup of it somewhere, e.g. |
---|
62 | on a CD-ROM that you lock away. |
---|
63 | |
---|
64 | 2. Copy the PUBLIC key onto your Unix server |
---|
65 | -------------------------------------------- |
---|
66 | |
---|
67 | You have two ways of doing this. |
---|
68 | |
---|
69 | ### Copy-paste ### |
---|
70 | |
---|
71 | Use putty.exe to make a normal ssh connection to your host as the 'sysadm' |
---|
72 | user. |
---|
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 | |
---|
79 | The key consists of one very long line, which looks like |
---|
80 | |
---|
81 | ssh-rsa <lots of base64 data> <comment> |
---|
82 | |
---|
83 | As 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 | |
---|
88 | If you don't see "1", then you'll need to fix it (possibly with an editor, |
---|
89 | or else just rm the file and start again) |
---|
90 | |
---|
91 | Now logout. |
---|
92 | |
---|
93 | ### Alternative way (if you're having problems with copy-paste) ### |
---|
94 | |
---|
95 | Double-click on psftp.exe. Open a connection to your server, and upload |
---|
96 | your 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 | |
---|
106 | Unfortunately, this public key is not in the format which openssh requires, |
---|
107 | so now login again using putty.exe, and use the following command to convert |
---|
108 | it and put it in the right place. |
---|
109 | |
---|
110 | $ ssh-keygen -i -f id_rsa.pub >>.ssh/authorized_keys |
---|
111 | |
---|
112 | 3. Login using your private key |
---|
113 | ------------------------------- |
---|
114 | |
---|
115 | Start putty.exe again. Enter the hostname as usual, but before clicking |
---|
116 | Open, browse in the left hand column to Connection > SSH > Auth |
---|
117 | |
---|
118 | [-] Connection |
---|
119 | | |
---|
120 | [-] SSH |
---|
121 | | |- Keyex |
---|
122 | | |- Auth <--- CLICK HERE |
---|
123 | |
---|
124 | Next to "Private key for authentication", click Browse. Find your |
---|
125 | id_rsa.ppk file, open it, then click Open to start the connection. |
---|
126 | |
---|
127 | You should be prompted for your username as before (sysadm), but then |
---|
128 | instead of being prompted for a password, you are asked for the passphrase |
---|
129 | for your private key. Enter it, and you should be logged in. |
---|
130 | |
---|
131 | This is quite painful (both locating the private key and entering the |
---|
132 | passphrase), so as the final step of the exercise we're going automate it |
---|
133 | using an agent. |
---|
134 | |
---|
135 | 4. Use a passphrase agent |
---|
136 | ------------------------- |
---|
137 | |
---|
138 | Run `pageant.exe` |
---|
139 | |
---|
140 | It runs in the background, and adds an icon to your task tray (a PC with |
---|
141 | a black hat at a jaunty angle). You may need to select "Show hidden icons" |
---|
142 | to see it. |
---|
143 | |
---|
144 | Right-click on the icon, and select "Add Key". Browse to your id_rsa.ppk |
---|
145 | and open it. You will be prompted for the passphrase - enter it. (If you |
---|
146 | make a mistake, you'll be prompted again until you get it right) |
---|
147 | |
---|
148 | Now run putty.exe again, enter your hostname, click Open, and enter your |
---|
149 | username (sysadm). You should be logged in immediately, with no prompt for |
---|
150 | either a password or a passphrase! |
---|
151 | |
---|
152 | Try 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 |
---|
154 | until you tell Pageant to forget the private key. |
---|
155 | |
---|
156 | |
---|
157 | For laptops running Linux (or BSD or OSX) |
---|
158 | ========================================= |
---|
159 | |
---|
160 | 1. 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 | |
---|
174 | The passphrase is used to keep your private key encrypted on disk. It can be |
---|
175 | pretty much anything you want and as long as you want - including spaces - |
---|
176 | but if you forget it, your key becomes worthless. For now pick something |
---|
177 | that you will easily remember. You can change it at any time you want in |
---|
178 | the future (using `ssh-keygen -p`) |
---|
179 | |
---|
180 | NOTE: Key generation is a one-off exercise. The more you deploy your public |
---|
181 | key, the more work it to be if you were to lose it and have to start again |
---|
182 | with a new one. I suggest you keep a secure backup of it somewhere, e.g. |
---|
183 | on a CD-ROM that you lock away. |
---|
184 | |
---|
185 | 2. Copy the PUBLIC key onto your Unix server |
---|
186 | -------------------------------------------- |
---|
187 | |
---|
188 | The 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 | |
---|
192 | Note that .ssh/authorized_keys can contain multiple keys, one per line, |
---|
193 | so 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 | |
---|
197 | 3. Login using your private key |
---|
198 | ------------------------------- |
---|
199 | |
---|
200 | Open an ssh connection to your server as normal: |
---|
201 | |
---|
202 | $ ssh sysadm@pcN.ws.nsrc.org |
---|
203 | |
---|
204 | This time, instead of being prompted for your password, you should be |
---|
205 | prompted for the passphrase on your private key. Enter it. You should be |
---|
206 | logged in. |
---|
207 | |
---|
208 | 4. Use a passphrase agent |
---|
209 | ------------------------- |
---|
210 | |
---|
211 | Entering a passphrase every time you connect would be painful, but this |
---|
212 | isn't necessary if you have an agent which decrypts the private key and |
---|
213 | keeps it in memory. |
---|
214 | |
---|
215 | If you are running under a modern graphical environment like Gnome, you |
---|
216 | probably already got a dialog box prompting you for a passphrase, and this |
---|
217 | means you're already running an agent. You should be able to logout and |
---|
218 | login to the remote server, without being prompted for your passphrase |
---|
219 | again. |
---|
220 | |
---|
221 | To see what identities (decrypted private keys) your agent has in memory: |
---|
222 | |
---|
223 | $ ssh-add -l |
---|
224 | |
---|
225 | To forget all identities: |
---|
226 | |
---|
227 | $ ssh-add -d |
---|
228 | |
---|
229 | If you don't have an agent, then you can start a new subshell with ssh-agent |
---|
230 | as its parent: |
---|
231 | |
---|
232 | $ ssh-agent bash |
---|
233 | $ ssh-add |
---|
234 | ... prompted for your passphrase |
---|
235 | $ |
---|
236 | |
---|
237 | Now the agent will handle future connections for you. |
---|
238 | |
---|
239 | If you are running an older graphical environment, and you normally start |
---|
240 | X using `startx`, then start it using `ssh-agent startx` instead. Then type |
---|
241 | 'ssh-add' in an xterm. |
---|
242 | |
---|
243 | --------------------------------------------------------------------------- |
---|
244 | |
---|
245 | Additional information [not part of exercises] |
---|
246 | ============================================== |
---|
247 | |
---|
248 | Agent forwarding |
---|
249 | ---------------- |
---|
250 | |
---|
251 | Using an agent, you can access across multiple ssh hops without having to |
---|
252 | copy your key or enter your passphrase anywhere. |
---|
253 | |
---|
254 | If you enable "agent forwarding" when you login to host X, you can then |
---|
255 | login from X to Y without any prompting (assuming Y has your public key in |
---|
256 | authorized_keys). The request to authenticate is forwarded securely back |
---|
257 | along your original ssh session to the agent running on your workstation. |
---|
258 | |
---|
259 | Under Unix: |
---|
260 | |
---|
261 | $ ssh -o ForwardAgent=yes user@host |
---|
262 | |
---|
263 | If 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 | |
---|
270 | Then you only have to type `ssh foo` to get a connection with those options. |
---|
271 | |
---|
272 | Advanced: X11 forwarding and port forwarding |
---|
273 | -------------------------------------------- |
---|
274 | |
---|
275 | ssh 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 | |
---|