| 1 | <h1>SSH Exercises</h1> |
|---|
| 2 | |
|---|
| 3 | <h2>Index</h2> |
|---|
| 4 | <ul> |
|---|
| 5 | <li><b><a href="#SSH">SSH with private/public key authentication</a></b></li> |
|---|
| 6 | <li><b><a href="#Windows">For laptops running Windows</a></b></li> |
|---|
| 7 | <ul> |
|---|
| 8 | <li>1. Generate an ssh public/private key pair</li> |
|---|
| 9 | <li>2. Copy the PUBLIC key onto your Unix server</li> |
|---|
| 10 | <ul> |
|---|
| 11 | <li>Copy-paste</li> |
|---|
| 12 | <li>Alternative way (if you're having problems with copy-paste)</li> |
|---|
| 13 | </ul> |
|---|
| 14 | <li>3. Login using your private key</li> |
|---|
| 15 | <li>4. Use a passphrase agent</li> |
|---|
| 16 | </ul> |
|---|
| 17 | <li><b><a href="#LinuxUnix">For laptops running Linux (or BSD or OSX)</a></b></li> |
|---|
| 18 | <ul> |
|---|
| 19 | <li>1. Generate an ssh public/private key pair</li> |
|---|
| 20 | <li>2. Copy the PUBLIC key onto your Unix server</li> |
|---|
| 21 | <ul> |
|---|
| 22 | <li>Copy-paste</li> |
|---|
| 23 | </ul> |
|---|
| 24 | <li>3. Login using your private key</li> |
|---|
| 25 | <li>4. Use a passphrase agent</li> |
|---|
| 26 | </ul> |
|---|
| 27 | <li><b><a href="#root">Copy your public keys to the root account</a></b></li> |
|---|
| 28 | <li><b><a href="#additional">Additional information [not part of exercises]</a></b></li> |
|---|
| 29 | <ul> |
|---|
| 30 | <li>Disable Password Access to your Machine</li> |
|---|
| 31 | <ul> |
|---|
| 32 | <li>Connect Only with SSH Keys</li> |
|---|
| 33 | </ul> |
|---|
| 34 | <li>Agent forwarding</li> |
|---|
| 35 | <li>Advanced: X11 forwarding and port forwarding</li> |
|---|
| 36 | </ul> |
|---|
| 37 | </ul> |
|---|
| 38 | |
|---|
| 39 | <p>In these exrcises we'll show how you can eliminate passwords by using ssh key |
|---|
| 40 | authentication and make this process more convenient.</p> |
|---|
| 41 | |
|---|
| 42 | <p>Choose the version of the exercises depending on what OS you are running on |
|---|
| 43 | your laptop.</p> |
|---|
| 44 | |
|---|
| 45 | <a id="SSH"></a> |
|---|
| 46 | <h1>SSH with private/public key authentication</h1> |
|---|
| 47 | |
|---|
| 48 | <p>Remember: the <code>$</code> character before commands indicates that they are to be |
|---|
| 49 | run as your normal login user, not as root.</p> |
|---|
| 50 | |
|---|
| 51 | <a id="Windows"></a> |
|---|
| 52 | <h1>For laptops running Windows</h1> |
|---|
| 53 | |
|---|
| 54 | <p>Download the following onto your desktop or into a downloads folder:</p> |
|---|
| 55 | |
|---|
| 56 | <ul> |
|---|
| 57 | <li>putty.exe</li> |
|---|
| 58 | <li>pageant.exe</li> |
|---|
| 59 | <li>puttygen.exe</li> |
|---|
| 60 | <li>pscp.exe</li> |
|---|
| 61 | <li>psftp.exe</li> |
|---|
| 62 | </ul> |
|---|
| 63 | |
|---|
| 64 | <p>For this class you can download each of these items by going to:</p> |
|---|
| 65 | |
|---|
| 66 | <blockquote> |
|---|
| 67 | <a href="http://wsnoc.nsrc.org/wiki/Putty">http://wsnoc.nsrc.org/wiki/Putty</a> |
|---|
| 68 | </blockquote> |
|---|
| 69 | |
|---|
| 70 | <p>Outside of class you can go to <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html">http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html</a> |
|---|
| 71 | (Or you can try the installer bundle which gets them all)</p> |
|---|
| 72 | |
|---|
| 73 | <h2>1. Generate an ssh public/private key pair</h2> |
|---|
| 74 | |
|---|
| 75 | <p>Double-click on <code>puttygen.exe</code></p> |
|---|
| 76 | |
|---|
| 77 | <p>At the bottom of the dialog box, under "Parameters":</p> |
|---|
| 78 | |
|---|
| 79 | <ul> |
|---|
| 80 | <li>Make sure the type of key to generate is "SSH-2 RSA" (near center-bottom of screen)</li> |
|---|
| 81 | <li>Set the number of bits to 2048 (bottom right of the screen)</li> |
|---|
| 82 | </ul> |
|---|
| 83 | |
|---|
| 84 | <p>Click on "Generate". Move the mouse randomly over the blank area until the |
|---|
| 85 | progress bar reaches 100%</p> |
|---|
| 86 | |
|---|
| 87 | Now you will have some additional fields in the puttygen.exe window: |
|---|
| 88 | |
|---|
| 89 | <pre><code> |
|---|
| 90 | Key comment: [Your Name <your@email.address> ] |
|---|
| 91 | Key passphrase: [chooose a passphrase ] |
|---|
| 92 | Confirm passphrase: [choose same passphrase ] |
|---|
| 93 | </code></pre> |
|---|
| 94 | |
|---|
| 95 | <p>The passphrase is used to keep your private key encrypted on disk. It can be |
|---|
| 96 | pretty much anything you want and as long as you want - including spaces - |
|---|
| 97 | but if you forget it, your key becomes worthless. For now pick something |
|---|
| 98 | that you will easily remember. You can change it at any time you want in |
|---|
| 99 | the future.</p> |
|---|
| 100 | |
|---|
| 101 | <p>Click "Save public key". Give a filename of "id_rsa.pub" (please save |
|---|
| 102 | files into the same directory as where the executables are)</p> |
|---|
| 103 | |
|---|
| 104 | <p>Click "Save private key". Give a filename of "id_rsa.ppk"</p> |
|---|
| 105 | |
|---|
| 106 | <p>Use the mouse to highlight all the text in the box "Public key for pasting |
|---|
| 107 | into OpenSSH authorized_keys file", and copy it to the clipboard.</p> |
|---|
| 108 | |
|---|
| 109 | <p>Exit puttygen.</p> |
|---|
| 110 | |
|---|
| 111 | <p>NOTE: Key generation is a one-off exercise. The more you deploy your public |
|---|
| 112 | key, the more work it will be if you were to lose it and have to start again |
|---|
| 113 | with a new one. We suggest you keep a secure backup of it somewhere, e.g. |
|---|
| 114 | on a CD-ROM, a safe USB key that you lock away, an encrypted file somewhere else, etc.</p> |
|---|
| 115 | |
|---|
| 116 | <h2>2. Copy the PUBLIC key onto your Unix server</h2> |
|---|
| 117 | |
|---|
| 118 | <p>You have two ways of doing this.</p> |
|---|
| 119 | |
|---|
| 120 | <h3>Copy-paste</h3> |
|---|
| 121 | |
|---|
| 122 | <p>Use putty.exe to make a normal ssh connection to your MacMini as the 'nsrc' |
|---|
| 123 | user. Your MacMini should be accessible as s1.ws.nsrc.org, but you use the IP address if |
|---|
| 124 | necessary of 10.10.0.241.</p> |
|---|
| 125 | |
|---|
| 126 | <p>You will likely see a window about the server's host key not being cached in the registry. In |
|---|
| 127 | order to connect click on "Yes" to continue. Do you understand what this meant?</p> |
|---|
| 128 | |
|---|
| 129 | <p>Check so if there is a .ssh directory already in the home directory for the nsrc user:</p> |
|---|
| 130 | |
|---|
| 131 | <pre><code>ls -la</code></pre> |
|---|
| 132 | |
|---|
| 133 | <p>If you do not see the directory, then you are the first one to connect. Create the .ssh |
|---|
| 134 | directory like this:</p> |
|---|
| 135 | |
|---|
| 136 | <pre><code>$ mkdir .ssh |
|---|
| 137 | $ chmod 700 .ssh |
|---|
| 138 | </code></pre> |
|---|
| 139 | |
|---|
| 140 | <p>Now you will paste the key you copied to your clipboard earlier in to a file called |
|---|
| 141 | .ssh/authorized_keys. The way we do this ensures that the text you paste will go on the end of the |
|---|
| 142 | file so you don't overwrite the public keys that your classmates are placing in this file as well</p> |
|---|
| 143 | |
|---|
| 144 | <pre><code>$ cat >>.ssh/authorized_keys |
|---|
| 145 | *** PASTE KEY FROM CLIPBOARD *** |
|---|
| 146 | *** If the cursor is still at the end of the line, hit Enter *** |
|---|
| 147 | *** hit ctrl-D *** |
|---|
| 148 | </code></pre> |
|---|
| 149 | |
|---|
| 150 | <p>The key consists of one very long line, which looks like</p> |
|---|
| 151 | |
|---|
| 152 | <pre><code>ssh-rsa <lots of base64 data> <comment> |
|---|
| 153 | </code></pre> |
|---|
| 154 | |
|---|
| 155 | <p>As a quick check that it hasn't been corrupted, count the lines in the file:</p> |
|---|
| 156 | |
|---|
| 157 | <pre><code>$ wc -l .ssh/authorized_keys |
|---|
| 158 | 1 .ssh/authorized_keys |
|---|
| 159 | </code></pre> |
|---|
| 160 | |
|---|
| 161 | <p>If you don't see "1", then you'll need to fix it (possibly with an editor). You can ask |
|---|
| 162 | an instructor for help. Don't delete the file!</p> |
|---|
| 163 | |
|---|
| 164 | <p>Now let's be sure that the file .ssh/authorized_keys ahs the right permissions set, otherwise the |
|---|
| 165 | ssh server on your MacMini will not allow you to connect using your private key. To do this do:</p> |
|---|
| 166 | |
|---|
| 167 | <pre><code>$ chmod 600 .ssh/authorized_keys |
|---|
| 168 | </code></pre> |
|---|
| 169 | |
|---|
| 170 | <p>Now log out of your MacMini</p> |
|---|
| 171 | |
|---|
| 172 | <pre><code>$ exit |
|---|
| 173 | </code></pre> |
|---|
| 174 | |
|---|
| 175 | <h3>Alternative way (if you're having problems with copy-paste)</h3> |
|---|
| 176 | |
|---|
| 177 | <p>Double-click on psftp.exe. Open a connection to your server, and upload |
|---|
| 178 | your public key:</p> |
|---|
| 179 | |
|---|
| 180 | <pre><code>psftp> open s1.ws.nsrc.org |
|---|
| 181 | login as: nsrc |
|---|
| 182 | nsrc@s1.ws.nsrc.org's password: <usual one> |
|---|
| 183 | Remote working directory is /home/nsrc |
|---|
| 184 | psftp> put id_rsa.pub |
|---|
| 185 | local:id_rsa.pub => remote:/home/nsrc/id_rsa.pub |
|---|
| 186 | psftp> quit |
|---|
| 187 | </code></pre> |
|---|
| 188 | |
|---|
| 189 | <p>Unfortunately, this public key is not in the format which openssh requires, |
|---|
| 190 | so now login again using putty.exe to connect to the server again as the user nsrc. First |
|---|
| 191 | we need to see if the .ssh directory exists in the nsrc user home directory:</p> |
|---|
| 192 | |
|---|
| 193 | <pre><code>ls -la</code></pre> |
|---|
| 194 | |
|---|
| 195 | <p>If you do not see the directory, then you are the first one to connect. Create the .ssh |
|---|
| 196 | directory like this:</p> |
|---|
| 197 | |
|---|
| 198 | <pre><code>$ mkdir .ssh |
|---|
| 199 | $ chmod 700 .ssh |
|---|
| 200 | </code></pre> |
|---|
| 201 | |
|---|
| 202 | <p>Now you can use the following command to convert the id_rsa.pub key file you uploaded to the |
|---|
| 203 | home directory and place this key at the end of a file called authorized keys in the .ssh |
|---|
| 204 | directory:</p> |
|---|
| 205 | |
|---|
| 206 | <pre><code>$ ssh-keygen -i -f id_rsa.pub >>.ssh/authorized_keys |
|---|
| 207 | </code></pre> |
|---|
| 208 | |
|---|
| 209 | <p>Now let's be sure that the file .ssh/authorized_keys has the right permissions set, otherwise the |
|---|
| 210 | ssh server on your MacMini will not allow you to connect using your private key. To do this do:</p> |
|---|
| 211 | |
|---|
| 212 | <pre><code>$ chmod 600 .ssh/authorized_keys |
|---|
| 213 | </code></pre> |
|---|
| 214 | |
|---|
| 215 | <p>We should remove our id_rsa.pub file sitting in the /home/nsrc directory:</p> |
|---|
| 216 | |
|---|
| 217 | <pre><code>$ rm id_rsa.pub |
|---|
| 218 | </code></pre> |
|---|
| 219 | |
|---|
| 220 | <p>Now log out of your MacMini:</p> |
|---|
| 221 | |
|---|
| 222 | <pre><code>$ exit |
|---|
| 223 | </code></pre> |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | <h2>3. Login using your private key</h2> |
|---|
| 227 | |
|---|
| 228 | <p>Start putty.exe again. Enter the hostname as usual, but before clicking |
|---|
| 229 | Open, browse in the left hand column to Connection > SSH > Auth</p> |
|---|
| 230 | |
|---|
| 231 | <pre><code>[-] Connection |
|---|
| 232 | | |
|---|
| 233 | [-] SSH |
|---|
| 234 | | |- Keyex |
|---|
| 235 | | |- Auth <--- CLICK HERE |
|---|
| 236 | </code></pre> |
|---|
| 237 | |
|---|
| 238 | <p>Next to "Private key for authentication", click Browse. Find your |
|---|
| 239 | id_rsa.ppk file, open it, then go back to the Open option to start the connection. Remember |
|---|
| 240 | Host Name or IP address is s1.ws.nsrc.org or 10.10.0.241</p> |
|---|
| 241 | |
|---|
| 242 | <p>You should be prompted for your username as before (nsrc), but then |
|---|
| 243 | instead of being prompted for a password, you are asked for the passphrase |
|---|
| 244 | for your private key. Enter it, and you should be logged in.</p> |
|---|
| 245 | |
|---|
| 246 | <p>This is quite painful (both locating the private key and entering the |
|---|
| 247 | passphrase), so as the final step of the exercise we're going automate it |
|---|
| 248 | using an agent.</p> |
|---|
| 249 | |
|---|
| 250 | <h2>4. Use a passphrase agent</h2> |
|---|
| 251 | |
|---|
| 252 | <p>Run <code>pageant.exe</code></p> |
|---|
| 253 | |
|---|
| 254 | <p>It runs in the background, and adds an icon to your task tray (a PC with |
|---|
| 255 | a black hat at an angle). You may need to select "Show hidden icons" |
|---|
| 256 | to see it.</p> |
|---|
| 257 | |
|---|
| 258 | <p>Right-click on the icon, and select "Add Key". Browse to your id_rsa.ppk |
|---|
| 259 | and open it. You will be prompted for the passphrase - enter it. (If you |
|---|
| 260 | make a mistake, you'll be prompted again until you get it right)</p> |
|---|
| 261 | |
|---|
| 262 | <p>Now run putty.exe again, enter your hostname, click Open, and enter your |
|---|
| 263 | username (sysadm). You should be logged in immediately, with no prompt for |
|---|
| 264 | either a password or a passphrase!</p> |
|---|
| 265 | |
|---|
| 266 | <p>Try logging in again. Also try using psftp.exe (when it runs, enter |
|---|
| 267 | "open wsnoc.nsrc.org" to start a connection). No passphrase is needed |
|---|
| 268 | until you tell Pageant to forget the private key. This can save quite a bit of time |
|---|
| 269 | if you are making multiple ssh connections.</p> |
|---|
| 270 | |
|---|
| 271 | <p>With Putty you save preconfigured sessions to save steps as well as play with many other |
|---|
| 272 | available options. Feel free to do this if you wish at your leisure.</p> |
|---|
| 273 | |
|---|
| 274 | <a id="LinuxUnix"></a> |
|---|
| 275 | <h1>For laptops running Linux (or BSD or OSX)</h1> |
|---|
| 276 | |
|---|
| 277 | <h2>1. Generate an ssh public/private key pair</h2> |
|---|
| 278 | |
|---|
| 279 | <p><b>WARNING!</b>: If you already have a public/private key that you use, then skip the first |
|---|
| 280 | exercise and go directly to exercise 2.</p> |
|---|
| 281 | |
|---|
| 282 | <p>Note that the "~" character represents the path to your home directory on your laptop.</p> |
|---|
| 283 | |
|---|
| 284 | <pre><code>$ ssh-keygen -t rsa -b 2048 |
|---|
| 285 | Generating public/private rsa key pair. |
|---|
| 286 | Enter file in which to save the key (~/.ssh/id_rsa): <HIT ENTER> |
|---|
| 287 | Created directory '~/.ssh'. |
|---|
| 288 | Enter passphrase (empty for no passphrase): <CHOOSE PASSPHRASE> |
|---|
| 289 | Enter same passphrase again: <SAME PASSPHRASE> |
|---|
| 290 | Your identification has been saved in ~/.ssh/id_rsa. |
|---|
| 291 | Your public key has been saved in ~/.ssh/id_rsa.pub. |
|---|
| 292 | The key fingerprint is: |
|---|
| 293 | 32:2b:e3:0e:14:fb:60:38:a6:e2:73:95:53:9d:a8:0f userid@yourlaptopname |
|---|
| 294 | </code></pre> |
|---|
| 295 | |
|---|
| 296 | <p>The passphrase is used to keep your private key encrypted on disk. It can be |
|---|
| 297 | pretty much anything you want and as long as you want - including spaces - |
|---|
| 298 | but if you forget it, your key becomes worthless. For now pick something |
|---|
| 299 | that you will easily remember. You can change it at any time you want in |
|---|
| 300 | the future (using <code>ssh-keygen -p</code>)</p> |
|---|
| 301 | |
|---|
| 302 | <p>NOTE: Key generation is a one-off exercise. The more you deploy your public |
|---|
| 303 | key, the more work it will be if you were to lose it and have to start again |
|---|
| 304 | with a new one. We suggest you keep a secure backup of it somewhere, e.g. |
|---|
| 305 | on a CD-ROM, a safe USB key that you lock away, an encrypted file somewhere else, etc.</p> |
|---|
| 306 | |
|---|
| 307 | <h2>2. Copy the PUBLIC key onto your Unix server</h2> |
|---|
| 308 | |
|---|
| 309 | <p>First we need to see if ssh has been set up on your MacMini and your nsrc account is ready |
|---|
| 310 | to accept public keys. To do this do:</p> |
|---|
| 311 | |
|---|
| 312 | <pre><code>$ ssh nsrc@s1.ws.nsrc.org</code></pre> |
|---|
| 313 | |
|---|
| 314 | <p>If you are prompted to say Yes or No to accepting the public key from the MacMini be sure to |
|---|
| 315 | answer "Yes" (you have to type "Yes" out fully). Do you understand what is happening? Now let's see |
|---|
| 316 | if there is a ".ssh" directory in the nsrc home directory on your MacMini:</p> |
|---|
| 317 | |
|---|
| 318 | <pre><code>ls -la</code></pre> |
|---|
| 319 | |
|---|
| 320 | <p>If you do not see the directory, then you are the first one to connect. Create the .ssh |
|---|
| 321 | directory and authorized_keys file. If you do see a ".ssh" directory, then please skip down |
|---|
| 322 | to the "Copying your public key" step below. To create the directory and file do this:</p> |
|---|
| 323 | |
|---|
| 324 | <pre><code>$ mkdir .ssh |
|---|
| 325 | $ chmod 700 .ssh |
|---|
| 326 | </code></pre> |
|---|
| 327 | |
|---|
| 328 | <p>Now let's create an empty .ssh/authorized_keys file with the correct permissions so that as we |
|---|
| 329 | place our public keys in the file things will work correctly</p> |
|---|
| 330 | |
|---|
| 331 | <pre><code>$ touch .ssh/authorized_keys |
|---|
| 332 | $ chmod 600 .ssh/authorized_keys |
|---|
| 333 | </code></pre> |
|---|
| 334 | |
|---|
| 335 | <p>Now log out of the MacMini to go back to your laptop</p> |
|---|
| 336 | |
|---|
| 337 | <h3>Copying your public key</h3> |
|---|
| 338 | |
|---|
| 339 | <p>The simplest way to copy the public key is with scp and the cat commnd. We do this |
|---|
| 340 | in this manner to avoid overwriting other people's public key in the authorized_keys file |
|---|
| 341 | on our MacMinis.</p> |
|---|
| 342 | |
|---|
| 343 | <p>From a terminal window on your laptop</p> |
|---|
| 344 | |
|---|
| 345 | <pre><code>$ cd |
|---|
| 346 | $ cat .ssh/id_rsa.pub | ssh nsrc@s1.ws.nsrc.org 'cat >>.ssh/authorized_keys' |
|---|
| 347 | </code></pre> |
|---|
| 348 | |
|---|
| 349 | <h2>3. Login using your private key</h2> |
|---|
| 350 | |
|---|
| 351 | <p>Open an ssh connection to your server as normal:</p> |
|---|
| 352 | |
|---|
| 353 | <pre><code>$ ssh nsrc@s1.ws.nsrc.org |
|---|
| 354 | </code></pre> |
|---|
| 355 | |
|---|
| 356 | <p>This time, instead of being prompted for your password, you should be |
|---|
| 357 | prompted for the passphrase on your private key. Enter it. You should be |
|---|
| 358 | logged in.</p> |
|---|
| 359 | |
|---|
| 360 | <h2>4. Use a passphrase agent</h2> |
|---|
| 361 | |
|---|
| 362 | <p>Entering a passphrase every time you connect would be painful, but this |
|---|
| 363 | isn't necessary if you have an agent which decrypts the private key and |
|---|
| 364 | keeps it in memory.</p> |
|---|
| 365 | |
|---|
| 366 | <p>If you are running under a modern graphical environment like Gnome or Mac OS X, you |
|---|
| 367 | probably already got a dialog box prompting you for a passphrase, and this |
|---|
| 368 | means you're already running an agent. You should be able to logout and |
|---|
| 369 | login to the remote server, without being prompted for your passphrase |
|---|
| 370 | again.</p> |
|---|
| 371 | |
|---|
| 372 | <p>If you don't have an agent, then you can start a new subshell with ssh-agent |
|---|
| 373 | as its parent:</p> |
|---|
| 374 | |
|---|
| 375 | <pre><code>$ ssh-agent bash |
|---|
| 376 | $ ssh-add |
|---|
| 377 | ... prompted for your passphrase |
|---|
| 378 | $ |
|---|
| 379 | </code></pre> |
|---|
| 380 | |
|---|
| 381 | <p>To see what identities (decrypted private keys) your agent has in memory:</p> |
|---|
| 382 | |
|---|
| 383 | <pre><code>$ ssh-add -l |
|---|
| 384 | </code></pre> |
|---|
| 385 | |
|---|
| 386 | <p>To forget all identities:</p> |
|---|
| 387 | |
|---|
| 388 | <pre><code>$ ssh-add -d |
|---|
| 389 | </code></pre> |
|---|
| 390 | |
|---|
| 391 | <p>(By the way, if you do the above command you will be prompted for your passphrase again the |
|---|
| 392 | next time you connect to your MacMini.)</p> |
|---|
| 393 | |
|---|
| 394 | <p>Now the agent will handle future connections for you.</p> |
|---|
| 395 | |
|---|
| 396 | <p>If you are running an older graphical environment, and you normally start |
|---|
| 397 | X using <code>startx</code>, then start it using <code>ssh-agent startx</code> instead. Then type |
|---|
| 398 | 'ssh-add' in an xterm.</p> |
|---|
| 399 | |
|---|
| 400 | <a id="root"></a> |
|---|
| 401 | <h1>Copy your public keys to the root account</h1> |
|---|
| 402 | |
|---|
| 403 | <p>Everyone can do this exercise. You should do this once everyone has copied their public |
|---|
| 404 | key to the /home/nsrc/.ssh/authorized_keys files on your group's MacMini, so check with your neighbor's |
|---|
| 405 | to see if they are done with that part of the exercises.</p> |
|---|
| 406 | |
|---|
| 407 | <p>Log in to your MacMini as the user nsrc. Once you are logged in do the following. When |
|---|
| 408 | prompted for a password use the same one you used originally to connect to the nsrc account:</p> |
|---|
| 409 | |
|---|
| 410 | <pre><code>$ sudo su - |
|---|
| 411 | # cd /root |
|---|
| 412 | # mkdir .ssh |
|---|
| 413 | # chmod 700 .ssh |
|---|
| 414 | # cat /home/nsrc/.ssh/authorized_keys >> .ssh/authorized_keys |
|---|
| 415 | # chown root:root .ssh/authorized_keys |
|---|
| 416 | # chmod 600 .ssh/authorized_keys |
|---|
| 417 | </code></pre> |
|---|
| 418 | |
|---|
| 419 | <p>If you wish to see how things look in the /root/.ssh directory do:</p> |
|---|
| 420 | |
|---|
| 421 | <pre><code># ls -la .ssh</code></pre> |
|---|
| 422 | |
|---|
| 423 | <p>Now you can log off from the MacMini and try connecting directly as the root user. You need to |
|---|
| 424 | type exit twice as the sudo command opened a new shell on the MacMini:</p> |
|---|
| 425 | |
|---|
| 426 | <pre><code># exit |
|---|
| 427 | $ exit |
|---|
| 428 | </code></pre> |
|---|
| 429 | |
|---|
| 430 | <p>Now try connection to the MacMini as the root user (root@s1.ws.nsrc.org) and see if you are prompted |
|---|
| 431 | for your passphrase, or if you log in without any prompt as your ssh agent program should take care |
|---|
| 432 | of the private/public key exchange between your machine and the MacMini.</p> |
|---|
| 433 | |
|---|
| 434 | <p>Once you are done you can log off.</p> |
|---|
| 435 | |
|---|
| 436 | <hr /> |
|---|
| 437 | |
|---|
| 438 | <a id="additional"></a> |
|---|
| 439 | <h1>Additional information [not part of exercises]</h1> |
|---|
| 440 | |
|---|
| 441 | <div align="center"> |
|---|
| 442 | <p>This section is for reference only. We are <em>not</em> going to turn off password access at |
|---|
| 443 | this time, so please do not do this exercise.</p> |
|---|
| 444 | </div> |
|---|
| 445 | |
|---|
| 446 | <h2>Disable Password Access to your Machine</h2> |
|---|
| 447 | |
|---|
| 448 | <h3>Connect Only with SSH Keys</h3> |
|---|
| 449 | |
|---|
| 450 | <p>Only do this exercise if you have successfully copied your public key to your machine and you |
|---|
| 451 | are being prompted for your ssh private key passphrase when you log in and not your password.</p> |
|---|
| 452 | |
|---|
| 453 | <p>Log in on your machine. Once logged in become the root user:</p> |
|---|
| 454 | |
|---|
| 455 | <pre><code>$ sudo su -</pre></code> |
|---|
| 456 | |
|---|
| 457 | <p>As the root user cat the authorized_keys file to the directory /root/.ssh. This wasy we ensure |
|---|
| 458 | we don't overwrite any other /root/.ssh/authorized_keys file that might already be in place.</p> |
|---|
| 459 | |
|---|
| 460 | <pre><code># mkdir /root/.ssh |
|---|
| 461 | # chmod 700 /root/.ssh |
|---|
| 462 | # cat /home/nsrc/.ssh/authorized_keys >> /root/.ssh/authorized_keys |
|---|
| 463 | # chown root:root /root/.ssh/authorized_keys |
|---|
| 464 | # chmod 644 /root/.ssh/authorized_keys |
|---|
| 465 | </pre></code> |
|---|
| 466 | |
|---|
| 467 | <p>Now log out of your machine and try to log back in, but this time as the "root" user, |
|---|
| 468 | not as the "nsrc" user. If you are prompted for your ssh private key passphrase and not a |
|---|
| 469 | password, then you are ready to disable password access to your machine.</p> |
|---|
| 470 | |
|---|
| 471 | <p>Log in on your machine as the root user. Now we are going to edit the file /etc/ssh/sshd_config.</p> |
|---|
| 472 | |
|---|
| 473 | <pre><code># vi /etc/ssh/sshd_config</pre></code> |
|---|
| 474 | |
|---|
| 475 | <p>Insie the file look for the following line:</p> |
|---|
| 476 | |
|---|
| 477 | <code>#PasswordAuthentication yes</code> |
|---|
| 478 | |
|---|
| 479 | <p>Just after this line add a line that says:</p> |
|---|
| 480 | |
|---|
| 481 | <code>PasswordAuthentication no</code> |
|---|
| 482 | |
|---|
| 483 | <p>Save and exit from the file. The reload the ssh server:</p> |
|---|
| 484 | |
|---|
| 485 | <pre><code># service ssh reload</pre></code> |
|---|
| 486 | |
|---|
| 487 | <p>Before logging out we recommend you leave your current session open in case there are problems. You |
|---|
| 488 | could lock yourself out of your machine. If you do let your instructor know. Now use ssh or putty on your laptop |
|---|
| 489 | and try connecting to your server. You should get prompted for your ssh private key's passphrase and |
|---|
| 490 | be able to log in. If you did, everthing is working. At this point all new users on your machine must |
|---|
| 491 | use ssh keys to connect and not passwords. If you wish to verify this you can do the following:</p> |
|---|
| 492 | |
|---|
| 493 | <pre><code># adduser testuser</pre></code> |
|---|
| 494 | |
|---|
| 495 | <p>Answer the on-screen prompts. Once the user has been created and you have given it a new password of |
|---|
| 496 | your choosing, try to open a new ssh session to your machine as that user. You should either be rejected |
|---|
| 497 | or your password attempts will have not affect (depends on the ssh server version).</p> |
|---|
| 498 | |
|---|
| 499 | <p>To log in as this user you would need to copy your public ssh key to their account as we did for |
|---|
| 500 | your nsrc user. This <b>greatly</b> enhances the security of your system. |
|---|
| 501 | |
|---|
| 502 | <h2>Agent forwarding</h2> |
|---|
| 503 | |
|---|
| 504 | <p>Using an agent, you can access across multiple ssh hops without having to |
|---|
| 505 | copy your key or enter your passphrase anywhere.</p> |
|---|
| 506 | |
|---|
| 507 | <p>If you enable "agent forwarding" when you login to host X, you can then |
|---|
| 508 | login from X to Y without any prompting (assuming Y has your public key in |
|---|
| 509 | authorized_keys). The request to authenticate is forwarded securely back |
|---|
| 510 | along your original ssh session to the agent running on your workstation.</p> |
|---|
| 511 | |
|---|
| 512 | <p>Under Unix:</p> |
|---|
| 513 | |
|---|
| 514 | <pre><code>$ ssh -o ForwardAgent=yes user@host |
|---|
| 515 | </code></pre> |
|---|
| 516 | |
|---|
| 517 | <p>If you do this frequently, it's easier to configure it in <code>.ssh/hosts</code></p> |
|---|
| 518 | |
|---|
| 519 | <pre><code>host foo |
|---|
| 520 | hostname foo.example.com |
|---|
| 521 | user nsrc |
|---|
| 522 | ForwardAgent yes |
|---|
| 523 | </code></pre> |
|---|
| 524 | |
|---|
| 525 | <p>Then you only have to type <code>ssh foo</code> to get a connection with those options.</p> |
|---|
| 526 | |
|---|
| 527 | <h2>Advanced: X11 forwarding and port forwarding</h2> |
|---|
| 528 | |
|---|
| 529 | <p>ssh can securely carry arbitrary forwarded TCP connections and X11 graphics.</p> |
|---|
| 530 | |
|---|
| 531 | <pre><code>$ ssh -L8080:some.where:80 user@remote.host |
|---|
| 532 | ... while ssh connection is open, a connection to 127.0.0.1 port 8080 |
|---|
| 533 | ... will be tunneled, and the far end will open a connection to |
|---|
| 534 | ... some.where port 80 |
|---|
| 535 | |
|---|
| 536 | $ ssh -X user@remote.host |
|---|
| 537 | ... |
|---|
| 538 | $ xclock # graphical output redirected back through ssh tunnel |
|---|
| 539 | </code></pre> |
|---|