How to Transfer Files with Secure Copy Protocol (SCP)?

author
Saumil Kalaria

Head, Open Source Solutions

What is SCP and How does it Work?

SCP stands for Secure Copy Protocol that allows for the creation of a “self-contained” program. In other words, the file combined with its resources is all packaged together into a single executable program.
SCP is a file format that enables access to the content of a document, directory, or computer system. It works by storing and retrieving files on a remote system.

How to Transfer Files Securely and Quickly with SCP

You can run the SCP program on any remote command-line interface, including ssh, which is an encrypted protocol used to connect to another computer. In addition, SCP provides secure file transfers faster than FTP since they don’t require data encryption overhead.

How to Transfer Files Securely and Quickly with scp

It is available on most systems we all regularly use, such as Windows, Mac OSX, Linux, Unix, and mobile operating systems such as iOS and Android.

SCP is the most feasible way to transfer files between two different computers securely.

This method is very beneficial for distributing programs to computers with limited hard drive storage and memory capacity because the self-contained executable doesn’t need to load any external libraries or files to execute.

To use SCP, you need to have an account on both the systems and log in with the appropriate credentials. The syntax for using it is as follows:

For example:

How to Transfer Files Securely and Quickly with scp

SCP is a command-line program for securely transferring files over a network. Here you’ll learn how to use SCP to copy files from the local server to the remote server.

[Also Read: How Shipping Activities can Scale Up using Odoo-AFS Integration]

For Windows users, you will need the SCP client installed on your machine. Linux or macOS users already have the SCP client installed.

There are two main ways to use the SCP command:

To transfer one or more files from a remote computer to your local machine, such as the following:

How to Transfer Files Securely and Quickly with scp

To copy one or more files from your local computer onto a remote machine, such as the following:

How to Transfer Files Securely and Quickly with scp

Using SCP to Copy Files From Local Server to Remote Destination

SCP is one of the safest ways to transfer files from one location to another. 

Here is the basic syntax of the SCP command. Let’s have a proper understanding before getting into how to utilize it.

The SCP syntax takes the following form:

How to Transfer Files Securely and Quickly with scp

Where

  • option – This includes SCP options such as cipher, ssh port, ssh configuration, limit, recursive copy, etc.
  • [user@]src_host:]file1 are the Source files.
  • [user@]dest_host:]file2 is nothing but the destination file

Local files generally consist of absolute or relative paths, whereas remote files have a user and host specification.

The SCP command uses ssh to transport data; it requires an ssh key or password to log into remote systems.

SCP differentiates between local and remote sites with the help of a colon (:).

You must have come across reading permission on the source file and the write permission on the target system while copying files.

And while copying files with the same number and address on both systems, be cautious because SCP will rewrite files without notifying you.

Copy Files and Directories Between Two Systems with SCP

Copy a Local File to a Remote System with the SCP Command

Run the following command to copy a file from a local to a remote system:

How to Transfer Files Securely and Quickly with scp

The file’s name we wish to copy is file.txt, the user on the remote server is remote username, and the server IP address is 10.10.0.2. If you don’t provide a remote location, the SCP command will copy the file to the hidden user’s home directory. To indicate the destination directory for the copied file, you can use the syntax /remote/directory.

After entering the user password, the transfer process will begin.

How to Transfer Files Securely and Quickly with scp

If you don’t specify a new filename in the destination location, the file will be copied with its original name.  Specify the new file name if you wish to save the file under a different name:

How to Transfer Files Securely and Quickly with scp

If SSH is listening on a port other than the default 22 on the remote host, then you can specify the port using the -P argument:

How to Transfer Files Securely and Quickly with scp

The command that copies the directory is similar to the one that copies files. The only difference is that for recursive, you must use the -r parameter.

Use the -r option to copy a directory from a local to a remote system:

How to Transfer Files Securely and Quickly with scp

Copy a Remote File to a Local System using the SCP Command

Use the distant location as the source and the local location as the destination to copy a file using SCP from remote to local system.

How to Transfer Files Securely and Quickly with scp

Copy a File Between Two Remote Systems using the SCP Command 

Unlike rsync, SCP does not require you to log into one of the servers in order to transfer files from one distant workstation to another.

This command will copy the file /files/file.txt from the remote host host1.com to the directory /files on the remote host host2.com.

How to Transfer Files Securely and Quickly with scp

To make the traffic go through the machine where the command is being executed, you can use the -3 option:

How to Transfer Files Securely and Quickly with scp

SSH Tunneling and Using ssh-agent

You’ll need an SSH client to connect to your server across a secure tunnel. PuTTy is a free SSH client for Windows and UNIX platforms that we used in the instructions below.

Note – To access the server via SSH tunnel using PuTTY on a specific port, you must have it configured. 

You’ll need to construct an SSH tunnel once you’ve configured your SSH client accurately and verified that you can securely access your instance over SSH. To do so, take the following steps:

  • Create a secure tunnel by forwarding a destination port on the remote server to a source port on the localhost in the “Connection -> SSH -> Tunnels” section (127.0.0.1 or localhost). Below is an example of how to set up an SSH tunnel between distant port 80 and local port 8888.
  • In order to add the secure tunnel configuration to the session, click the “Add” button. (The newly inserted port will appear in the list of “Forwarded ports.”) Below is an example of how to set up an SSH tunnel between distant port 80 and local port 8888.
  • Save your changes in the “Session” section by clicking the “Save” button.
  • To connect to the server through SSH, click the “Open” button. The SSH session will now include a secure SSH tunnel between the two selected ports.
How to Transfer Files Securely and Quickly with scp
How to Transfer Files Securely and Quickly with scp

You should be able to access the application over the secure SSH tunnel you constructed by going to http://127.0.0.1:SOURCE-PORT/ or http://localhost:SOURCE-PORT/ when the tunnel is running. Remember to substitute the source port number for SOURCE-PORT.

Access A Server Using An SSH Tunnel On Linux And macOSlocalhost X

To use an SSH tunnel to connect to a server on a specified port, you’ll need the following information:

  • The IP address of the server
  • Password and username; if there is a need to connect the server using the SSH username and password).
  • If you have deployed your server using an SSH key pair, the server username and corresponding SSH private key are stored in .pem format for Linux and macOS SCP.

Once you have the following information, follow these steps to connect to the server via an SSH tunnel:

  • On your local machine, open a new terminal window.
  • There are two options for configuring the SSH tunnel: you can either connect to the server using a private key, or connect using an SSH password.

    Follow the instructions below per each option:
  • Option 1: Connect to the server using a private key
    • Make sure you’ve got your SSH credentials ready.

To configure the SSH tunnel, use the command below. Remember to change SOURCE-PORT to your server’s source port, DESTINATION-PORT to your server’s destination port, KEYFILE to the path to your private key, and SERVER-IP to your server’s public IP address or hostname:

How to Transfer Files Securely and Quickly with scp
  • Option 2: Connect to the server using an SSH password
    • Execute the command below. Replace SOURCE-PORT with the source port, DESTINATION-PORT with the destination port, USERNAME, and SERVER-IP with your server’s public IP address or hostname. When prompted, provide your SSH password.
How to Transfer Files Securely and Quickly with scp

Transferring Multiple Files Simultaneously using SCP with ssh

Copying SCP Multiple Files

If we wish to copy a large number of files from a single location. Rather than copying the entire folder, we can supply the required files in the same command and separate them with a space. It should be as follows:

How to Transfer Files Securely and Quickly with scp

It is also possible to copy from multiple locations if necessary. We simply need to submit a space-separated list of them.

[Also Read: The Best Synchronization Ever: Canvas-Odoo Integration]

Copying Folder

A little change in the command is required to move a folder and its contents from or to a server instead of a file. The ‘-r’ option specifies that the folder and its contents should be processed recursively.

Let’s see with an odoo example.

How to Transfer Files Securely and Quickly with scp

It will copy the folder and all of its contents to the specified location.

How to Transfer Files Securely and Quickly with scp

This command copies all files from the specified location to the specified location.

Files can also be copied between two remote servers. We can use the following command to copy files from server-1 to server-2:

How to Transfer Files Securely and Quickly with scp

Planning to integrate Odoo ERP?

Brainvire offers Odoo Implementation and consulting services tailored to your needs.

Conclusion

In this post, you learned everything there is to know about the Secure Copy Protocol, including how to use it, command syntax, and some samples. In a nutshell, the SCP command transfers files between computers using the SSH service securely and straightforwardly. Knowing what an SCP is and how it functions is merely the start. The SCP command has a plethora of choices from which to choose.

How to Transfer Files Securely and Quickly with scp
Odoo Usage Statistics

Odoo is a business management suite designed for SMEs and enterprises which provides the best of both worlds – customization meets scalability. The user can customize Odoo to their needs by tailoring it to suit, adding features as required, or updating existing ones without compromising performance. 

How to Transfer Files Securely and Quickly with scp

The above diagram signifies the market position of Odoo in terms of popularity and traffic compared to the most popular content management systems.

But, of course, one has to have an idea in mind and contact reliable Odoo partners with relevant requirements before employing any team of experts who will create your enterprise application explicitly tailored to you!

Brainvire is an expert Odoo Gold Partner that has delivered hundreds of solutions to businesses across the globe. Our experts make sure that your business goes online without any data negligence and with minimal or zero downtime. Contact us at [email protected] to schedule a consultation.

FAQs

Can you transfer multiple files at once using SCP?

Use SCP with the -r option to copy a directory (including all of its files). This instructs SCP to replicate the source directory’s contents recursively. On the source system, you will be asked for your password. If you don’t use the correct password, the command will fail.

How do I Sftp multiple files at once?

Use the mget command to download multiple files from the sftp server. mget works by widening each filename in the list and then running the get command on each of them. The files are copied to the current working directory, which can be modified using the lcd command.

How do you run SCP in parallel?

The best option is to utilize a tool specializing in batch file transfers, such as rsync, which may also be used via regular ssh.
If you don’t have rsync access, you can use zip, tar, and gzip, or bzip2, and then SCP the generated archives (then connect with ssh, and do the unpacking).

Is SCP multithreaded?

SCP uses a single thread in SSH. It appears that the crypto, rather than the wire speed, is sometimes the barrier. Their new approach (HPN-SSH) takes advantage of multithreaded platforms, substantially boosting the speed of securely copying files.

h
About Saumil Kalaria

Saumil has been in the digital transformation space for over nine years. He’s an expert who offers custom LAMP development solutions across various industries—looking for a reliable expert in LAMP Technologies? Then do get in touch with Saumil today!

Lets Connect!