Tuesday, August 20

Transfer Files remotely Using SCP Command on Linux RHEL /CentOS 5&6&7


Transfer Files Using SCP

             There are many way to transfer files over the network between the linux operating system.  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. Before start, there is the definition for  every syntax that i will use:

local server1 (currently ssh) = The server1 ip (192.168.0.2) that i ssh from my notebook.
remote server2 = The server2 ip (192.168.0.8)
another remote server3 = The server3 ip (192.168.0.9)

There are three ways to use the scp command which are :

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.0.2
server2 : 192.168.0.8

Examples of the syntax as below :


# scp -r /Server1Path/FileName Server2Username@Server2IpAddress:/Server2Path
[root@server1 ~]# scp -r /opt/test.php  root@192.168.0.8:/tmp/
root@192.168.0.8's password:
test.php                                   100%    9     0.0KB/s   00:00
[root@server1 ~]#

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

Examples of the syntax as below :
# scp -r Server2Username@Server2IpAddress:/Server2Path/FileName /Server1Path
[root@server1 ~]# scp -r root@192.168.0.8:/opt/script.php  /tmp/
root@192.168.0.8's password:
scptest.php                                  100%  773     0.8KB/s   00:00
[root@server1 ~]#

3. To copy files from a remote server2 to another remote server3.
server1 : 192.168.0.2
server2 : 192.168.0.8
server3 : 192.168.0.9

Examples of the syntax as below :
# scp -r Server2Username@Server2IpAddress:/Server2Path/FileName
Server3Username@Server3IpAddress:/Server3Path
[root@server ~]# scp -r root@192.168.0.8:/tmp/scptest.txt root@192.168.0.9:/tmp
root@192.168.0.8's password:
root@192.168.0.9's password:
scptest.txt                                   100%    9     0.0KB/s   00:00
Connection to 192.168.0.8 closed.
[root@server ~]#
[root@server ~]# ssh root@192.168.0.9
root@192.168.0.9's password:
[root@server2 ~]# ls  /tmp
scptest.txt
[root@server2 ~]#



Note: Copy Files to more than 2 Servers using Script.



Copy File to more than one server, write shell script

first install sshpass command
 

# yum install sshpass -y 
create one file like copy.sh, open that file

vim copy.sh , Write these script

sshpass -p 'serverpasswd' scp -r /opt/filename root@serveripaddress:/path/

sshpass -p 'serverpasswd' scp -r /tmp/filename root@serveripaddress:/path/

sshpass -p 'serverpasswd' scp -r /home/filename root@serveripaddress:/path/

:wq! (save &quit)


#sh copy.sh (enter)

check ur servers file is copied or not.

7 comments:

  1. Sir ,

    How to copy a file to more than two Servers at a time .
    Please help

    ReplyDelete
    Replies
    1. Copy File to more than one server, write shell script

      first install sshpass command
      # yum install sshpass -y

      create one file like copy.sh, open that file

      vim copy.sh , Write these script

      sshpass -p 'serverpasswd' scp -r /opt/filename root@serveripaddress:/path/

      sshpass -p 'serverpasswd' scp -r /tmp/filename root@serveripaddress:/path/

      sshpass -p 'serverpasswd' scp -r /home/filename root@serveripaddress:/path/

      :wq! (save &quit)

      #sh copy.sh (enter)

      check ur servers file is copied or not.

      Delete
    2. @Mahesh Sir,

      Thank you for Sharing Nice Guide ,

      Could i copy a single file to multiple server at a time Using a SINGLE command ??
      I don't want to make any scripts. Sir Is it possible ?

      Delete
    3. MR Sahil single command is not possible, using above script you can do it...

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Thank you for Sharing Nice Guide ,

    Could i copy a single file to multiple server at a time Using a SINGLE command ??
    I don't want to make any scripts. Sir Is it possible ?
    linux scp

    ReplyDelete
    Replies
    1. With out using Script its not possible, please try with above mentioned Scriprt.

      Delete

:: Linux - Legends ::