Difference between revisions of "SSHing into your Raspberry Pi"

From ESE205 Wiki
Jump to navigation Jump to search
(Fixed code formatting and small typos)
 
(13 intermediate revisions by 4 users not shown)
Line 5: Line 5:
 
== Enable SSH ==
 
== 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
 
* 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.
+
* 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 <font face="Courier">sudo raspi-config</font> to access it. Enable SSH and then exit with <font face="Courier">finish</font>.
 
* 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.  
 
* 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.  
 
[[File:Ssh-Wifi ip.png|frameless|center]]
 
[[File:Ssh-Wifi ip.png|frameless|center]]
 
* 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.”
 
* 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.”
 
[[File:Ssh-Putty.png|frameless|center]]
 
[[File:Ssh-Putty.png|frameless|center]]
* 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.
+
* Note that if you have OS X (Mac), ssh is installed by default. To use it, open Terminal and type <font face="Courier">ssh pi@<<IP address>></font> and replacing the <<IP address>> with the IP address of your Pi. Then, enter <font face="Courier">raspberry</font> 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 advise you to change your password, which is advisable to do as soon as possible for security reasons.  
+
* A command terminal will pop up. It may take a while for any text to show up. When prompted, type <font face="Courier">pi</font> as your login and <font face="Courier">raspberry</font> 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.  
 
[[File:Ssh-pi.png|frameless|center]]
 
[[File:Ssh-pi.png|frameless|center]]
* When ready, type “sudo halt” to shut down or “sudo reboot” to restart. Always halt before pulling the plug.
+
* When ready, type <font face="Courier">sudo halt</font> to shut down or <font face="Courier">sudo reboot</font> to restart. Always halt before pulling the plug.
  
 
== Emailing the IP address ==
 
== 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.
+
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.
* Install ssmtp by typing in the command prompt
+
 
   sudo apt-get update && apt-get install ssmpt
+
 
* Install  
+
* Update the package list:
   sudo apt-get install mailutils
+
   <font face="Courier">sudo apt-get update</font>
* Type “sudo nano /etc/ssmtp/ssmtp.conf”. In the document that appears, change/add the following lines:
+
* Install ssmtp:
 +
  <font face="Courier">sudo apt-get install ssmtp</font>
 +
* Install mailutils:
 +
   <font face="Courier">sudo apt-get install mailutils</font>
 +
* Type <font face="Courier">sudo nano /etc/ssmtp/ssmtp.conf</font>. In the document that appears, change/add the following lines:
 +
  <font face="Courier">
 
   AuthUser=ese205smtp@gmail.com
 
   AuthUser=ese205smtp@gmail.com
   AuthPass=ese205rulz
+
   AuthPass=PLEASE_ASK_JIM
 
   FromLineOverride=YES
 
   FromLineOverride=YES
 
   mailhub=smtp.gmail.com:587
 
   mailhub=smtp.gmail.com:587
 
   UseSTARTTLS=YES
 
   UseSTARTTLS=YES
 +
  </font>
 
[[File:Ssh-Ssmtp.png|frameless|center]]
 
[[File:Ssh-Ssmtp.png|frameless|center]]
 
* Save and exit by hitting ctrl+X, then y, then enter.
 
* 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:
 
* Ensure Sendmail is turned off by using the following lines in the command prompt:
 +
  <font face="Courier">
 
   $ sudo service sendmail stop
 
   $ sudo service sendmail stop
 
   $ sudo mkdir /root/.bakup
 
   $ sudo mkdir /root/.bakup
 
   $ sudo mv /usr/sbin/sendmail /root/.bakup
 
   $ sudo mv /usr/sbin/sendmail /root/.bakup
   $ sudo ln -s /usr/local/ssmtp/sbin/ssmtp /usr/sbin/sendmail
+
   $ 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.
+
  </font>
 +
* You should be able to send emails from your Pi now from the address ese205smtp@gmail.com. To test this, type <font face="Courier">echo "This is a test" | mail -s "Test" your-email@wustl.edu</font> and check your inbox.
 
* Type  
 
* Type  
   sudo nano /etc/ip_emailer.sh
+
   <font face="Courier">sudo nano /etc/ip_emailer.sh</font>
 
to create a new script. In the new file, write the lines
 
to create a new script. In the new file, write the lines
 +
  <font face="Courier">
 
   #!/bin/bash
 
   #!/bin/bash
   ifconfig -a | mail -s "RasPi IP" your-email@wustl.edu
+
   sleep 30
 +
  hostname -I | mail -s "RasPi IP" your-email@wustl.edu
 +
  </font>
 
[[File:Ssh-Ip-emailer.png|frameless|center]]
 
[[File:Ssh-Ip-emailer.png|frameless|center]]
 
* Save and exit with ctrl+X.
 
* Save and exit with ctrl+X.
 
* Make the script executable with  
 
* Make the script executable with  
   chmod +x /etc/ip_emailer.sh
+
   <font face="Courier">chmod +x /etc/ip_emailer.sh</font>
 
* To schedule this script to be run upon reboot, type  
 
* To schedule this script to be run upon reboot, type  
   sudo crontab –e
+
   <font face="Courier">sudo crontab –e</font>
 
* At the bottom of the script, add the line  
 
* At the bottom of the script, add the line  
   @reboot /etc/ip_emailer.sh
+
   <font face="Courier">@reboot /etc/ip_emailer.sh</font>
 
Save and exit.
 
Save and exit.
 
[[File:Ssh-Crontab.png|frameless|center]]
 
[[File:Ssh-Crontab.png|frameless|center]]
 
* 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.
 
* 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 <font face="Courier">echo "This is a test" | mail -s "Test" your-email@wustl.edu</font> in the terminal gives you errors, try reinstalling mailutils and ssmpt:
 +
  <font face="Courier">
 +
  $ sudo apt-get remove mailutils
 +
  $ sudo apt-get remove ssmtp
 +
  $ sudo apt-get install mailutils
 +
  $ sudo apt-get install ssmtp
 +
  </font>
 +
And test it again.
  
 
== Beyond the Command Prompt ==
 
== Beyond the Command Prompt ==
Line 56: Line 77:
  
 
== Setup Script ==
 
== 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]]
 
[[File: Setup_ip_emailing_2.bash]]
 +
  
 
[[Category:HowTos]]
 
[[Category:HowTos]]
 +
[[Category:Raspberry_Pi]]

Latest revision as of 16:28, 1 October 2018

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