- SCP Command Syntax
- How to Use the SCP Command in Linux
- Transfer Local File to Remote Server
- Transfer File from Remote Server to Local Machine
- Transfer Local Directory to Remote Server Recursively
- Transfer Directory from Remote Server to Local Recursively
- Transfer Multiple Files to Remote Servers
- Increase Transfer Speed by Enabling Compression
- Specify Different SSH Port
- Preserves Permissions, Modes, and Access Time of Files
- Use Identify File in SCP Command
- Conclusion
The scp
(Secure Copy) command uses SSH to transfer data from one host to another and uses SSH’s same authentication and security. However, the command relies on SSH for data transfer, requiring an ssh key or password to authenticate on the remote systems.
When transferring data with scp
, both the files and password are encrypted so that anyone snooping on the traffic doesnโt get anything sensitive. So, this is one of the most secure ways to transfer data on a network.
The scp
command in Linux can be used in 3 ways:
- To copy from a remote server to a local machine.
- Also, to copy from a local machine to a remote server.
- To copy from a remote server to another remote server.
SCP Command Syntax
The syntax for the scp
command is:
scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
Code language: CSS (css)
Some of the most widely used scp
command options include:
-P
: Specify server SSH port-p
: Preserves permissions, modes, and access time of files (note the lower-case)-q
: Quiet mode, donโt display progress or messages-C
: Compress the data during transmission-r
: Recursive – include subdirectories and their contents-i
: Identity file or private key
How to Use the SCP Command in Linux
Transfer Local File to Remote Server
Copy file.txt
from the current directory of the local system to the remote serverโs /tmp
directory.
scp file.txt user@remotehost:/tmp/
Code language: JavaScript (javascript)
Transfer File from Remote Server to Local Machine
The following command will copy /tmp/file.txt
from the remote server to the local machine under the user’s home directory.
scp user@remotehost:/tmp/file.txt /home/user
Code language: JavaScript (javascript)
Transfer Local Directory to Remote Server Recursively
You can use the -r
option in the scp
command in Linux to recursively copy the entire directory from one system to another.
The following command will copy the /home/user/myfiles
directory from the local machine to the remote serverโs /tmp
directory.
scp -r /home/user/myfiles user@remotehost:/tmp/
Code language: JavaScript (javascript)
Transfer Directory from Remote Server to Local Recursively
The following command will copy /tmp/serverfiles
directory from the remote server to the local machine under the user’s home directory recursively.
scp -r user@remotehost:/tmp/serverfiles /home/user
Code language: JavaScript (javascript)
Transfer Multiple Files to Remote Servers
In the following example, the files file1.txt
and file2.txt
from the source host are copied to the remote server’s /tmp
directory.
scp file1.txt file2.txt user@remotehost:/tmp/
Code language: JavaScript (javascript)
Increase Transfer Speed by Enabling Compression
You can increase the transfer speed by enabling the compression using the -C
option. It will automatically allow compression at the source and decompression at the destination host.
The following command will copy the /home/user/myfiles
directory from the local machine to the remote serverโs /tmp
directory recursively with enabled compression.
scp -r -C /home/user/myfiles user@remotehost:/tmp/
Code language: JavaScript (javascript)
Specify Different SSH Port
There can be cases where the SSH port is changed on the destination host, so using the scp command in Linux, you can specify the SSH port number using the -P
option.
The following command will copy file.txt
from the current directory of the local system to the remote serverโs /tmp
directory using port 2222.
scp -P 2222 file.txt user@remotehost:/tmp/
Code language: JavaScript (javascript)
Preserves Permissions, Modes, and Access Time of Files
Use the -p
option in the scp
command to preserve permissions, access time, and modes while copying files.
The following command will copy file.txt
from the current directory of the local system to the remote serverโs /tmp
directory and will keep its properties.
scp -p file.txt user@remotehost:/tmp/
Code language: JavaScript (javascript)
Use Identify File in SCP Command
When using an SSH Key instead of a password during the SSH session, the -i
flag allows you to select the file from which the identity (private key) for public-key authentication is read.
The following command will copy file.txt
from the current directory of the local system to the remote serverโs /tmp
directory using the my_second_indent.pem
private key file.
scp -i my_second_indent.pem file.txt user@remotehost:/tmp/
Code language: JavaScript (javascript)
Conclusion
In this tutorial, you learned how to use the scp
command on Linux to copy files and directories. This is especially useful as a replacement for FTP, which is inherently insecure by default.
You may also want to set up an SSH key-based authentication and connect to your Linux servers without entering a password.
For more about the scp
command in Linux, consult its manual page.