Difference between revisions of "UsingSSH"

From SourceWiki
Jump to navigation Jump to search
Line 57: Line 57:
  
 
<pre>
 
<pre>
scp ~/.ssh/id_rsa.pub brian:~/.ssh/from-dylan.pub
+
scp ~/.ssh/id_rsa.pub brian:~/.ssh/from-host.pub
 
</pre>
 
</pre>
  
Line 79: Line 79:
 
# keep a copy of your '''authorized_keys''' file, if you have one
 
# keep a copy of your '''authorized_keys''' file, if you have one
 
# add your public key to the list of authorized keys
 
# add your public key to the list of authorized keys
 +
# exit from the remote host
  
 
<pre>
 
<pre>
 
chmod 700 ~/.ssh
 
chmod 700 ~/.ssh
 
+
cd ~/.ssh
 +
chmod 600 *
 +
cp authorized_keys authorized_keys.bak
 +
cat from-host.pub >> authorized_keys
 +
exit
 
</pre>
 
</pre>
  

Revision as of 17:10, 10 June 2009

Using SSH to connect to machines and to move data

Introduction

Using Key-Pairs

Creating the Keys

First, let's create a key-pair. Start by typing:

ssh-keygen

You will see a message like:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/gethin/.ssh/id_rsa):

The default filename suggested is fine, so accept it by hitting return.

Next you are prompted for a passphrase:

Enter passphrase (empty for no passphrase):

Think of a strong, yet memorable one and enter it. (One tip is to think of a phrase, saying, song lyric etc. For example "One small step for man, one giant leap for mankind." Then take the first letters from each word, perhaps substituting digits for letters, to create the passphrase, "Oss4mogl4m.") You will be prompted for your passphrase twice:

Enter same passphrase again: 

When the key-pair creation is completed, you will get some lines of text as confirmation, such as:

Your identification has been saved in /home/gethin/.ssh/id_rsa.
Your public key has been saved in /home/gethin/.ssh/id_rsa.pub.
The key fingerprint is:
37:7a:b3:81:e2:0e:fa:5e:b2:df:84:a5:fb:f9:e6:f7 

Distributing Your Public Key

Now that you have your key-pair, you can copy your public key to any machine that you would like to connect to from the machine that you are currently logged into. When the keys are setup correctly, you will be able to connect without typing your password. Hurrah for the convenience!

The first step is to ensure that the permissions on your files are correct. The following commands will take care of this:

cd ~/.ssh
chmod 600 *
cd ~
chmod 700 .ssh

Now, let's copy your public key to the remote host of interest. In this case, I want to be able to login to a machine called brian, from one called dylan:

scp ~/.ssh/id_rsa.pub brian:~/.ssh/from-host.pub

(I'm assuming here that your username matches on the two machines. If not, you can prepend your username to the destination string, i.e. <username>@brian:~/.ssh/from-dylan.pub.)

Now, login to the remote host in the normal way:

ssh brian

where you will be prompted for your password, as per usual:

gethin@brian's password:

The following commands will:

  1. ensure that your file permissions are correct on the remote host
  2. keep a copy of your authorized_keys file, if you have one
  3. add your public key to the list of authorized keys
  4. exit from the remote host
chmod 700 ~/.ssh
cd ~/.ssh
chmod 600 *
cp authorized_keys authorized_keys.bak
cat from-host.pub >> authorized_keys
exit

Using ssh-agent

ssh-agent bash

Using ssh-keychain