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)
3 SCP command in linux example :
- 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)
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
[root@server1 ~]# scp /tmp/scptest.txt root@192.168.2.5:/tmp root@192.168.2.5's password: scptest.txt 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.2.2
server2 : 192.168.2.5
Examples of the syntax as below :
# scp Server2Username@Server2IpAddress:/Server2Path/FileName /Server1Path
[root@server1 ~]# scp root@192.168.2.5:/tmp/scptest1.txt /tmp root@192.168.2.5's password: scptest1.txt 100% 773 0.8KB/s 00:00 [root@server1 ~]#
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
[root@server ~]# scp -r root@192.168.2.5:/tmp/scptest.txt root@192.168.2.6:/tmp root@192.168.2.5's password: root@192.168.2.6's password: scptest.txt 100% 9 0.0KB/s 00:00 Connection to 192.168.2.5 closed. [root@server ~]#
[root@server ~]# ssh root@192.168.2.6 root@192.168.2.6's password: [root@server2 ~]# ls /tmp scptest.txt [root@server2 ~]