Connecting to Raspberry Pi via SSH after clean reinstall on Mac OSX

Posted by in Mac OSX, Softwares, Technology, Tutorials

Blog Post

 

Anyone who has worked with SSH, knows how convenient it is to manage a remote machine. If you’re using(or planning to use) a Raspberry Pi as a headless home file server or as a Time Machine backup device, you’ll be accessing it through SSH often. Setting up a headless RPi server is supposed to be ‘set up everything once & forget about it‘. But in practice, this isn’t the case always. If your Pi reboots because of some software issue or power failure without properly unmounting the file system, the SD card(in worst cases, the attached external drive also!) of the Pi will get corrupted, it might not happen always, but like any other Linux machine, file system corruptions can happen to the Pi if the working file system is not unmounted properly.Though there are many ways recover the corrupted file systems in desktop versions of Linuxes, there isn’t a proper way to do this on the Pi (as of now).

Suppose you used to connect to the Pi via SSH in Mac. One day, your Pi died & you reinstalled the OS on Pi. You tried connecting to your Pi like you used to do. Instead of the usual output, you’re greeted with the following window!  😯

Bash

 

There is nothing nasty nor there is an attack! This happens because your RPi’s RSA key has changed because you’ve reinstalled the OS. Hence, the stored key in Mac OS no longer matches with the one sent by the RPi. The OS thinks that its a Man In The Middle Attack & refuses the connection. This is one of the common errors you might face when you’re dealing with SSH. If you’re familiar with SSH & Mac OS, you might not find this a big issue. But if you’ve started tinkering with an RPi & SSH recently, you might find this big.

How to resolve?

The easy way is to delete the known_hosts file in the .ssh directory of your folder, that folder & file are hidden. You need an application called Desktop Utility to unhide the hidden files & folders. You can find it here. Download & drag the application into your Applications folder. Its a tiny little utility that runs on the menu bar & its handy in situations where you need to see the hidden files.

To unhide the files & folders, run the application, click on the Desktop utility icon(gear icon) on the menu bar & select show invisible files. You’re good to go! You will see a lot of hidden files & folders all around. I recommend you not to tinker with any of them!

Desktop Utility

 

Next, to remove the known_hosts file, you’ll have to go the folder Computer > Users > yourname > .ssh the .ssh folder will be hidden & you might need to open it by right clicking & selecting Open in a new tab to open the folder. There, you’ll find our known_hosts file. Simply remove it after making a copy of it just in case you need it.

 

Make a copy

 

Do not remove this file if you’re using SSH to connect to other devices, just copy the file to desktop, open it up with a text editor & remove the line starting with your RPi’s static IP address in your network, like this:

192.168.2.149 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC.......

Save the file as known_hosts to desktop, copy it the modified file to the .ssh folder & authenticate when asked! That’ll do it! In case you deleted it, you’ll have to re add all those devices again when connecting.

Now, click on the desktop utility & select Hide hidden files to hide them. There you have it! You can connect to your RPi like you used to do earlier. You’ll need to add the host list of known hosts since you are connecting it to your mac for the first time  😀

$ ssh <pi's static address in your network> -l pi

Keys

 

Update: There is another way to do this & its too simple! Just fire up the Terminal, enter the following & hit Enter!

rm -f ~/.ssh/known_hosts

The saying Where there is a shell, there is a way is always true when you’re dealing with *NIX machines! 😀