Secure copy protocol (SCP) is a means of securely transferring computer files between a local host and a remote host, or between two remote hosts. It is based on the Secure Shell (SSH) protocol. “SCP” commonly refers to both the Secure Copy Protocol and the program itself.
Step 1 : Install SCP
On Mac and Linux, the SCP command is pre-installed, so no additional installation is usually required. If you try to use the SCP command and get an error like : bash: scp: command not found, it means you need to install SCP on your local or remote server.
Fedora or Red Hat and CentOS:
$ yum -y install openssh-clients
Debian or Ubuntu:
$ apt-get install openssh-client
Step 2 : How to use SCP
SCP Command Syntax:
Before going into how to use the scp
command, let’s start by reviewing the basic syntax.
The scp
command syntax take the following form:
$ scp [OPTION] s_user@SHOST:file1 d_user@DHOST:file2
OPTION
– scp options such as ssh configuration, ssh port, limit, recursive copy …etc.s_user@SHOST:file1
– Source file.d_user@DHOST:file2
– Destination file
Local files should be specified using an absolute or relative path, while remote file names should include a user and host specification.
SCP has a number of options that allow you to control every aspect of its behavior. The most commonly used ones are:
-P
: Specifies the ssh port of the remote host.-p
: Preserves modification times, access times, and modes from the original file.-q
: Quiet mode: disables the progress meter, as well as warning and diagnostic messages from ssh.-C
: Compression enable. Passes the -C flag to ssh to enable compression.-r
: Recursively copy entire directories. Note that SCP follows symbolic links encountered in the tree traversal.
Step 3 : Using SCP
Copy Files and Directories Between Two Systems
- Local File to a Remote System
$ scp file1.txt [email protected]:/data/test
Where file1.txt
is the name of the file to be copied, ruser
is the remote server user, and 192.168.1.22
is the server IP address. The path to the directory you want to copy the file to is /data/test. If no remote directory is specified, the file will be copied to the remote user’s home directory.
You will be prompted to enter the user password before the transfer can begin.
[email protected]'s password:
file1.txt 100% 0 0.0KB/s 00:00
When the filename is removed from the destination location, the file is copied with the original name. If you want to save the file under a different name, use the following syntax:
$ scp file1.txt [email protected]:/data/test/newfile.txt
If SSH on the remote host is listening on a port other than the default 22, use the -P argument to specify the port:
$ scp -P 2222 file1.txt [email protected]:/data/test
The command to copy a directory is similar to the command to copy files. The only difference is that for recursive, you must use the -r flag.
Use the -r option to copy a directory from a local to a remote system:
$ scp -r /data/test [email protected]:/data/testRemote
- 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 system.
To copy a file named test.txt
from a remote server with IP 192.168.1.22
, for example, use the following command:
$ scp [email protected]:/data/test.txt /data
If you have not configured a Private key for SSH authentication to the remote machine, you will be prompted to enter the user password.
Copy a File Between Two Remote Systems
Unlike rsync, you do not need to log in to one of the servers to transfer files from one to another remote machine when using SCP.
The following command will copy the file /folder/file.txt from remote host web1.com
to remote host web2.com
‘s directory /files.
$ scp user1@web1.com:/folder/file.txt [email protected]:/files
The passwords for both remote accounts will be requested. Data will be sent directly from one remote host to another.
Use the -3 option to route traffic through the machine from which the command is issued:
$ scp -3 user1@web1.com:/folder/file.txt [email protected]:/files
Conclusion
This guide teaches you how to copy files and directories using the SCP command.
You may also want to configure a Private key for SSH authentication so that you can connect to your Linux servers without entering a password.