This tool is used to copy data to or from a remote host over SSH. Because it uses SSH, data you send to an off-site backup will be encrypted in transit, protecting its confidentiality. Like SSH, scp uses TCP port 22 by default. The following is an example of copying a file to a remote host:
# scp file.txt user@host:/home/dir
Given that scp defaults to ssh, we need to make sure that we have the password (or even better, key-based authentication) in place for it to work. Let’s assume we have a remote machine with the IPv4 address 63.32.106.149, and we want to copy a file there from our local machine:
$ scp file.txt remote_host:/tmp/
Here,
- Source is the file file.txt in the current directory.
- Destination is the /tmp directory on machine remote_host
scp Command Examples in Linux
1. Copy a local file to a remote host:
# scp path/to/local_file remote_host:path/to/remote_file
2. Use a specific port when connecting to the remote host:
# scp -P port path/to/local_file remote_host:path/to/remote_file
3. Copy a file from a remote host to a local directory:
# scp remote_host:path/to/remote_file path/to/local_directory
4. Recursively copy the contents of a directory from a remote host to a local directory:
# scp -r remote_host:path/to/remote_directory path/to/local_directory
5. Copy a file between two remote hosts transferring through the local host:
# scp -3 host1:path/to/remote_file host2:path/to/remote_directory
6. Use a specific username when connecting to the remote host:
# scp path/to/local_file remote_username@remote_host:path/to/remote_directory
7. Use a specific ssh private key for authentication with the remote host:
# scp -i ~/.ssh/private_key local_file remote_host:/path/remote_file