SCP (secure copy) is a Linux command line utility that allows you to securely copy files and directories between two locations.
Using scp, you can copy a file or directory:
- From local to remote system.
- From the remote system to your local machine.
- Between two remote systems from the local system.
When transferring data using scp, the files and password are encrypted, so tracking traffic will not work.
In this article, we will show you how to use the scp command with practical examples and detailed explanations of the most common scp options.
SCP command syntax
Before using the command
scp, let’s start by looking at the basic syntax.
The scp command expressions take the following form:
scp [OPTION] [[email protected]]SRC_HOST:]file1 [[email protected]]DEST_HOST:]file2
OPTION– scp options like cipher, ssh config, SSH port, limitation, recursive copy, etc.
[[email protected]]SRC_HOST:]file1– Original file.
[[email protected]]DEST_HOST:]file2– Destination file
The local file must be specified using an absolute or relative path, while the remote file names must include the user and host specification.
scp provides a number of parameters that control every aspect of its behavior. The most commonly used options are:
-PPoints to the ssh port of the remote host.
-pSaves file changes and access times.
-qUse this option if you want to suppress the progress bar and error-free messages.
-C… This option will force scp to compress the data as it is sent to the destination computer.
-rThis option tells scp to copy directories recursively.
Before you start
ssh to transfer data, so it requires an SSH key or password to authenticate to remote systems.
:) in scp distinguish between local and remote hosts.
To be able to copy files, you must have at least read permissions on the source file and write permissions on the target system.
Be careful when copying files with the same name and location on both systems, scp will overwrite files without warning.
When transferring large files, it is recommended to run the scp command inside a screen or tmux session in Linux.
Copying files and directories between two systems using SCP
Copy local file to remote system using scp command
To copy a file from local to remote system, run the following command:
scp file.txt [email protected]:/remote/directory
file.txt this is the name of the file we want to copy, user
remote_username on a remote server,
10.10.0.2 is the server’s IP address. IN
/remote/directory this is the path to the folder where you want to copy the file, if you do not specify the remote directory, then the file will be copied to the user’s remote home directory.
You will be prompted for a user password and the transfer process will begin.
[email protected]'s password: file.txt 100% 0 0.0KB/s 00:00
If there is no file name in the target location, the file is copied with the original name. If you want to save the file under a different name, you need to specify a new name:
scp file.txt [email protected]:/remote/directory/andreyex.txt
If SSH on the remote host is listening on the default port 22, then you can specify the port using the argument
ssh -P 2322 file.txt [email protected]:/remote/directory
The command for copying a directory is the same as for copying files. The only difference is that you have to use the flag
-r for recursion.
Copy folder from local to remote system use option
scp -r /local/directory [email protected]:/remote/directory
Copying remote file to local system using scp command
To copy a file from a remote system to your local system, use the remote location as the source and the local location as the destination.
For example, to copy the file
file.txt from a remote server from IP
10.10.0.2 run the following command:
scp [email protected]:/remote/file.txt /local/directory
If you have not yet set up passwordless SSH to log into the remote computer, you will be prompted for the user’s password.
Copying a file between two remote systems using scp command
Unlike rsync, with scp you do not have to log into one of the servers to transfer files from one to another remote machine.
The following command copies the file
/files/file.txt from remote host
host1.com to directory
/files on the remote host
scp [email protected]:/files/file.txt [email protected]:/files
You will be prompted to enter passwords for both remote accounts. Data will be transferred immediately from one remote host to another.
To route traffic through the machine, use the option
scp -3 [email protected]:/files/file.txt [email protected]:/files
In this article, you learned how to use the scp command to copy files and directories.
Please disable your ad blocker or whitelist this site!