Tuesday, October 18, 2016

The B2 and SSH keys, or how not to need a password

** Warning this hack assumes you have some previous experience with SSH and all of my examples below are from a Mac, your mileage will vary on other *unix systems or Windows putty **

While it is great that the B2 allows us to play and look into its inner workings, one of the small issues I found was that every ssh or scp copy to the B2 required me to enter the root brennan password.

Thankfully being a Raspberry Pi based system allows us to use ssh keys to allow us another means of connecting to the B2 which takes us from this (assuming b2 is an alias to your B2):

iMac:~ cm7$ ssh root@b2
root@b2's password: 


to a directly connecting connection:

iMac:~ cm7$ ssh b2
#


First start by creating your keys in your computer users .ssh folder:

cd .ssh
ssh-keygen -y -f id_rsa > id_rsa.pub
(enter a password when prompted)

check they have been created:

# ls -al
total 32
drwxr-xr-x   6 cm7  staff   204 Oct 18 19:56 .
drwxr-xr-x+ 32 cm7  staff  1088 Oct 18 20:13 ..
-rw-r--r--   1 cm7  staff    82 Oct 18 19:56 config
-rw-------   1 cm7  staff   986 Oct 18 20:10 id_rsa
-rw-------   1 cm7  staff   213 Oct 18 20:13 id_rsa.pub
-rw-r--r--   1 cm7  staff  1013 Sep 20 21:16 known_hosts


the 2 id_rsa files are the ones we are interested in.

Next we need to copy the "Public Key" to the B2.  We need to log on to B2 with the user root and brennan password and created our .ssh folder and files:
First we check we are in the root home folder (/root):
# pwd
/root


Then we make or .ssh folder and move into it and check we are their:
# mkdir .ssh 
# cd .ssh
# pwd
/root/.ssh


We next need create our authorised key file and paste in the contents of our user/computer id_rsa.pub file:
touch authorized_keys
ls -al
chmod u+w authorized_keys
vi authorized_keys

(and paste the 1 line of long text)

The file will look something like this:

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAmLmwkzQDjEOW1Rj3TP5NldVDqUODVH9xuYrkeaSkxtdP
J8D9Hz+XAWnGAXdaIkCVOw2YEfHKWSo6befgNxiS+AKS+S+wM/bJpc4qOLe5ozFjZPNRHcw5O8WkgP5g
/wg2BOvxBqSKpsSzvi4rYVRLtl7TLVMyajhELiJ9GqT8f25gr3jFmtuQQIkRES1aC4oL2tHsn529POfP
1lPhh5tb2FbqEpm9L3779ljjkSX7mD4zza3zUckkuAIb5R7KSOrvPnJaEU903hrI0tx5omGyDy+h/2D1
h0aqHanPcU9Ml91ZpMKdpa0+FeVgs2M3LHYTNnvZ76ScV2VtUQwm3YEvjw== alex@smartarse.org

We can now exit the B2:
# exit
Connection to b2 closed.


and ssh back to it with
ssh root@b2


If that did not work you may get warnings about the permissions of the .pub file (this varies from system to system, but quick google search should help) or that the brennan pasword was still required, on my mac I found the following helped by adding the password (of the key, not the brennan):

ssh-add ~/.ssh/id_rsa
Enter passphrase for /Users/cm7/.ssh/id_rsa:
Identity added: /Users/cm7/.ssh/id_rsa (/Users/cm7/.ssh/id_rsa)


The last step for me was then to remove the need for the root@ part, this was a simple edit (or create if one did not exist) of my computers .ssh/config file which now looks like this:

iMac:.ssh cm7$ pwd
/Users/cm7/.ssh
iMac:.ssh cm7$ ls
config        id_rsa        id_rsa.pub    known_hosts
iMac:.ssh cm7$ cat config
Host b2
    HostName b2
    Port 22
    User root

IdentityFile ~/.ssh/id_rsa.pub

iMac:.ssh cm7$


That now allows me to ssh or scp copy files without ever entering a password.

iMac:.ssh cm7$ ssh b2
#


Hope that helps someone. A simple google of "ssh tutorial" can help if this all seems a bit too much. Have Fun.

No comments:

Post a Comment