Ubuntu Server 18.04 – Transferring files with scp

File Management Commands in Linux

A useful alternative to rsync is the Secure Copy (SCP) utility, which comes bundled with OpenSSH. It allows you to quickly copy files from one node to another. While rsync also allows you to copy files to other network nodes via SSH, SCP is more practical for one-off tasks; rsync is geared more toward more complex jobs. If your goal is to send a single file or a small number of files to another machine, SCP is a great tool you can use to get the job done. To utilize SCP, we’ll use the scp command. Since you most likely already have OpenSSH installed, you should already have the scp command available. If you execute which scp, you should receive the following output:

/usr/bin/scp 

If you don’t see any output, make sure that the openssh-client package is installed.

Using SCP is very similar in nature to rsync. The command requires a source, a target, and a filename. To transfer a single file from your local machine to another, the resulting command would look similar to the following:

scp myfile.txt jdoe@192.168.1.50:/home/jdoe 

With this example, we’re copying the myfile.txt file (which is located in our current working directory) to a server located at 192.168.1.50. If the target server is recognized by DNS, we could’ve used the DNS name instead of the IP address. The command will connect to the server as user jdoe and place the file into that user’s home directory. Actually, we can shorten that command a bit:

scp myfile.txt jdoe@192.168.1.50: 

Notice that I removed the target path, which was /home/jdoe. I’m able to omit the path to the target, since the home directory is assumed if you don’t give the scp command a target path. Therefore, the myfile.txt file will end up in /home/jdoe whether or not I included the path. If I wanted to copy the file somewhere else, I would definitely need to call out the location. Make sure you always include at least the colon when copying a file, since if you don’t include it, you’ll end up copying the file to your current working directory instead of the target.

The scp command also works in reverse as well:

scp jdoe@192.168.1.50:myfile.txt  . 

With this example, we’re assuming that myfile.txt is located in the home directory for the user jdoe. This command will copy that file to the current working directory of our local machine, since I designated the local path as a single period (which corresponds to our current working directory). Using scp in reverse isn’t always practical, since you have to already know the file is where you expect it to be before transferring it.

With our previous scp examples, we’ve only been copying a single file. If we want to transfer or download an entire directory and its contents, we will need to use the -r option, which allows us to do a recursive copy:

scp -r /home/jdoe/downloads/linux_iso jdoe@192.168.1.50:downloads 

With this example, we’re copying the local folder /home/jdoe/downloads/linux_iso to remote machine 192.168.1.50. Since we used the -r option, scp will transfer the linux_iso folder and all of its contents. On the remote end, we’re again connecting via the user jdoe. Notice that the target path is just simply downloads. Since scp defaults to the user’s home directory, this will copy the linux_iso directory from the source machine to the target machine under the /home/jdoe/downloads directory. The following command would’ve had the exact same result:

scp -r /home/jdoe/downloads/linux_iso jdoe@192.168.1.50:/home/jdoe/downloads 

The home directory is not the only assumption the scp command makes, it also assumes that SSH is listening on port 22 on the remote machine. Since it’s possible to change the SSH port on a server to something else, port 22 may or may not be what’s in use. If you need to designate a different port for scp to use, use the -P option:

scp -P 2222 -r /home/jdoe/downloads/linux_iso jdoe@192.168.1.50:downloads 

With that example, we’re connecting to the remote machine via port 2222. If you’ve configured SSH to listen on a different port, change the number accordingly.

Although port 22 is always the default for OpenSSH, it’s common for some administrators to change it to something else. While changing the SSH port doesn’t add a great deal of benefit in regards to security (an intensive port scan will still find your SSH daemon), it’s a relatively easy change to make, and making it even just a little bit harder to find is beneficial. We’ll discuss this further in Chapter 15, Securing Your Server.

Like most commands in the Linux world, the scp command supports the verbose mode. If you want to see how the scp command progresses as it copies multiple files, add the -v option:

scp -rv /home/jdoe/downloads/linux_iso jdoe@192.168.1.50:downloads 

Well, there you have it. The scp command isn’t overly complex or advanced, but it’s really great for situations in which you want to perform a one-time copy of a file from one node to another. Since it copies files over SSH, you benefit from its security, and it also integrates well with your existing SSH configuration. An example of this integration is the fact that scp recognizes your ~/.ssh/config file (if you have one), so you can shorten the command even further. Go ahead and practice with it a bit, and in the next section, we’ll go over yet another trick that OpenSSH has up its sleeve.

Comments are closed.