use scp command on linux 1

How to Use SCP Command on Linux

Use the SCP command on Linux and seamlessly protect your data while transferring across SSH connections. You can do that by encrypting the files and the passwords. Read and learn precisely how.

The term SCP stands for Secure copy protocol. It usually means securely transferring computer files and programs between a local machine to the user’s device or vice versa. SCP typically refers to both Protocol and the program itself. 

In other words, SCP is a command-line utility that allows users to copy files and a whole directory between two hosts. To put it in simple words, it is more like a secure version of the cp (copy) command.

The SCP Command: The Benefits

SCP protects user data while copying data across a Secure Shell connection. The same seamlessly encrypts the files and passwords of the target machine. The advantage of encryption is that, even if someone steals the data, they will fail to unlock it. 

SCP is known for its simplicity, security, pre-installed availability for new users, and security. Some common uses of SCP include:

  • Copying files from a local host to a remote host
  • Copying files from a remote host to a local host

Remote SCP: Understanding the Modes

Here, hosts can refer to a server, remote computer, or even a user computer. Usually, there are two modes of remote SCP process:

  • Source mode, which reads files and sends them back to the client
  • Sink mode, which accepts the files sent by the client and writes them on a remote host

Basic Syntax:

The basic syntax for SCP depends on the nature of the transfer. Here is what it looks like:

When you copy a local file to a remote host

$ scp [flags] SendingFile user@remotehost:directory/TargetFile
use scp command

When you copy a file from a remote host 

$ scp [flags] user@remotehost:directory/SourceFile LocalTargetFile
use scp command

Some Crucial Flags Associated With the SCP Command

Before you learn how to use the SCP command on Linux, you need to understand the different options that work with it. Here is the list of the most crucial ones:

  • -4: The flag lets use the IPV4 address
  • -6: This flag lets use the IPV6 address
  • -C: The -C option is for enabling compression
  • -d: The -d option is for copying the file
  • -l limit: It helps limit the bandwidth in Kbit/s
  • -P: It specifies a custom port

How to Use the SCP Command On Linux

Now that you’ve got enough information already, let’s learn how to use the SCP command on Linux.

Prerequisites

Here are some utilities essential for using the SCP command on Linux. Make sure you’ve access to these before employing the SCP command.

  • A secure shell login on the server
  • Root access for both hosts (Client and Server)

From Local Machine to Remote Server or Host

First, we’ll learn the best ways to share a single file. Follow the guide to understand how.

Step 1: Using the SCP Command

Type the following command to view different options with the SCP command:

$ scp
use scp command

Step 2: Navigate to the Directory

From there, head towards the directory where your files are. In my case, it’s the desktop.

$ cd Desktop/
navigate to the directory

Step 3: Connecting the Machine

This step is all about connecting our machine to the remote machine and making sure the remote machine is running well.

For this, we need to deal with the following things:

Name of the file you want to send. Suppose it is hello.sh for my case.

Username of the remote server. Let it be code bind, for instance.

Now the next thing is knowing about the public IP of the remote machine. For this, you have to invoke the following command in your remote machine: 

$ ifconfig 
connecting to the machine

After that, specify the directory you want to copy the files. In my case, I’m considering /home/codebind for the purpose.

Next up is passing the password for your remote server. Do that and hit the “Enter key” to proceed.

Once everything is done, you will see the required files being copied to the remote server. Keep an eye; it should get copied in the location you provided earlier.

Step 4: Verify

To verify status, launch the Terminal and type the ls command. Make sure you do that on the remote server. Here, you will find that location holds the file as expected. If not, repeat the process to this point.

Share Multiple files (Directory):

To transfer an entire content directory, we must use the -r flag. What it does, is copy all the files recursively.

To understand how it works, let’s create one and name it test123.

Now use the following command as earlier.

share multiple files

As you can see, the contents in my test123 directory test123 are copied to the remote server.

From Remote Server To Local Machine :

As you can recognize the pattern, just tweak some commands. For this, use the following syntax:

$ scp [remote-host-name]@[remote-host-ip]:[target-directory]/[file-name] [destination-directory]

Here is what it looks like for our case:

Remote Server username is codebind, IP address is 192.168.245.141, target file and directory is /home/codebind/scala-2.12.3.deb and destination directory is /test/Desktop

Hit the “Enter key”. It will then ask for the password of the remote server. Input the same and hit “Enter key”. You will then see the file being copied to the destination directory on your machine. 

If your remote PC uses a different port other than the default ssh port, which is 443, you add the -P flag followed by the port number, whatever is required.

Changing SCP Cipher to Encrypt Files

In all the above commands, we used the default Cipher for SCP, which is AES-128, to encrypt files. You can use the -c flag to change all other ciphers like AES-129, AES-256, 3des, etc.

In the commands, you should use -c followed by a Cipher name like mentioned above.

According to OpenSSH developers, which provide a secure channel over client-server architecture, SCP is outdated, inflexible, and not readily fixed. Their recommendation is to use modern technology like SFTP, a CLI  client program that transfers files using SSH File Transfer Protocol (SFTP).

Likewise, you might want to consider new things if you can, but you can use which one benefits you the most.

So, in the above tutorial, we sent a file from a remote server to a local machine and vice versa using SCP.

If this guide helped you, please share it.

Leave a Reply
Related Posts