7. Connecting Remotely

7.1. SSH via remot3.it

remot3.it services allow you to connect easily and securely to your Pi from a mobile app, browser window and a terminal. It allows you to control remote computers (such as the Pi) using TCP hosts such as SSH. You will be able to connect to your Pi from laptop or desktop at home. The free remot3.it account allows for multiple registered services and 8 hours connections on up to 1 concurrent service(s).

  1. To configure weaved in our Pi, first we need to open an account on the remot3.it website. You can register from your laptop or desktop.
  2. Once you have an account: from your Pi terminal, we need to install weaved (which is the precursor on which remot3.it is based) to be able to connect our Pi. To install it:
sudo apt-get -y install weavedconnectd
  1. Then we will open the weaved installer to link your Pi to your remot3.it account:
sudo weavedinstaller
  1. Enter your remot3.it account username and password. Next, you will see this menu:
../_images/remot3-installer-menu-00.png
  1. Then enter a name for your Pi (e.g. “pi01”). You can make it up, but remember to make a name easy for you to identify a specific Pi in case you have more than one attached to the weaved service:
../_images/remot3-installer-menu-01.png
  1. Initially you won’t have any Weaved services installed, so the upper part is empty. Enter 1 to attach Weaved to an existing TCP service (host) on your Raspberry Pi. You should now see the following screen:
../_images/remot3-installer-menu-02.png
  1. Enter 1 for SSH.
  2. Next, we accept the default port (y).
../_images/remot3-installer-menu-03.png
  1. The installer confirms your choice and asks you to give this connection a name:
../_images/remot3-installer-menu-04.png
  1. You will now return to the main menu, where you can see your Weaved Service Connection installed, then enter 3 to exit.
../_images/remot3-installer-menu-05.png

Your Pi is now ready to run headless (without a direct connection to a screen), we just have to connect with it over SSH on our laptop to control it from the terminal. We have created two access guides, one for Linux and Mac Users and the other for Windows (Accessing from Windows).

7.1.1. Accessing from Linux or macOS

  1. We will now see how to access using your laptop to your Pi from the terminal. First, if you login to your remot3.it account, you will get a list of your devices:
../_images/remot3-logged_in.png
  1. In your case you will have just one item. When you click on the name of you device, a pop-up will open:
../_images/remot3-pop-up-01.png
  1. Click on the name of your ssh service and then “Confirm”.
  2. A second pop-up will appear:
../_images/remot3-pop-up-02.png

We copy the command after For pi username, in this example it is: ssh -l pi proxy54.yoics.net -p 30015. For you it will be different.

  1. Then, paste the command in your laptop or desktop terminal app. (Optional alternative app for Mac)
  2. The terminal is going to show you this message:
../_images/ssh-authenticity-check.png

Type yes.

  1. Then, you will be prompted to enter a password, you should enter the password of the pi user of your Pi. By default it is raspberry but you should have changed it in an earlier chapter (User password).

You will see on your laptop’s terminal that now you are user pi. You are connected from your laptop to your Pi. As long as your Pi is connected to the internet, you can remotely log into it and control it, so you don’t need to use the display and mouse anymore.

For some more details on remote connections see Alternative ways to connect via SSH.

Tip

To manage remote terminal sessions we suggest you use Screen, check out the guide later in the section SSH using Screen.

7.1.2. Accessing from Windows

If your computer operative system is Windows, to access remotely you will need to install PuTTY, which is a free implementation of SSH and Telnet for Windows and Unix platforms.

  1. To download it click here.
../_images/windows-putty.jpg
  1. Once downloaded, proceed with the standard installation.
  2. Once installed double click on the putty.exe and you will see a window that looks like the one below:
../_images/windows-putty-connect.jpg
  1. Then, if you login to your remot3.it account, you will get a list of the services linked to your devices:
../_images/remot3-logged_in.png
  1. In your case you will have just one item. When you click on the name of you device, a pop-up will open:
../_images/remot3-pop-up-01.png
  1. Click on the name of your ssh service and then “Confirm”.
  2. A second pop-up will appear:
../_images/remot3-pop-up-02.png
  1. Insert the server address and port obtained from remot3.it into Putty and connect!

Note

Rather than typing in ssh -l pi <server> -p <port>, you just need to insert the server url and port from remot3.it.

  1. When asked for username and password, use your Pi username and password to log-in.

Note

This is not weaved username and password. The default password is raspberry but you should have changed it in an earlier chapter (User password).

../_images/windows-putty-login.png

To exit your putty session, type “exit” and enter.

Tip

To manage remote terminal sessions we suggest you use Screen, check out the guide later in the section SSH using Screen.

For some more details on remote connections see Alternative ways to connect via SSH.

7.2. SSH using Screen

Remember you can be connected to your Pi for up to 8 hours using remot3.it, after that time you have to connect again to your account and do the same access procedure we explained in the previous sections. Therefore we will show you how a virtual terminal can help you when you are working remotely on your Pi.

Screen is a full-screen software program allows you to use multiple windows (virtual VT100 terminals) in Unix. It offers a user to open several separate terminal instances inside a one single terminal window manager.

The screen application is very useful, if you are dealing with multiple programs from a command line interface and for separating programs from the terminal shell. It also allows you to share your sessions with others users and detach/attach terminal sessions.

7.2.1. When to use Screen?

One of the advantages of Screen, is that you can detach it. Then, you can restore it without losing anything you have done on the Screen. One of the typical scenario where Screen is of great help is when you are in the middle of SSH session and you want to download a file, update the operative, or transfer a big file to your RPi. The process could be 2 hours long. If you disconnect the SSH session, or suddenly the connection lost by accident, then the download process will stop. You have to start from the beginning again. To avoid that, we can use screen and detach it.

7.2.2. Installing Screen

Screen allows you to use multiple windows (virtual VT100 terminals) in Unix. If your local computer crashes, or you are connected remotely and lose the connection, the processes or login sessions you establish through screen don’t get lost. To install Screen you can enter the following command on the Pi terminal:

sudo apt-get -y install screen

7.2.3. How to use Screen

  • When you are in your terminal, you can create a screen or virtual terminal e.g. we will name the screen mysession:

    ../_images/screen-terminal.png
  • Then you will be automatically attached to the mysession screen, that from now on we will call just “screen”. You can now execute commands and work in the terminal without worrying to loose your work:

    ../_images/screen-attached.png
  • You can detach from the “screen” by pressing Ctrl-A and then d. Once detached we will be returned to our Pi terminal outside any screen session. To check the list of active screens: screen -ls

    ../_images/screen-list.png
  • We get a list with all the screen IDs. If we want to attach to a particular screen we can enter screen -r name_of_terminal like in the example below:

    ../_images/screen-attaching.png

7.2.4. Basic commands to work with Screen

Screen command Description
screen -S name_of_terminal Assigning name to the virtual terminal or screen session
screen -ls List all the virtual sessions or screens opened
screen -X -S name_of_terminal quit Kill an specific virtual terminal.
screen -r name_of_terminal Attach to the virtual terminal or screen
Press Ctrl-A and d Detach from virtual terminal or screen
Press Ctrl-A and K This command will leave and kill the virtual terminal or screen
Press Ctrl-A and n Switching to the next virtual terminal or screen
Press Ctrl-A and p Switching to the previous virtual terminal or screen

For additional commands check out the Screen Cheatsheet

7.3. Alternative ways to connect via SSH

We already know how to connect through remot3.it service, but we know that the connection lasts 8 hours and it allows us to work on one terminal session at a time. Therefore, with the help of remot3.it and another commands we can connect to or Pi for longer and using multiple terminals. In this section we are going to connect to our Pi using its IP address.

If you do not know what is an IP address, please go to the this video for a quick explanation. The IPs can be dynamic or static, but what is the difference? When a device is assigned a static IP address, the address does not change. Most devices use dynamic IP addresses, which are assigned by the network when they connect and change over time (which is the case for our Pi on the Imperial-WPA).

7.3.1. Get IP address from remot3.it

remot3.it displays the external IP of the devices you have registered. You can get your Pi’s one in the External IP Tab:

../_images/remot3-ip.png

Note

If you are connected with your laptop to the same network of your RPi the internal and external IP addresses will be the same like in the example above.

7.3.2. Get IP address from the terminal

We can use a command to check the different internet connections available on our system: ifconfig or ifconfig -a.

$ ifconfig

This command allows to know the IP addresses assigned to our Pi. The wlan0, indicates the status of the WiFi, and eth0 shows the status of the Ethernet (wired) connection. In the next screen shoot shows an example of a Pi connected to the internet using the ethernet port. The red oval shows where to find the IP address assigned to the Pi for the Ethernet connection.

../_images/ifconfig.png

You can find your IP address for the WiFi connection in the corresponding wlan0 inet addr field.

7.3.3. Connect knowing your IP

Once you know the IP (e.g. your IP is 192.31.123.122), you can access using your laptop terminal to the Pi like this:

$ ssh pi@192.31.123.122

Tip

The syntax for this command is ssh username@ipaddress. You may want to log in using a different username to the default pi if you created one.

Note

The IP addresses at Imperial are dynamic (most IPs are dynamic), meaning they are constantly changing and being reallocated as needed. It could be that your IP changes in a couple of hours, a day, or a bit longer, so be prepared to have to repeat the steps above to rediscover what your new IP address is.

7.4. VNC GUI control

Todo

Using VNC for remote GUI control will be added later.

7.5. Transferring files

7.5.1. Using terminal

If are programming on your laptop and you want to transfer your code to test it in your Pi, you can use a number of different methods:

  • Secure Copy (scp)
  • SSH File Transfer Protocol (sftp).
  • Secure File Transfer Protocol (ftps)

7.5.1.1. Secure Copy (scp)

scp - Securely copy a file from one location to another.

The general syntax is as follows: scp copy_from copy_to

The locations are written relatively. So if you were to copy a file from one place on your local computer to anther place, you would simply provide the path:

scp /home/pi/Desktop/myprogram.py /home/pi/Desktop/myfolder/

Which would copy the file myprogram.py to a folder within the same location.

Note

When you use SSH to remotely ‘log in’ to a computer such as a Pi, then all the commands you then type into your terminal are considered ‘local’.

If you wanted to copy something from your computer to the Pi then that would be considered a local-to-remote copy.

For that you would need to :

  1. Get the path to the file locally on your computer.
  2. Get the path to the location on the Pi you would like to save it.
scp /Users/username/Desktop/program.py pi@192.168.1.10:/home/pi/Desktop

As you can see we copied the file /Users/username/Desktop/program.py from the local computer to a remote computer (which is why we need to prefix it with the username and IP address pi@192.168.1.10) in the location specified /home/pi/Desktop.

We can even copy a file back by reversing the order of the commands:

scp pi@192.168.1.10:/home/pi/Desktop/program.py /Users/username/Desktop/

Hint

If you want to copy a folder (not an individual file) then you need to add the recursive flag to the command. This tells the terminal that you want to copy the folder and all its sub-contents to the new location. i.e.

scp /Users/username/Desktop/Gizmo_Folder/ pi@192.168.1.10:/home/pi/Desktop/

7.5.1.2. SFTP

  1. First we log into a session with the Pi using the correct username and IP address
sftp pi@192.168.1.1
  1. Once establish the connection through SFTP, we can navigate around using cd (change directory) and pwd (print working directory) and ls (to list contents of current directory).
  2. Once we have the a file to download from the remote computer (Pi):
get /path/to/file

Or for a folder:

get -r /path/to/directory/
  1. To transfer files on our (local) computers to the remote (Pi) we can put:
put /path/of/local/file

The same flags that work with get apply to put. So to copy an entire local directory:

put -r /path/of/local/directory/

Note

More details and examples of SFTP can be found here.

7.5.2. Using Software

Instead a terminal, we can use to transfer files using a software that mounts any remote server storage as a local disk in the Finder.app on Mac and the File Explorer on Windows. We suggest: