The SCP (Secure Copy) command is a method of encrypting the transmission of files between Unix or Linux systems. It’s a safer variant of the cp (copy) command.
SCP includes encryption over an SSH (Secure Shell) connection. This ensures that even if the data is intercepted, it is protected.
The SCP utility can serve in the following use cases:
- Copy files within the same machine.
- Copy files from a local host to a remote host.
- Copy files from a remote host to a local host.
- Copy files between two different remote servers.
This guide will walk you through how to transfer or copy files using the
- A secure shell login on the server
- (optional) Root access on both the client and server
- A secure shell login on the server system
How to Securely Copy Files Using SCP
Copy File From Local to Remote Server with SCP
scp command allows the use of wildcards.
Use the tilde character
~/ to stand for the user’s home/user directory.
You can specify a string of text with the
/~/*.txt would direct SCP to copy all files in the home directory that end in .txt.
Typically, you do not need to specify the location for a file in your current directory. If you are in your /home/user directory, and you want to copy the test.txt file to the server, you can enter the following:
scp test.txt username2@destination:/location2
To copy all .txt files into username2’s home directory, enter the following:
scp *.txt username2@destination_host:/~/
If you only specify the destination directory, SCP will leave the filename as-is.
To change the filename, define a new filename in the destination:
scp test.txt username2@destination_host:/user/home/user1test.txt
This example copies the test.txt file from the local machine, then save it as user1test.txt in the user directory of the destination system.
If a remote system is configured to listen to SSH requests on a port other than the default Port 22, Use the
–P switch to specify the port:
scp -P 1234 test.txt user2@destination_host:/location2/
This copies test.txt from your local system to the destination host using Port 1234.
Copy From One Remote Host to Another
You aren’t limited just to connecting between your local machine and a remote server.
To copy from one remote system to another:
scp email@example.com:/files/test.txt firstname.lastname@example.org:/files
This would replicate the test.txt file from the /files directory on host1.com to the /files directory on /host2.com. (The system will prompt you to enter the password for user1 and user2 before the operation can complete.)
Copying Large Files With SCP
If you’re copying large files, run the command in a terminal multiplexer, such as tmux.
If the operation is interrupted, the multiplexer will allow you to resume the copy without having to start over.
You can check whether your system has tmux installed by running the following in a terminal:
Note: If you do not have tmux on your system, learn how you can install and use tmux.
SCP Usage Considerations
The SCP command does not check the destination location before writing. Any files in the destination with the same name will be overwritten without notification.
You will be prompted for a password when you hit enter.
Use the password for the user on the remote system(s).
On the source system, you’ll need to use an account with read access to the file(s) you want to copy.
On the destination system, you’ll need to use an account with write access to the directory where the file(s) will be saved. If you run into errors while copying, you can try a root user account to troubleshoot permissions.
SCP Command Options
The basic syntax of SCP is:
scp [options] username1@source_host:/location1/file1 username2@destination_host:/location2/file2
scp command options include:
-P– Specify server SSH port
-p– Preserve the timestamp for modification and access (note the lower-case)
-q– Quiet mode, don’t display progress or messages (will still show errors)
-C– Compress the data during transmission
-r– Recursive – include subdirectories and their contents
The section immediately following the options is the source (path) of the file you want to copy. You can copy from your system to a remote system, or vice-versa.
The next section specifies the location the file is being copied to.
scp user@local_system:/home/user/test.txt admin@remote_system:/home/user
This would copy the test.txt document from the user directory on the local system, and place a copy in the admin’s user account directory on the remote system.
Another SCP example:
To copy a file from a remote host to a local host:
scp user@from_host:file.txt /local/directory/
In this guide, you have learned what the
scp command is and how to use it to secure the transmission of files.
This is especially useful as a replacement for FTP, which is inherently insecure by default. The secure copy protocol also follows regular command-line and SSH functionality, helping to create a seamless command set for managing files between Linux machines.