SSHing into your Raspberry Pi

From ESE205 Wiki
Jump to navigation Jump to search

Secure Shell (SSH) is a way to remotely access your Pi from any computer connected to the same network as your Pi; this means you can have it running in your room while accessing it with your laptop elsewhere on campus. Unfortunately, WashU’s network system makes this more complicated than normal, so we need to set up SMTP on your Pi in order to email its IP address to you.

For the initial set up of the pi, follow this guide here. You will need a monitor, mouse, and keyboard for the rest of this tutorial but afterwards they will not be needed once you’re connected on another device. These items can be found in Urbauer 015-- Please use only the monitors not connected to a computer, and return everything you use to its original place after finishing. If your Pi is not a 3 or Zero W, you will also need an Ethernet cable.

Enable SSH

  • On the device you want to use to access your Pi, download and install the SSH client PuTTY: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
  • Boot up your Pi. If this is the first time, you may be presented with the raspi-config menu; otherwise open the command prompt and type sudo raspi-config to access it. Enable SSH and then exit with finish.
  • On your Pi, connect to wustl-guest-2.0 on the top right corner of your screen. Then hover over the network icon to find the Pi’s public IP address.
Ssh-Wifi ip.png
  • Open PuTTY. Where it says host name, type the IP address, omitting the “/” and everything afterwards. Make sure your device is connected to the same network. For port number use 22 and for connection type select the SSH option. Click “open.”
Ssh-Putty.png
  • Note that if you have OS X (Mac), ssh is installed by default. To use it, open Terminal and type ssh pi@<<IP address>> and replacing the <<IP address>> with the IP address of your Pi. Then, enter raspberry as the password and follow the prompts to change your password.
  • A command terminal will pop up. It may take a while for any text to show up. When prompted, type pi as your login and raspberry as your password, and then you should have access to the Pi’s terminal! It will then suggest you change your password, which is advisable to do as soon as possible for security reasons.
Ssh-pi.png
  • When ready, type sudo halt to shut down or sudo reboot to restart. Always halt before pulling the plug.

Emailing the IP address

Upon future reboots the Pi may use different addresses, preventing you from SSHing into it when you don’t have access to a monitor to check it. Now we will configure the Pi to email its address to you every time it starts up. This setup is for an installation using Raspbian Stretch, the procedure may need to be modified for other distributions.


  • Update the package list:
 sudo apt-get update
  • Install ssmtp:
 sudo apt-get install ssmtp
  • Install mailutils:
 sudo apt-get install mailutils
  • Type sudo nano /etc/ssmtp/ssmtp.conf. In the document that appears, change/add the following lines:
 
 AuthUser=ese205smtp@gmail.com
 AuthPass=PLEASE_ASK_JIM
 FromLineOverride=YES
 mailhub=smtp.gmail.com:587
 UseSTARTTLS=YES
 
Ssh-Ssmtp.png
  • Save and exit by hitting ctrl+X, then y, then enter.
  • Ensure Sendmail is turned off by using the following lines in the command prompt:
 
 $ sudo service sendmail stop
 $ sudo mkdir /root/.bakup
 $ sudo mv /usr/sbin/sendmail /root/.bakup
 $ sudo ln -s /usr/sbin/ssmtp /usr/sbin/sendmail
 
  • You should be able to send emails from your Pi now from the address ese205smtp@gmail.com. To test this, type echo "This is a test" | mail -s "Test" your-email@wustl.edu and check your inbox.
  • Type
 sudo nano /etc/ip_emailer.sh

to create a new script. In the new file, write the lines

 
 #!/bin/bash
 sleep 30
 hostname -I | mail -s "RasPi IP" your-email@wustl.edu
 
Ssh-Ip-emailer.png
  • Save and exit with ctrl+X.
  • Make the script executable with
 chmod +x /etc/ip_emailer.sh
  • To schedule this script to be run upon reboot, type
 sudo crontab –e
  • At the bottom of the script, add the line
 @reboot /etc/ip_emailer.sh

Save and exit.

Ssh-Crontab.png
  • Reboot the Pi. You should receive an email containing its IP address you can now use to SSH into your Pi from anywhere on campus! If you do not receive an email, the Pi may be having trouble connecting to the network; try moving it to a different location or trying again later.

Troubleshooting

If your Pi suddenly stops emailing you one day and you are sure the network is fine, and running echo "This is a test" | mail -s "Test" your-email@wustl.edu in the terminal gives you errors, try reinstalling mailutils and ssmpt:

 
 $ sudo apt-get remove mailutils
 $ sudo apt-get remove ssmtp
 $ sudo apt-get install mailutils
 $ sudo apt-get install ssmtp
 

And test it again.

Beyond the Command Prompt

If you wish to access the full Pi desktop and not just the command prompt from your laptop, follow this guide to set up a VNC server.

Setup Script

This script was written to automate the process above. It was written for the Jessie distribution and may need to be modified for other versions.

File:Setup ip emailing 2.bash