I routinely use ssh to control my raspberry pi from my pc, but the process of opening the command line, starting a ssh session and logging into the pi takes more time than it should. I wanted to just click a button and have windows automatically start the ssh session and login for me. So after some googling and the like, I decided to write this guide to show you how to set this up for yourself too!
1. Passwordless ssh loginThe first step I needed to take was to get windows to automatically login to the ssh session for me. As it turns out, this is actually a lot easier than I had thought it would be. The way we can do this is with a ssh key that is shared between my computer the raspberry pi that will take the place of any passwords you may have to use.
First of all open up windows powershell which we will use to set up the ssh keys.
Now you can check if you already set up a ssh key by typing the following command into the shell.
ls ~/.ssh
If you get an error it means this hasn't been setup yet. I'd assume that if your reading this guide then this is your first time setting this up but some computers might already have keys setup or something. Anyways, if you don't get an error you already have ssh keys and you can skip the next step.
2. Create a new ssh keyTo create a new ssh key type the following command into the shell:
ssh-keygen
It will then ask you where you'd like to save the key. I recommend leaving this blank and pressing enter to save it to the default location ( ~/.ssh/id_rsa).
Next it will ask you for a passphrase. Remember, the whole point of this was to make it passwordless so just hit enter twice for no password.
Now you can search for the directory again to make sure everything worked.
ls ~/.ssh
If you get an error at this point then something went wrong and you can either try this step again or ask a question in the comments down below and I'll do my best to help. You should see the files id_rsa and id_rsa.pub. The id_rsa file contains your private key. Don't share this one with anyone. The id_rsa.pub file contains your public key. This is the one we're gonna share with the raspberry pi to allow for passwordless ssh.
3. Send your public key to your raspberry piFor this step you're gonna have to know your raspberry pi username (default is 'pi') and your raspberry pi's hostname. You are also going to have to be on the same network as the pi. For finding out your username and hostname, open up the terminal on the raspberry pi and refer to the image below.
The first piece of text before the @ is the username (pi) and the second after the @ is the hostname (raspi).
Now we can send a copy of the public key to your pi.
cat ~/.ssh/id_rsa.pub | ssh USERNAME@HOSTNAME 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'
Type this into the shell on windows but be sure to update 'USERNAME' and 'HOSTNAME' with the values you found for your raspberry pi.
At this point you could check the home directory on your pi and there should be a folder named '.ssh' that contains a copy of your public key.
Now you can type ssh USER@HOSTNAME
into the shell and if you did everything correctly (The first time it might ask you if you want to add this key to your list or something and ask for your pi's password. Just follow along with what it tells you) it should immediately begin a ssh session with your pi without asking for any passwords!
All that's left to do to achieve the 'one click button' is to create a windows batch file that runs the command to start the ssh session. This is actually the easiest part of this tutorial and at the end we will have really achieved the 'one click button' goal we wanted!
4. Create the windows batch fileWindows batch files (*.bat) are a very easy way to run various commands only by running the file.
First, open up notepad or your favorite text editor and copy/paste the following commands into the file and save it as 'SSH.bat'.
As always, be sure to update 'USER@HOST' with your pi's username and hostname.
echo off
title Raspberry Pi SSH
echo opening ssh
ssh USER@HOST
pause
The first command 'echo off' prevents everything in the file from being printed when you run it.
Next, 'title Raspberry Pi SSH' sets the name at the top of the window.
Next, 'echo opening ssh' prints "opening ssh" to the command line.
Next, 'ssh USER@HOST' begins the ssh session with the pi.
And finally, 'pause' just stops the batch file cause we don't need it anymore.
I saved the file to my desktop but really anywhere is fine. To run the file just double click it like opening up any other file and the command prompt should open up and start the ssh session!
You should get a window like this!
5+ (Going further)Not much for the going further section for this project but you definitely try things such as ssh for multiple pi's (or really any other computer supporting ssh). I also wrote this guide for windows users cause that what I have but when I was doing the research for this project I happened upon many guides for both windows and osx. As always, thank you for reading and I'm happy to answer any questions you might have in the comments below!
Comments
Please log in or sign up to comment.