How to use SCP to transfer files from local to server

close up photo of programming of codes
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 transfer one side to another side.

Why use Scp?

You can copy a file or directory from one machine to another machine or local machine to virtual machine:

  • From your local system to a remote system.
  • From a remote system to your local system.

When you transfer the data, SCP both the files and password are encrypted format.

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.

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 path while remote file names should include a user and host.

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.
  • -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 based on ssh for moving data one machine to another machine, so it requires an ssh key or password to authenticate on the remote systems.

In copy files, you must have at least read permissions on the source file and write permission on the target or destination system.

Importanat point to copy files that share the same name and location on both systems, scp will overwrite files without warning.

Copy Files Between two systems using SCP:

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

scp -i  ec2.pem demo.txt ubuntu@54.86.131.241:/home/ubuntu/myfiles
  • ec2.pem
  • -i – identity_file
  • demo.txt – text file
  • ubuntu@54.86.131.241 – Remote server Public IP address
  • /home/ubuntu/myfiles – Destination Path
local machine
server machine

Transfer file server to local machine:

Transfer a file from server to local firstly you add the public key (.pem) file.

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

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

remote machine
scp  ubuntu@54.89.145.193:/home/ubuntu/demo.txt .
  • 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:

  • it is 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 is not as complete a process as other protocols. It can only transfer a file.
  • It is slower than it should be, especially on high bandwidth networks.

https://blog.knoldus.com/