If you’re connecting to a server through SSH and there’s a file that you want to download from it, how do you do that? In this post I’ll show you show to use
scp to download and upload files from a remote server through SSH.
I’m going to assume that you have an SSH key generated already. If you don’t, this guide from GitHub is fantastic. Follow that first.
You’ll also need a server to SSH into. It could be a server hosting a web application, a Raspberry Pi, or another computer on your home network. If you don’t have any of those and still want to try this out, I recommend using AWS EC2 to create the smallest instance size they have.
Downloading A File With SCP
To download a file, first you’ll have to locate it. Start by SSHing into your remote server. I’m going to be using a Raspberry Pi running Ubuntu on my home network.
1 ssh firstname.lastname@example.org
You can replace the IP address portion of this command with a URL. For instance, many AWS EC2 instances will have a URL that you connect to like this:
1 ssh email@example.com
Once you’re in the server, use
ls to locate the file you want to transfer. You can also use
ls -lha to show the file sizes in a human readable way.
The last thing you’ll need to do is to figure out the full directory this file is in. For instance, this won’t work:
myfolder/myfiles.tar.gz. You’ll need this instead:
/home/ubuntu/myfolder/myfiles.tar.gz. You can type the command
pwd—print working directory—when you’re in the right folder to find this full path.
Back On Your Computer
Now that you’ve found the file and the full path to it, type
exit to return to your computer. You invoke the SCP command like this:
1 scp username@remote_host:source_file destination_file
So for my Raspberry Pi where the host name is
ubuntu, I would do this to copy the file to the current directory on my computer:
1 scp firstname.lastname@example.org:/home/ubuntu/myfolder/myfiles.tar.gz ./
Full Download Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ~/myfolder ❯ ssh email@example.com Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-1018-raspi aarch64) ... ubuntu@raspberrypi:~$ ls file1 file2 myfiles.tar.gz ubuntu@raspberrypi:~$ pwd /home/ubuntu ubuntu@raspberrypi:~$ exit logout Connection to 192.168.1.2 closed. ~/myfolder ❯ ls ~/myfolder ❯ scp firstname.lastname@example.org:/home/ubuntu/myfiles.tar.gz ./ myfiles.tar.gz ~/myfolder ❯ ls myfiles.tar.gz
Uploading A File With SCP
To upload a file to a server, you just switch the arguments to SCP:
1 scp source_file username@remote_host:destination_file
So to upload that same
myfiles.tar.gz file back to my Raspberry Pi, I would use:
1 scp ./myfiles.tar.gz email@example.com:/home/ubuntu/myfolder/
Full Upload Example
1 2 3 4 5 6 7 8 9 10 11 12 ~/myfolder ❯ scp ./myfiles.tar.gz firstname.lastname@example.org:/home/ubuntu/myfolder/ myfiles.tar.gz ~/myfolder ❯ ssh email@example.com Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-1018-raspi aarch64) ... ubuntu@raspberrypi:~$ ls myfolder/ myfiles.tar.gz
Getting files from and onto remote servers might seem impossible if you’ve never done it before. But
scp is one of those things that, once you use it, you’ll always have at least a faint memory of its existence. And once you know there’s a way to do it, the only problem left is remembering the syntax.