How to Transfer Files Using SCP Command between Linux Systems

Transferring files between Linux systems is a common task for system administrators. There are numerous ways to achieve this, but one of the most secure and reliable methods is by using the Secure Copy Protocol (SCP). SCP is a network protocol that allows files to be transferred to, from, or between Linux or Unix hosts. It uses SSH for data transfer and provides the same authentication and same level of security as SSH.

In this tutorial, I will show the basic syntax of the SCP command and how to use it to transfer files between Linux machines.

Installing SCP Command in Linux

SCP Command is part of the openssh-clients package. If your server does not have the SCP command installed, you will encounter an error message. To install the SCP command, use the following syntax:

# yum install openssh-clients -y

Understanding SCP Command Syntax

Before we delve into examples of using the SCP command, it’s important to understand the syntax. In our examples, we will use the following definitions:

  • Local server1 (currently ssh) = The server1 IP (192.168.2.2) that I ssh from my notebook.
  • Remote server2 = The server2 IP (192.168.2.5)
  • Another remote server3 = The server3 IP (192.168.2.6)

We will illustrate three scenarios of using the SCP command:

  • To copy from local server1 (current ssh) to a remote server2. (server1–>server2)
  • To copy from a remote server2 to local server1 (currently ssh). ( server2 –> server1)
  • To copy from a remote server2 to another remote server3. ( server2–>server3)
See also  How to Install Java Development Kit (JDK) on Ubuntu

In the third case, the data is transferred directly between the servers; your local server1 ( currently ssh) will only instruct the servers what to transfer.

Using SCP Command in Linux

Now, let’s look at how to use the SCP command in different scenarios.

1. Copying from Local Server to Remote Server

To copy from local server1 (currently ssh) to a (remote) server2, use the following syntax:

# scp /Server1Path/FileName Server2Username@Server2IpAddress:/Server2Path

For example:

[root@server1 ~#]# scp /tmp/scptest.txt root@192.168.2.5:/tmp

You will be prompted to enter the password for the remote server. Upon successful authentication, the file will be transferred.

2. Copying from Remote Server to Local Server

To copy files from a remote server2 to local server1 (currently ssh), use the following syntax:

# scp Server2Username@Server2IpAddress:/Server2Path/FileName /Server1Path

For example:

[root@server1 ~]# scp root@192.168.2.5:/tmp/scptest1.txt /tmp

Again, you will be prompted to enter the password for the remote server. Upon successful authentication, the file will be transferred.

3. Copying from One Remote Server to Another Remote Server

To copy files from a remote server2 to another remote server3, use the following syntax:

# scp -r Server2Username@Server2IpAddress:/Server2Path/FileName Server3Username@Server3IpAddress:/Server3Path

For example:

[root@server ~]# scp -r root@192.168.2.5:/tmp/scptest.txt root@192.168.2.6:/tmp

You will be prompted to enter the passwords for both remote servers. Upon successful authentication, the file will be transferred directly between the two remote servers.

See also  How to Install CentOS 5.7 Server Part 2

Commands Mentioned

  • yum install openssh-clients -y – Installs the openssh-clients package which includes the SCP command.
  • scp /Server1Path/FileName Server2Username@Server2IpAddress:/Server2Path – Copies a file from the local server to a remote server.
  • scp Server2Username@Server2IpAddress:/Server2Path/FileName /Server1Path – Copies a file from a remote server to the local server.
  • scp -r Server2Username@Server2IpAddress:/Server2Path/FileName Server3Username@Server3IpAddress:/Server3Path – Copies a file from one remote server to another remote server.

Conclusion

In this tutorial, we’ve covered the basics of the SCP command in Linux. The SCP command is a powerful tool for transferring files between Linux systems. It provides a secure and reliable method for file transfer, leveraging the security of SSH.

By understanding the basic syntax and usage of the SCP command, system administrators can efficiently manage file transfers between servers. Whether you’re copying files from your local server to a remote server, from a remote server to your local server, or between two remote servers, the SCP command offers a straightforward solution. Remember to replace the placeholders in the command syntax with your actual file paths, usernames, and IP addresses.

However, there’s much more to learn about managing Linux servers and web hosting. For more information, check out our articles on the best web servers, Apache, Nginx, LiteSpeed, dedicated server hosting, VPS server hosting, cloud hosting, and shared hosting.

For a more in-depth understanding of web servers, you can visit our pages on the best web servers, Apache, Nginx, and LiteSpeed. If you’re interested in different types of hosting, we have detailed articles on dedicated server, VPS server, cloud hosting, and shared hosting.

See also  4 lvcreate Command Examples on Linux

FAQ

  1. What is the SCP command in Linux?

    The SCP (Secure Copy) command in Linux is used to copy files between servers in a secure manner. It uses SSH for data transfer and provides the same level of security as SSH.

  2. How do I install the SCP command in Linux?

    You can install the SCP command in Linux by installing the openssh-clients package. The command for this is: yum install openssh-clients -y.

  3. How do I copy files from my local server to a remote server using SCP?

    To copy files from your local server to a remote server using SCP, use the following syntax: scp /LocalServerPath/FileName RemoteServerUsername@RemoteServerIpAddress:/RemoteServerPath. Replace the placeholders with your actual file path, username, and IP address.

  4. How do I copy files from a remote server to my local server using SCP?

    To copy files from a remote server to your local server using SCP, use the following syntax: scp RemoteServerUsername@RemoteServerIpAddress:/RemoteServerPath/FileName /LocalServerPath. Replace the placeholders with your actual file path, username, and IP address.

  5. How do I copy files between two remote servers using SCP?

    To copy files between two remote servers using SCP, use the following syntax: scp -r Server1Username@Server1IpAddress:/Server1Path/FileName Server2Username@Server2IpAddress:/Server2Path. Replace the placeholders with your actual file path, username, and IP address.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *