202.1 Transferring files using SSH/SCP
This article discusses how to transfer files using SSH/SCP.
Requirements:
- (2) Linux-powered computers, or in my case, I am using two Raspberry Pis
- Admin (sudo) rights on both computers
- Both computers can communicate with each other (ping)
SCP is the quickest to copy files using the command line or python on the Raspberry Pi. It utilizes part of the SSH protocol known as “Secure Copy.”
I will use two Raspberry Pis in this example: a Raspberry Pi Zero W (Rover) and a Raspberry Pi 4 Model B (Mission Control). This example shows how to send files from the Rover back to Mission Control.
Manual Copy Steps:
- Find the IP Addresses of each of your Raspberry Pis. Here is the Guide on how to find your IP.
Mission Control: 172.16.40.84
Rover: 172.16.40.84 - From a terminal or command line type:
scp test.jpg bill@172.16.40.84:/home/bill/test.jpg
The example above, will copy the file test.jpg file located in the current folder to the server 172.16.40.84 to the /home/bill/ folder and call the file test.jpg logging into the server as bill.
- You will then be asked if connecting to the server is okay.
The authenticity of host '172.16.40.84 (172.16.40.84)' can't be established. ECDSA key fingerprint is SHA256:h9/UT11W2C7ZfcPwcNd9ab6PpSkyIxGnA3DqZaxiuWY. Are you sure you want to continue connecting (yes/no/[fingerprint])?
Answer yes to this question
- Next, you will enter your password.
bill@172.16.40.84's password:
Type in your password
- Now, wait while the magic happens and the file is copied from one computer to another.
We have now learned to copy a file from one Pi to another. What we want to do is copy a file via a Python script. Because at the time of writing this article, I would like to take a photo of the solar test unit and copy that image to Mission Control. To do that, we need to set up a key pair. A key pair is like an encrypted saved password.
- Generate your Key Pair using the ssh-keygen tool.
From the Terminal prompt type:ssh-keygen
- It will now ask you where you put the RSA key pair
Generating public/private rsa key pair. Enter file in which to save the key (/home/bill/.ssh/id_rsa):
Your folder will be different because your username is different. Press enter to accept this folder.
- Press enter to NOT enter a passphrase. We don’t want a passphrase because we need our Python script to connect without a passphrase.
Enter passphrase (empty for no passphrase): Your identification has been saved in /home/bill/.ssh/id_rsa Your public key has been saved in /home/bill/.ssh/id_rsa.pub The key fingerprint is: SHA256:M+jQMTO8kHanSolRoZI9eFf7pjdeR2D2bpsEDhotdog bill@Solar The key's randomart image is: +---[RSA 3072]----+ | ... . . | | . . ! o= | | . = %B=.. | | . Nope | | . X S.. | | = YEP | | . o.+ o . | | ..+ = | | .+o* | +----[SHA256]-----+
- It is time to copy the key you created above to Mission Control, thus creating a key pair.
ssh-copy-id -i ~/.ssh/id_rsa 172.16.40.84
After pressing enter, you will see the following:
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/bill/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
- Now you will enter your password. This is the password we use to log on to the server.
Note: “*****************” is where I typed in my passphrase.bill@172.16.40.84's password: ***************** Number of key(s) added: 1 Now try logging into the machine, with: "ssh '172.16.40.84'" and check to make sure that only the key(s) you wanted were added.
- Now test it using the SSH command to log on to Mission Control. What should happy is we log on without typing in our password.
ssh 172.16.40.84
If everything is working well, you should log in without entering a password.
Linux MissionControl 5.15.61-v7l+ #1579 SMP Fri Aug 26 11:13:03 BST 2022 armv7l The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Sat Dec 31 09:50:00 2022 from 172.16.40.52 bill@MissionControl:~ $
You will notice that your prompt has changed from the hostname of your rover to the hostname of MissionControl.
Note: The hostname is the part after the @.