Introduction
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 SCP
command.
Prerequisites
- 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
The 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 *
sign.
For example, /~/*.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 user1@host1.com:/files/test.txt user2@user2.com:/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:
tmux -V
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).
Managing Permissions
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
Some common 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.
For example:
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/
Conclusion
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.
Next you should also read
How to Copy Files and Directories in Linux
May 21, 2019
Want to learn how to copy files in Linux OS? This guide will show you how to use the Linux commands to copy…
How to Use mkdir Command to Make or Create a Linux Directory
April 8, 2019
The mkdir command in Linux allows users to create or make new directories. mkdir stands for “make directory.”…
How To Use grep Command In Linux/UNIX
March 28, 2019
This guide details the most useful grep commands for Linux / Unix systems. After going through all the…
How to Find Files in Linux With the Find Command
December 19, 2018
The find command is a useful command-line tool in Linux. It allows you to use the terminal to search for a…
Web Servers,SysAdmin,Bare Metal Servers
How to Restart or Reboot Linux Server from the Command Line
October 22, 2018
There's a reason that tech support asks you if you've rebooted your Linux server. It's cliched but true -…
Author
Dejan Tucakov
Dejan is the Technical Writing Team Lead at phoenixNAP with over 6 years of experience in Web publishing. Prior to joining phoenixNAP, he was Chief Editor of several websites striving to advocate for emerging technologies. He is dedicated to simplifying complex notions and providing meaningful insight into data center and cloud technology.