SCP (secure copy) is a command-line application that allows you to copy files and directories between two places securely.
You can use SCP to copy a file or directory:
- From your local computer to a remote computer.
- Transferring data from a remote system to your local system.
- Between two remote systems.
When using SCP to send data, both the files and the password are encrypted to ensure that anyone to eavesdrop on the stream does not obtain anything sensitive.
This guide will teach you how to use the SCP command by providing you with examples of its application and exhaustive descriptions of its most frequently used options.
Table of Contents
SCP Syntax
Let’s study the SCP command’s basic syntax before delving into its application.
The correct syntax for a SCP command looks like this:
scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
OPTION - scp options such as cipher, ssh configuration, ssh port, limit, recursive copy …etc.
[user@]SRC_HOST:]file1 - Source file.
[user@]DEST_HOST:]file2 - Destination file
If you’re working with a local file, use an absolute or relative path, but be sure to include the user and host information when sharing files remotely.
There are many configuration options available for SCP that allow you to alter its behaviour in any way you like. Most frequently selected are:
-P - Defines the SSH port on the remote host.
-p - maintains the modification and access times of files.
-q - If you want to disable the progress bar and non-error warnings, choose this option.
-C - With this option, scp is compelled to compress the data as it travels to the target machine.
-r - SCP is instructed to copy folders recursively using this option.
Before you start using SCP, read the below.
The SCP command uses ssh to transfer data, therefore it needs to authenticate on the remote systems with an ssh key or password. SCP distinguishes between local and remote sites using the colon (:). You must have at least read permissions on the source file and write permission on the target system in order to copy files. SCP will overwrite data without giving you a chance to stop it, so take care when copying files that have the same name and location on both systems.
It is advised to use a screen or tmux session when using the scp or any command to transmit huge files.
Copy Directories and Files Using SCP, between Two Systems
The SCP command copies a local file to a remote system. Run the following command to copy a file from a local system to a remote one:
scp abc.txt [email protected]:/remote/directory
The name of the file we want to copy is abc.txt, the user on the remote server is remote username, and the IP address of the server is 10.10.10.2. The path to the directory you wish to copy the file to is in the /remote/directory field. The file will be copied to the remote user’s home directory if a remote directory is not specified. The user password must be entered before the transfer process can begin.
The file is copied with its original name when the filename is left out of the destination location. You must give the new file name if you wish to save the file with a different name:
scp abc.txt [email protected]:/remote/directory/xyz.txt
You can specify the port using the -P parameter if SSH on the remote host is listening on a port other than the usual 22:
scp -P 2322 abc.txt [email protected]:/remote/directory
Similar to copying files, you can copy directories with the same command. The -r flag must be used for recursive, which is the only difference.
Use the -r option to copy a directory from a local system to a remote one:
scp -r /local/directory [email protected]:/remote/directory
SCP Command to Copy a Remote File to a Local System
Use the remote location as the source and the local location as the destination to copy a file from a remote system to a local one.
Run the following command, for instance, to copy a file named abc.txt from a remote server with the IP 10.10.10.2:
scp [email protected]:/remote/abc.txt /local/directory
You will be prompted for the user password if you haven’t configured the remote machine’s SSH login to be password-free.
Using the SCP Command to Copy a File Between Two Remote Systems
With SCP, you can move files from one remote system to another without logging onto a server, unlike rsync. The file /files/abc.txt from the remote host host1.com will be copied to the directory /files on the remote host host2.com by the following command.
scp [email protected]:/files/abc.txt [email protected]:/files
The login information for both remote accounts will be required from you. Direct data transfers will take place between two remote hosts. Use the -3 option to send traffic through the device where the command is being issued:
scp -3 [email protected]:/files/abc.txt [email protected]:/files
Conclusion
You learned the SCP command’s use to copy files and directories in this tutorial. Additionally, you might prefer to configure SSH key-based authentication so that you can access your Linux servers without having to input a password. By listing all of your connections in the SSH config file, you can streamline your process if you frequently log in to the same systems.