SCP is a command-line utility that allows you to securely copy files and directories between two locations.
With scp, you can copy a file or directory:
When transferring data with scp, both the files and password are encrypted.
The scp command syntax:
Local files should be specified using an absolute or relative path, while remote file names should include a user and host specification.
scp provides a number of options:
The scp command requires ssh for data transfer, so it requires an ssh key or password to authenticate on the remote systems.
The colon (:) is how scp distinguish between local and remote locations.
To be able to copy files, you must have at least read permissions on the source file and rite permission on the target system.
Be careful when copying files that share the same name and location on both systems, scp will overwrite files without warning.
To copy a file from a local to a remote system run the following command:
Where file.txt is the name of the file we want to copy, remote_username is the user on the remote server, 10.10.0.2 is the server IP address. The /remote/directory is the path to the directory you want to copy the file to. If you don’t specify a remote directory, the file will be copied to the remote user home directory.
You will be prompted to enter the user password, and the transfer process will start.
remote_username@10.10.0.2's password: file.txt 100% 0 0.0KB/s 00:00
Omitting the filename from the destination location copies the file with the original name.
If you want to save the file under a different name, you need to specify the new file name:
If SSH on the remote host is listening on a port other than the default 22 then you can specify the port using the -P argument:
The command to copy a directory is much like as when copying files. The only difference is that you need to use the -r flag for recursive.
To copy a directory from a local to remote system, use the -r option:
To copy a file from a remote to a local system, use the remote location as a source and local location as the destination.
For example to copy a file named file.txt from a remote server with IP 10.10.0.2 run the following command:
If you haven’t set a passwordless SSH login to the remote machine, you will be asked to enter the user password
When using scp you don’t have to log in to one of the servers to transfer files from one to another remote machine.
The following command will copy the file /files/file.txt from the remote host host1.com to the directory /files on the remote host host2.com.
You will be prompted to enter the passwords for both remote accounts. The data will be transfer directly from one remote host to the other.
To route the traffic through the machine on which the command is issued, use the -3 option: