How to transfer file between servers with scp command.

What is scp command?

The scp command stands for "secure copy" and is a command-line tool used to securely transfer files between local and remote systems over a secure SSH (Secure Shell) connection. Here are some key points about the scp command:

  • scp is primarily used in Unix-like operating systems, including Linux and macOS.
  • It allows you to copy files and directories between local and remote systems or even between two remote systems.
  • The basic syntax of the scp command is:
scp [options] <source> <destination>

Copy files using SCP command in Linux

The steps to copy files from one server to another server are:

  • Step 1: Get login information for each server.
  • Step 2: Get file path of the files to be copied.
  • Step 3: Login to the second server and use scp command to copy files.

Get login information for each server.

Assume we have to copy a file F from server A to server B. Get the Login information for both servers. Be sure to get the IP address of the servers as the hostname will not work with scp command which we will use.

To get the IP address of the system you are logged, use the following command:

ifconfig

It will be as follows:

  • Server A
    • Username: userA
    • Password: pswA
    • IP: XX.XX.XX.XX
  • Server B
    • Username: userB
    • Password: pswB
    • IP: XX.XX.XX.XX

Get file path of the files to be copied.

Login to remote server using an utility like MobaXterm.

Get the file path of the file you want to copy. If you want to copy an entire directory, get the absolute path of the directory.

We can get the absolute path of the current position we are on using the following command:

pwd

It will give an output as:

/rot

It might be that you need to copy the file named "information.txt" in the current location.

Step 3: Login to the second server and use scp command to copy files.

In this step, login to server B where you want to copy the files and go to the location you want to keep the files. Once done, use the following command to copy file named "information.txt" from server A to current server:s

scp root@XX.XX.XX.XX:/file.txt

Note:

  • . is to get the file in the current working directory. We can specify a path instead it.

For our case, the real command is:

scp '/home/liviu.desktop//file.txt' root@aa.bb.cc.dd:/root

To copy the entire directory, use scp -r. The command will be as follows:

scp -r '/home/liviu.desktop//file.txt' root@aa.bb.cc.dd:/root

You will be prompted to enter the password of the server A from where files are to be copied. Type the password (will not be visible on screen) and press ENTER key.

With this, you will copy your file or directory successfully.

Alternative approach using WinSCP

In this approach, we need not login to the remote server using a terminal utility. You can browse through the files in a server using an utility like WinSCP.

The approach is as follows:

  • Step 1: Login to server A using WinSCP
  • Step 2: Download the files from server A to your local system (Windows)
  • Step 3: Login to server B using WinSCP
  • Step 4: Upload the local files to server B

To download and upload files, you just need to select the concerned files and drag it to the appropriate location.

With this, you must have a strong idea of how to copy files from one remote server to another remote server.