Agenda: exercise-ssh-key.2.htm

File exercise-ssh-key.2.htm, 15.5 KB (added by andy, 5 years ago)
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
2<html xmlns="">
4  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5  <meta http-equiv="Content-Style-Type" content="text/css" />
6  <meta name="generator" content="pandoc" />
7  <title>SSH with private/public key authentication</title>
8  <style type="text/css">code{white-space: pre;}</style>
9  <link rel="stylesheet" href="../../style.css" type="text/css" />
12<div id="header">
13<h1 class="title">SSH with private/public key authentication</h1>
15<div id="TOC">
17<li><a href="#introduction"><span class="toc-section-number">1</span> Introduction</a><ul>
18<li><a href="#goals"><span class="toc-section-number">1.1</span> Goals</a></li>
19<li><a href="#notes"><span class="toc-section-number">1.2</span> Notes</a></li>
21<li><a href="#for-laptops-running-windows"><span class="toc-section-number">2</span> For laptops running Windows</a><ul>
22<li><a href="#generate-an-ssh-publicprivate-key-pair"><span class="toc-section-number">2.1</span> 1. Generate an ssh public/private key pair</a></li>
23<li><a href="#copy-the-public-key-onto-your-unix-server"><span class="toc-section-number">2.2</span> 2. Copy the PUBLIC key onto your Unix server</a><ul>
24<li><a href="#copy-paste"><span class="toc-section-number">2.2.1</span> Copy-paste</a></li>
25<li><a href="#alternative-way-if-youre-having-problems-with-copy-paste"><span class="toc-section-number">2.2.2</span> Alternative way (if you're having problems with copy-paste)</a></li>
27<li><a href="#login-using-your-private-key"><span class="toc-section-number">2.3</span> 3. Login using your private key</a></li>
28<li><a href="#use-a-passphrase-agent"><span class="toc-section-number">2.4</span> 4. Use a passphrase agent</a></li>
30<li><a href="#for-laptops-running-linux-or-bsd-or-osx"><span class="toc-section-number">3</span> For laptops running Linux (or BSD or OSX)</a><ul>
31<li><a href="#generate-an-ssh-publicprivate-key-pair-1"><span class="toc-section-number">3.1</span> 1. Generate an ssh public/private key pair</a></li>
32<li><a href="#copy-the-public-key-onto-your-unix-server-1"><span class="toc-section-number">3.2</span> 2. Copy the PUBLIC key onto your Unix server</a></li>
33<li><a href="#login-using-your-private-key-1"><span class="toc-section-number">3.3</span> 3. Login using your private key</a></li>
34<li><a href="#use-a-passphrase-agent-1"><span class="toc-section-number">3.4</span> 4. Use a passphrase agent</a></li>
36<li><a href="#additional-information-not-part-of-exercises"><span class="toc-section-number">4</span> Additional information [not part of exercises]</a><ul>
37<li><a href="#agent-forwarding"><span class="toc-section-number">4.1</span> Agent forwarding</a></li>
38<li><a href="#advanced-x11-forwarding-and-port-forwarding"><span class="toc-section-number">4.2</span> Advanced: X11 forwarding and port forwarding</a></li>
42<h1 id="introduction"><a href="#introduction"><span class="header-section-number">1</span> Introduction</a></h1>
43<h2 id="goals"><a href="#goals"><span class="header-section-number">1.1</span> Goals</a></h2>
44<p>In this exercise we'll show how you can eliminate passwords by using ssh key authentication.</p>
45<h2 id="notes"><a href="#notes"><span class="header-section-number">1.2</span> Notes</a></h2>
47<li>Commands preceded with &quot;$&quot; imply that you should execute the command as a general user - not as root.</li>
48<li>Commands preceded with &quot;#&quot; imply that you should be working as root.</li>
49<li>Commands with more specific command lines (e.g. &quot;rtrX&gt;&quot; or &quot;mysql&gt;&quot;) imply that you are executing commands on remote equipment, or within another program.</li>
51<p>Choose the version of the exercises depending on what OS you are running on your laptop.</p>
52<h1 id="for-laptops-running-windows"><a href="#for-laptops-running-windows"><span class="header-section-number">2</span> For laptops running Windows</a></h1>
53<p>Download the following onto your desktop or into a downloads folder:</p>
55<li>putty.exe (you should already have this)</li>
60<p>from (Or you can try the installer bundle which gets them all)</p>
61<h2 id="generate-an-ssh-publicprivate-key-pair"><a href="#generate-an-ssh-publicprivate-key-pair"><span class="header-section-number">2.1</span> 1. Generate an ssh public/private key pair</a></h2>
62<p>Double-click on <code>puttygen.exe</code></p>
63<p>At the bottom of the dialog box, under &quot;Parameters&quot;:</p>
65<li>Make sure the type of key to generate is &quot;SSH-2 RSA&quot;</li>
66<li>Set the number of bits to 2048</li>
68<p>Click on &quot;Generate&quot;. Move the mouse randomly over the blank area until the progress bar reaches 100%</p>
69<pre><code>    Key comment:        [Your Name &lt;your@email.address&gt;   ]
70    Key passphrase:     [chooose a passphrase             ]
71    Confirm passphrase: [choose same passphrase           ]</code></pre>
72<p>The passphrase is used to keep your private key encrypted on disk. It can be pretty much anything you want and as long as you want - including spaces - but if you forget it, your key becomes worthless. For now pick something that you will easily remember. You can change it at any time you want in the future.</p>
73<p>Click &quot;Save public key&quot;. Give a filename of &quot;; (please save files into the same directory as where the executables are)</p>
74<p>Click &quot;Save private key&quot;. Give a filename of &quot;id_rsa.ppk&quot;</p>
75<p>Use the mouse to highlight all the text in the box &quot;Public key for pasting into OpenSSH authorized_keys file&quot;, and copy it to the clipboard.</p>
76<p>Exit puttygen.</p>
77<p>NOTE: Key generation is a one-off exercise. The more you deploy your public key, the more work it to be if you were to lose it and have to start again with a new one. I suggest you keep a secure backup of it somewhere, e.g. on a CD-ROM that you lock away.</p>
78<h2 id="copy-the-public-key-onto-your-unix-server"><a href="#copy-the-public-key-onto-your-unix-server"><span class="header-section-number">2.2</span> 2. Copy the PUBLIC key onto your Unix server</a></h2>
79<p>You have two ways of doing this.</p>
80<h3 id="copy-paste"><a href="#copy-paste"><span class="header-section-number">2.2.1</span> Copy-paste</a></h3>
81<p>Use putty.exe to make a normal ssh connection to your host as the 'sysadm' user.</p>
82<pre><code>$ cat &gt;&gt;.ssh/authorized_keys
84    *** If the cursor is still at the end of the line, hit Enter ***
85    *** hit ctrl-D ***</code></pre>
86<p>The key consists of one very long line, which looks like</p>
87<pre><code>ssh-rsa &lt;lots of base64 data&gt; &lt;comment&gt;</code></pre>
88<p>As a quick check that it hasn't been corrupted, count the lines in the file:</p>
89<pre><code>$ wc -l .ssh/authorized_keys
901 .ssh/authorized_keys</code></pre>
91<p>If you don't see &quot;1&quot;, then you'll need to fix it (possibly with an editor, or else just rm the file and start again)</p>
92<p>Now logout.</p>
93<h3 id="alternative-way-if-youre-having-problems-with-copy-paste"><a href="#alternative-way-if-youre-having-problems-with-copy-paste"><span class="header-section-number">2.2.2</span> Alternative way (if you're having problems with copy-paste)</a></h3>
94<p>Double-click on psftp.exe. Open a connection to your server, and upload your public key:</p>
95<pre><code>psftp&gt; open
96login as: sysadm;s password: &lt;usual one&gt;
98Remote working directory is /home/sysadm
99psftp&gt; put =&gt; remote:/home/sysadm/
101psftp&gt; quit</code></pre>
102<p>Unfortunately, this public key is not in the format which openssh requires, so now login again using putty.exe, and use the following command to convert it and put it in the right place.</p>
103<pre><code>$ ssh-keygen -i -f &gt;&gt;.ssh/authorized_keys</code></pre>
104<h2 id="login-using-your-private-key"><a href="#login-using-your-private-key"><span class="header-section-number">2.3</span> 3. Login using your private key</a></h2>
105<p>Start putty.exe again. Enter the hostname as usual, but before clicking Open, browse in the left hand column to Connection &gt; SSH &gt; Auth</p>
106<pre><code>    [-] Connection
107         |
108        [-] SSH
109         |  |- Keyex
110         |  |- Auth    &lt;--- CLICK HERE</code></pre>
111<p>Next to &quot;Private key for authentication&quot;, click Browse. Find your id_rsa.ppk file, open it, then click Open to start the connection.</p>
112<p>You should be prompted for your username as before (sysadm), but then instead of being prompted for a password, you are asked for the passphrase for your private key. Enter it, and you should be logged in.</p>
113<p>This is quite painful (both locating the private key and entering the passphrase), so as the final step of the exercise we're going automate it using an agent.</p>
114<h2 id="use-a-passphrase-agent"><a href="#use-a-passphrase-agent"><span class="header-section-number">2.4</span> 4. Use a passphrase agent</a></h2>
115<p>Run <code>pageant.exe</code></p>
116<p>It runs in the background, and adds an icon to your task tray (a PC with a black hat at a jaunty angle). You may need to select &quot;Show hidden icons&quot; to see it.</p>
117<p>Right-click on the icon, and select &quot;Add Key&quot;. Browse to your id_rsa.ppk and open it. You will be prompted for the passphrase - enter it. (If you make a mistake, you'll be prompted again until you get it right)</p>
118<p>Now run putty.exe again, enter your hostname, click Open, and enter your username (sysadm). You should be logged in immediately, with no prompt for either a password or a passphrase!</p>
119<p>Try logging in again. Also try using psftp.exe (when it runs, enter &quot;open; to start a connection). No passphrase is needed until you tell Pageant to forget the private key.</p>
120<h1 id="for-laptops-running-linux-or-bsd-or-osx"><a href="#for-laptops-running-linux-or-bsd-or-osx"><span class="header-section-number">3</span> For laptops running Linux (or BSD or OSX)</a></h1>
121<h2 id="generate-an-ssh-publicprivate-key-pair-1"><a href="#generate-an-ssh-publicprivate-key-pair-1"><span class="header-section-number">3.1</span> 1. Generate an ssh public/private key pair</a></h2>
122<pre><code>$ ssh-keygen -t rsa -b 2048
123Generating public/private rsa key pair.
124Enter file in which to save the key (/home/sysadm/.ssh/id_rsa): &lt;HIT ENTER&gt;
125Created directory &#39;/home/sysadm/.ssh&#39;.
126Enter passphrase (empty for no passphrase): &lt;CHOOSE PASSPHRASE&gt;
127Enter same passphrase again: &lt;SAME PASSPHRASE&gt;
128Your identification has been saved in /home/sysadm/.ssh/id_rsa.
129Your public key has been saved in /home/sysadm/.ssh/
130The key fingerprint is:
132<p>The passphrase is used to keep your private key encrypted on disk. It can be pretty much anything you want and as long as you want - including spaces - but if you forget it, your key becomes worthless. For now pick something that you will easily remember. You can change it at any time you want in the future (using <code>ssh-keygen -p</code>)</p>
133<p>NOTE: Key generation is a one-off exercise. The more you deploy your public key, the more work it to be if you were to lose it and have to start again with a new one. I suggest you keep a secure backup of it somewhere, e.g. on a CD-ROM that you lock away.</p>
134<h2 id="copy-the-public-key-onto-your-unix-server-1"><a href="#copy-the-public-key-onto-your-unix-server-1"><span class="header-section-number">3.2</span> 2. Copy the PUBLIC key onto your Unix server</a></h2>
135<p>The simplest way to copy the public key is with scp:</p>
136<pre><code>$ scp .ssh/</code></pre>
137<p>Note that .ssh/authorized_keys can contain multiple keys, one per line, so on a shared system you might want to append your key instead:</p>
138<pre><code>$ cat .ssh/ | ssh &#39;cat &gt;&gt;.ssh/authorized_keys&#39;</code></pre>
139<h2 id="login-using-your-private-key-1"><a href="#login-using-your-private-key-1"><span class="header-section-number">3.3</span> 3. Login using your private key</a></h2>
140<p>Open an ssh connection to your server as normal:</p>
141<pre><code>$ ssh</code></pre>
142<p>This time, instead of being prompted for your password, you should be prompted for the passphrase on your private key. Enter it. You should be logged in.</p>
143<h2 id="use-a-passphrase-agent-1"><a href="#use-a-passphrase-agent-1"><span class="header-section-number">3.4</span> 4. Use a passphrase agent</a></h2>
144<p>Entering a passphrase every time you connect would be painful, but this isn't necessary if you have an agent which decrypts the private key and keeps it in memory.</p>
145<p>If you are running under a modern graphical environment like Gnome, you probably already got a dialog box prompting you for a passphrase, and this means you're already running an agent. You should be able to logout and login to the remote server, without being prompted for your passphrase again.</p>
146<p>To see what identities (decrypted private keys) your agent has in memory:</p>
147<pre><code>$ ssh-add -l</code></pre>
148<p>To forget all identities:</p>
149<pre><code>$ ssh-add -d</code></pre>
150<p>If you don't have an agent, then you can start a new subshell with ssh-agent as its parent:</p>
151<pre><code>$ ssh-agent bash
152$ ssh-add
153 ... prompted for your passphrase
154$ </code></pre>
155<p>Now the agent will handle future connections for you.</p>
156<p>If you are running an older graphical environment, and you normally start X using <code>startx</code>, then start it using <code>ssh-agent startx</code> instead. Then type 'ssh-add' in an xterm.</p>
157<hr />
158<h1 id="additional-information-not-part-of-exercises"><a href="#additional-information-not-part-of-exercises"><span class="header-section-number">4</span> Additional information [not part of exercises]</a></h1>
159<h2 id="agent-forwarding"><a href="#agent-forwarding"><span class="header-section-number">4.1</span> Agent forwarding</a></h2>
160<p>Using an agent, you can access across multiple ssh hops without having to copy your key or enter your passphrase anywhere.</p>
161<p>If you enable &quot;agent forwarding&quot; when you login to host X, you can then login from X to Y without any prompting (assuming Y has your public key in authorized_keys). The request to authenticate is forwarded securely back along your original ssh session to the agent running on your workstation.</p>
162<p>Under Unix:</p>
163<pre><code>$ ssh -o ForwardAgent=yes user@host</code></pre>
164<p>If you do this frequently, it's easier to configure it in <code>.ssh/hosts</code></p>
165<pre><code>host foo
167user sysadm
168ForwardAgent yes</code></pre>
169<p>Then you only have to type <code>ssh foo</code> to get a connection with those options.</p>
170<h2 id="advanced-x11-forwarding-and-port-forwarding"><a href="#advanced-x11-forwarding-and-port-forwarding"><span class="header-section-number">4.2</span> Advanced: X11 forwarding and port forwarding</a></h2>
171<p>ssh can securely carry arbitrary forwarded TCP connections and X11 graphics.</p>
172<pre><code>$ ssh -L8080:some.where:80
173... while ssh connection is open, a connection to port 8080
174... will be tunneled, and the far end will open a connection to
175... some.where port 80
177$ ssh -X
179$ xclock   # graphical output redirected back through ssh tunnel</code></pre>