Scp is a vital tool for any system administrator. Much like cp, its local counterpart, scp is used to copy files or directories. Unlike cp, scp works across systems, transferring files from or to a remote server, or even between remote servers. It also does so securely, encrypting and optionally compressing files in transit.
Scp is a component of any complete SSH installation, so a server running OpenSSH or another SSHD qualifies. This includes almost any virtual or dedicated server, since SSH is the primary access method for connecting securely to remote servers.
Scp is a complex command, but its basics are easy to memorize. Here is the basic form of any invocation. Note that you can use the -P flag to set the port used.
scp source_file_path destination_file_path
Say you’re running SSHD on a nonstandard port, which is a good security practice. The scp utility connects to your SSHD port, normally 22. Here is how you’d use a different port:
scp -P 2222 source_file_path destination_file_path
Here are some examples of its use. Say you wish to copy /root/example.txt from your local host to a remote server, placing it in /home/user.
scp /root/example.txt firstname.lastname@example.org:/home/user/
Now let’s download example.txt from a remote server into your local /root directory.
scp email@example.com:/home/user/example.txt /root/
You can also specify multiple files. This command invocation uploads both foo.txt and bar.txt to /home/user on a remote host.
scp foo.txt bar.txt firstname.lastname@example.org:/home/user/
You cal also upload entire directories. Here’s how you might upload the contents of /root/backup, recursively, to the /root/backup folder on a remote host.
scp -r /root/backup/ email@example.com:/root/backup/
Some files, such as text and binaries, compress well in transit. If CPU power is cheap but bandwidth is more scarce, the -C flag will transmit data compressed, decompressing it transparently on the other end.
scp -rC /root/backup/ firstname.lastname@example.org:/root/backup/
Sometimes, transmitting file content isn’t enough. You may wish to preserve other metadata, such as file update times and modes. The -p flag achieves this.
scp -p /root/backup/ email@example.com:/root/backup/
Whether performing a simple transfer or architecting a complex backup strategy, the scp command is essential for any good system administrator. With the above foundations in place, it should be simple to create scp command to accomplish any of these tasks. If this guide was helpful to you, kindly share it with others who may also be interested.