Transferring files between hosts with SCP
Using the Secure Copy (SCP) utility in Linux lets you securely copy files to and from remote hosts, and it's very easy to use.
Sections
- Introduction
- SCP Command Syntax
- SCP Options
- Copying files and directories to a remote host
- Copying from a remote host to a local machine
- Transferring files between two remote hosts
- References
Introduction
Secure Copy is a command line utility that transfers files between the local host and a remote host via the SSH protocol. You will be prompted for the remote user’s password, or you can use an authorized SSH key.
SCP Command Syntax
scp -OPTION user@SourceIP:file.txt user@DestinationIP:file.txt
SCP Options
-r
: Recursively copy entire directories, also follows symbolic links-P
: Specifies an SSH port on destination host, use if destination uses SSH port other than 22-p
: Copied files keep modification times, access times, permissions and modes-v
: Verbose mode; prints additional debugging messages about transfer progress-q
: Quiet mode; disables the progress bar and warning/diagnostic messages-C
: Compresses sent data for faster transfer speeds
Copying files and directories to a remote host
Copy a file from local host to a remote host:
scp file.txt [email protected]:/directory
If preferred, use hostnames instead of IP addresses:
scp file.txt remote-user@hostname:/directory
You can also copy a file with a different name at the destination:
scp file.txt [email protected]:/directory/newfilename.txt
If the remote host uses a port besides the default 22 for SSH, specify it with the -P option:
scp -P 2222 file.txt [email protected]:/directory
To copy a directory, you'll need to do so recursively with the -r option:
scp -r /directory [email protected]:/directory
Copying from a remote host to the local machine
To copy from the local host rather than to it, put the remote host’s info first and the local host info after:
scp [email protected]:/directory/file.txt /local-directory
Transfer files between two remote hosts
With SCP its even possible to transfer between two other systems, as long as your local machine has access to both via SSH or password.
scp [email protected]:/directory/file.txt [email protected]:/directory/file.txt