How to Transfer Files Using SCP Command in Linux

Share this Article :

There are many way to transfer files over the network between the linux operating system. In this post, I will show the basic syntax on how to use secure copy (SCP) command to transfer the files. SCP 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.

SCP Command in Linux

SCP Command will transfer files between linux machines over a secure and encrypted connection. But for if your server does not have SCp command, you will get the following error message :

# scp
-bash: /usr/bin/scp: No such file or directory

SCP command is part of openssh-clients package. You can install scp command with this syntax :

# yum install openssh-clients -y

Before start with SCP command example, there is the definition for every syntax that i will use :

  • 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)
Also Read :   How to Capture the Perfect Screenshots on the HTC Desire HD

3 SCP command in linux example :

  1. To copy from local server1 (current ssh) to a remote server2. (server1–>server2)
  2. To copy from a remote server2 to local server1 (currently ssh). ( server2 –> server1)
  3. To copy from a remote server2 to another remote server3. ( server2–>server3)

In the third case, the data is transferred directly between the servers; your local server1 ( currently ssh) will only tell the servers what to be transfer. Let’s have a look at the syntax of this command:

1. To copy from local server1 (currently ssh) to a (remote) server2.
server1 : 192.168.2.2
server2 : 192.168.2.5

Examples of the syntax as below :

# scp /Server1Path/FileName Server2Username@Server2IpAddress:/Server2Path
[[email protected] ~]# scp /tmp/scptest.txt [email protected]:/tmp
[email protected]'s password:
scptest.txt                                   100%    9     0.0KB/s   00:00
[[email protected] ~]#

2. To copy files from a remote server2 to local server1 (currently ssh)
server1 : 192.168.2.2
server2 : 192.168.2.5

Also Read :   How to Install MySQL Database in Linux Using Yum

Examples of the syntax as below :

# scp Server2Username@Server2IpAddress:/Server2Path/FileName /Server1Path
[[email protected] ~]# scp [email protected]:/tmp/scptest1.txt /tmp
[email protected]'s password:
scptest1.txt                                  100%  773     0.8KB/s   00:00
[[email protected] ~]#

3. To copy files from a remote server2 to another remote server3.
server1 : 192.168.2.2
server2 : 192.168.2.5
server3 : 192.168.2.6

Examples of the syntax as below :

# scp -r Server2Username@Server2IpAddress:/Server2Path/FileName 
Server3Username@Server3IpAddress:/Server3Path
[[email protected] ~]# scp -r [email protected]:/tmp/scptest.txt [email protected]:/tmp
[email protected]'s password:
[email protected]'s password:
scptest.txt                                   100%    9     0.0KB/s   00:00
Connection to 192.168.2.5 closed.
[[email protected] ~]#
[[email protected] ~]# ssh [email protected]
[email protected]'s password:
[[email protected] ~]# ls /tmp
scptest.txt
[[email protected] ~]


Leave a Reply