Smart Cities - Transferring Files using Secure Copy

From Sensors in Schools
Jump to navigation Jump to search

Introduction

  • The fastest way to copy files between two computers is to use Secure Copy (scp)[1][2].
  • This method is executed from the Terminal and hence eliminates the overhead of a GUI application.
  • It is both fast and secure.
  • Files can be transferred in both directions.

Prerequisites

  • In this lesson it is assumed that Secure Shell (SSH) has been enabled on both computers.

Enabling SSH on a Raspberry Pi

  • In the Terminal enter the command sudo raspi-config

  • In the raspi-config menu select Interface Options

  • Select SSH (Secure Shell) and then select Enable SSH

  • SSH will now be enabled. There is no need to reboot the computer.
  • Exit raspi-config

Copy File From Local Host to Remote Server

  • In this example we will copy a file from a local host computer to a remote computer on the same network [3].
    • 192.168.1.105 Local computer IP address
    • 192.168.1.120 Remote computer IP address
    • On the Host computer create a new directory named test with the command mkdir test
    • Enter the directory using cd and create an empty file named file_to_send.txt with the command touch file_to_send.txt
  • To send the file enter the command scp file_to_send.txt pi@192.168.1.120:/home/pi/
  • You will be asked to enter the pi user password on the Remote computer

  • The file will now appear on the Remote computer in the /home/pi directory

Copy Files From Remote to Local

  • In this example we will copy a file from a remote computer to a local computer (the one issuing the commands) on the same network.
    • 192.168.1.105 Local computer IP address
    • 192.168.1.120 Remote computer IP address
    • A file named remote_file.txt has been created on the remote computer.
  • To send the file from the remote computer to the local computer (user home directory) enter the command scp pi@192.168.1.120:/home/pi/remote_file.txt .
  • The dot (.) at the end means to copy to file to the user home directory of the local computer.
  • You will be asked to enter the password of the pi user on the Remote computer.

Copy Files From Remote to Local using Public and Private keys

  • Secure Copy (scp) works the same way but with a few Options added to the instruction.
    • -P the Port may need to be specified
    • -i the path to the private key on the Local computer
    • IP address of the remote computer
    • path and file name for the file on the remote computer
  • In this example the file on the remote computer is located at /home/pi/solar_python/data.db

  • To copy the file from the remote computer enter the command scp -P 1099 -i /Users/edmond/.ssh/solar-pi pi@110.174.170.224:/home/pi/solar_python/data.db .
    • -P 1099 is the port
    • -i /Users/edmond/.ssh/solar-pi is the location of the Private key on the Local computer
    • pi@110.174.170.224 is the IP address of the Remote computer
    • :/home/pi/solar_python/data.db is the path to the file on the Remote computer
    • . (dot) indicates to copy the file to the current working directory on the Local computer. We could specify any path and also a new file name for the copied file.