If you’re a newbie to Linux, you might be wondering how to use the SCP command. SCP is a command that transfers files between two systems by using SSH. Essentially, you’ll use an SSH key to authenticate the remote system and transfer data between them. SCP requires both write and read permissions on the target file, so make sure you have these permissions before you start using the command. Using the SCP command in a Linux session is also recommended. So, in this tutorial, I will show you how to use the SCP command to transfer files!
Secure copy (SCP) is a command-line utility that lets you securely copy files and directories from one location to another.
SCP allows you to copy files or directories:
- You can move from your local system to remote systems.
- You can connect from a remote location to your local network.
- Two remote systems can be linked to your local system.
SCP is used to transfer data. Both the password and files are encrypted so that no one can spy on traffic.
This tutorial will demonstrate using SCP commands through concrete examples and detailed explanations for the most popular SCP options.
SCP Command Syntax
The SCP Command Syntax is used to copy files between two remote machines. When you are using the command, you specify the source file, which is located on your current host, and the destination path, which is located on the remote host. The command is very similar to copy cp, except that you must specify a port number other than the default port 22. This allows the SCP service to bypass your local computer, and the copy process is carried out directly between the two remote servers.
Let’s check the syntax before we get into the SCP commands.
The SCP command syntax takes the following form.
scp [OPTION] [[email protected]]SRC_HOST:]file1 [[email protected]]DEST_HOST:]file2
- OPTION SCP Options, such as limit, recursive copy, cipher, and ssh configuration
- [[email protected]]SRC_HOST:]file1 – Source file.
- [[email protected]]DEST_HOST:]file2 – Destination file
Local files should be identified using an absolute path or a relative path. Remote file names should include a host and user specification.
SCP offers a variety of options to control its behavior. These are the most popular options:
- -P – Specifies remote host ssh port.
- -p -Saves files modifications and access times.
- -q -This option is used to disable the progress meter and other non-error messages
- –C This option forces SCP to compress the data transmitted to the destination machine.
- -r This option instructs _ SCP how to create directories recursively.
Before you begin
The SCP command uses ssh to transfer data. Therefore, it needs an ssh key/password to authenticate on remote systems.
SCP uses the colon (
: ) to distinguish between remote and local locations.
You must have read permissions on the source files and written permission for the target system to copy them.
Copy files with the same location and name on both systems carefully. SCP can overwrite files without notice.
Note: Using the SCP command within a screen session or Linux session is advisable when transferring large files.
Copy Files and Directories between Two Systems using SCP
Secure Copy is a command that can be used to transfer files between Linux systems. This command utilizes the Secure Shell (SSH) process to transfer files. SSH is a standard authentication system across Linux systems, and it is already preinstalled on Linux machines. To use SCP, you must first install SCP on your local system. Once installed, you must execute a few commands on the remote server to transfer files.
Copy a local file to a remote system using the SCP Command
When you need to transfer a local file to a remote system, you can use the SCP Command. This command will allow you to copy files to any remote system that supports SSH. It requires authentication from a user with an SSH key or password. Besides that, this command allows you to specify a remote host ssh port. Using the SCP command, you can also encrypt your files on the fly.
The following command can be used to copy a file from a remote computer to a local one:
scp file.txt [email protected]:/remote/directory
file.txt refers to the file name we wish to copy, remote_username represents the remote user, 10.10.0.2 the IP address of the server. The remote/directory path to the folder you want to copy the file is specified. The remote directory will be used if you do not specify one.
The password will be required to start the transfer process.
[email protected]'s password: file.txt 100% 0 0.0KB/s 00:00
The file will be saved with the original name if the destination location does not contain the filename. You must specify the new filename if you wish to save the file with a different name.
scp file.txt [email protected]:/remote/directory/newfilename.txt
SSH on a remote host listening on a port other than 22 can be specified using the -P argument.
scp -P 2322 file.txt [email protected]:/remote/directory
The command to copy a directory works in the same way as copying files. You will need the -r flag to make it recursive.
Copy a directory from a remote to a local system using the -r option
scp -r /local/directory [email protected]:/remote/directory
Copy a remote file to a local system using the SCP Command
The SCP command is a simple yet powerful command that allows you to copy a remote file to your local system. You must have the SSH service running on both the client and the server to use it. Once you’ve set up SSH, you can use the command to copy a file from the remote system to your local system. This is especially useful when you need to move multiple files from one server to another.
Copy a file from a remote location to a local one by using the remote location and the local location as the source.
To copy a file called file.txt to a remote server with IP 10,10.0.2, use the following command:
scp [email protected]:/remote/file.txt /local/directory
If you haven’t set up a passwordless SSH login to remote machines, you will be asked for your password.
Copy a file between two remote systems using the SCP Command
This tutorial will show you how to use the SCP command to copy a file between two remote systems. First, you’ll need to specify the destination path. For example, if the file is on a local machine, you’ll use the local file location to copy it. Once that’s done, you can run the SCP command to transfer the file to a remote system.
rsync, you don’t need to log in to any servers to transfer files to another remote machine using SCP.
The following command will copy the file /files/file.txt of remote host1.com into the directory /files at remote host2.com .
scp [email protected]:/files/file.txt [email protected]:/files
The passwords for the remote accounts will be asked, and data will be transferred directly from one remote host.
Use the -3 option to route traffic through the machine where the command was issued.
scp -3 [email protected]:/files/file.txt [email protected]:/files
The SCP command is used to copy a file from the current host to the destination path. It uses the user’s account to perform the copying. However, the SCP command needs a different port number in case you change the default ssh 22 port. You must specify the port number with the -P option to complete the transfer process successfully. This command doesn’t go through the local computer. Instead, it takes place directly between the two remote servers.