How to use SCP to transfer files from local to server

Reading Time: 3 minutes

Introduction

Scp stands for secure copy and its means of securely transferring files between two machines on a network. It is a file transfer network protocol. SCP uses Secure Shell (SSH) mechanisms for data transfer and authentication to ensure the confidentiality of the data in transit.

Why use Scp?

Using Scp you can copy a file or directory:

  • From your local system to a remote system.
  • From a remote system to your local system.
  • Between two remote systems from your local system.

When transferring data with scp, both the files and password are encrypted so that anyone snooping on the traffic doesn’t get anything sensitive.

How Does Scp Work?

The SCP client can easily upload files to an SSH server or request files and directories for downloading. Then, the server sends all the subdirectories and the files that are available for download. it is in fact a native command in most operating systems, such as macOS, Windows, or Linux.

SCP Command Syntax:

scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
[user@]SRC_HOST:]file1 - Source file.
[user@]DEST_HOST:]file2 - Destination file

Local files should be specified using an absolute or relative path, while remote file names should include a user and host specification.

scp provides a number of options that control every aspect of its behaviour. The most widely used options are:

  • -P – Specifies the remote host ssh port.
  • -p – Preserves files modification and access times.
  • -q – Use this option if you want to suppress the progress meter and non-error messages.
  • -C – This option forces scp to compress the data as it is sent to the destination machine.
  • – r – This option tells Scp us to copy directories recursively.
  • -J – destination
  • -l – limit
  • -o – ssh_option

Before you Start:

The scp command relies on ssh for data transfer, so it requires an ssh key or password to authenticate on the remote systems.

The colon (:) is how scp distinguish between local and remote locations.

To be able to copy files, you must have at least read permissions on the source file and write permission on the target system.

Be careful when copying files that share the same name and location on both systems, scp will overwrite files without warning.

Copy Files and Directories Between two systems with SCP:

To copy a file from a local to a remote system run the following command

scp -i  ec2.pem demo.txt ubuntu@54.86.131.241:/home/ubuntu/myfiles
  • ec2.pem – . pem files are generally the public key used by the client to verify and decrypt data sent by servers.
  • -i – identity_file
  • demo.txt – text file
  • ubuntu@54.86.131.241 – Remote server Public IP address
  • /home/ubuntu/myfiles – Destination Path
Local machine
Remote machine

Transfer file server to local machine

Transfer a file from a server to a local machine using the SCP command.

ssh-add ec2.pem
Add the publickey (.pem) identity
add .pem

Demo.txt file transfer from server to the local machine.

Remote machine
scp  ubuntu@54.89.145.193:/home/ubuntu/demo.txt .
local machine demo.txt file
  • ubuntu@54.89.145.193 – Remote machine IP address(Public IP).
  • /home/ubuntu/demo.txt – Remote machine file path
  • (.) – local machine current Directory

Advantages of Scp:

  • SCP can only transfer files, it can do it significantly faster than SFTP.
  •  it maintains the Security of the data being transferred and protects the authenticity by blocking packet sniffers from extracting valuable information from the data packets.

Disadvantage of Scp:

  • it can only transfer files and is not as complete a process as other protocols.
  • SCP is slower than it should be, especially on high bandwidth networks, is that it has statically defined internal flow control buffers which end up becoming network performance bottlenecks.

https://www.thegeekdiary.com/how-to-transfer-files-securely-using-scp-command-in-linux/

https://blog.knoldus.com/

knoldus