SSHing into your Raspberry Pi (Buster)

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 msmtp and msmtp-mta:
 sudo apt-get install msmtp msmtp-mta
  • Install mailutils:
 sudo apt-get install mailutils
  • Type sudo nano /etc/msmtprc. In the empty document add the following lines:
 
 #Generics
 defaults
 auth     on
 tls      on
 
 tls_trust_file /etc/ssl/certs/ca-certificates.crt
 # log location
 logfile     ~/.msmtp.log
 
 #gmail
 account     gmail
 host        smtp.gmail.com
 port        587
 
 from        root@raspi-buster
 user        ese205smtp@gmail.com
 password    ASK_JIM (Not the actual password... email or ask Jim for it)
 
 account default : gmail
 
Emailerconfig.png
  • Save and exit by hitting ctrl+X, then y, then enter.
  • If you're on campus you may experience difficulties with the wustl-2.0 WiFi network. To remove this as a default network, first open the file containing the WiFi networks:
 
 sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
 
  • Then delete the lines
 
 network = {
 ssid="wustl-2.0"
 bla bla bla ...
 }
 
  • Now save and exit the file using Ctrl+o ENTER Ctrl+x.
  • 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
 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.

If your Pi stops emailing you and you are off the school's network, and running echo "This is a test" | mail -s "Test" your-email@wustl.edu in the terminal gives you authentication errors, you may need to add a new device to the ese205smtp@gmail.com account. To do this, log onto the ese205smtp gmail account and then visit https://g.co/allowaccess so that Google expects a new device to log into the email account. After you visit this page test the emailer 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 Buster distribution and may need to be modified for other versions.

File:Setup ip emailing 2.bash