You know that, scp command allows us to copy files across the hosts. In this post you will find scp example by which you can copy files or directories across the hosts.
Basic syntax of the scp command
scp <file name> <user name>@<destination host>
SCP Example 1: File copy Push method.
In this type of scp example we will copy local file to destination server. Lets you have file called name.txt on the server “rhel1” and you want to copy this file on the remote host “RHEL2” at the location /tmp. Then use below command.
[root@rhel1 ~]# scp name.txt root@RHEL2:/tmp/ The authenticity of host 'rhel2 (192.168.216.135)' can't be established. RSA key fingerprint is 77:68:bb:aa:d9:a0:81:74:b1:f9:6d:67:29:1f:53:4d. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'rhel2' (RSA) to the list of known hosts. root@rhel2's password: name.txt 100% 27KB 26.6KB/s 00:00 [root@rhel1 ~]#
SCP Example 2: File copy Pull method
In this type of scp example we will copy file located at remote server to local location. Let’s say you have file name “/tmp/ftp-0.17-51.1.el6.x86_64.rpm” on the server “RHEL2” and you want to copy this to local server “rhel1” at location /tmp, then give below command from “rhel1” as below:
[root@rhel1 ~]# scp root@RHEL2:/tmp/ftp-0.17-51.1.el6.x86_64.rpm /tmp/ root@rhel2's password: ftp-0.17-51.1.el6.x86_64.rpm 100% 57KB 56.9KB/s 00:00 [root@rhel1 ~]#
SCP Example 3: Directory copy Push method
In this type of scp example we will copy local directory to destination server. Lets you have directory called /test on the server “rhel1” and you want to copy this directory on the remote host “RHEL2” at the location /tmp. Then use below command.
Kindly note that in the below command -p option preserves access time and modes from original file whereas -r option help us to copy recursively.
[root@rhel1 /]# scp -pr /test root@RHEL2:/tmp/ root@rhel2's password: prefixab 100% 10KB 10.0KB/s 00:00 prefixaf 100% 10KB 10.0KB/s 00:00 prefixag 100% 10KB 10.0KB/s 00:00 ftp-0.17-51.1.el6.x86_64.rpm 100% 57KB 56.9KB/s 00:00 prefixak 100% 9080 8.9KB/s 00:00 prefixac 100% 10KB 10.0KB/s 00:00 prefixad 100% 10KB 10.0KB/s 00:00 prefixah 100% 10KB 10.0KB/s 00:00 prefixai 100% 10KB 10.0KB/s 00:00 prefixae 100% 10KB 10.0KB/s 00:00 prefixaa 100% 10KB 10.0KB/s 00:00 prefixaj 100% 10KB 10.0KB/s 00:00 [root@rhel1 /]#
SCP Example 4: Directory copy Pull method
In this type of scp example we will copy directory located at remote server to local location. Let’s say you have directory named “/tmp/test” on the server “RHEL2” and you want to copy this to local server “rhel1” at location /root/, then give below command from “rhel1” as below:
[root@rhel1 /]# scp -pr root@RHEL2:/tmp/test /root/ root@rhel2's password: prefixag 100% 10KB 10.0KB/s 00:00 prefixaa 100% 10KB 10.0KB/s 00:00 prefixah 100% 10KB 10.0KB/s 00:00 prefixac 100% 10KB 10.0KB/s 00:00 prefixad 100% 10KB 10.0KB/s 00:00 prefixae 100% 10KB 10.0KB/s 00:00 prefixab 100% 10KB 10.0KB/s 00:00 prefixai 100% 10KB 10.0KB/s 00:00 ftp-0.17-51.1.el6.x86_64.rpm 100% 57KB 56.9KB/s 00:00 prefixaf 100% 10KB 10.0KB/s 00:00 prefixak 100% 9080 8.9KB/s 00:00 prefixaj 100% 10KB 10.0KB/s 00:00 [root@rhel1 /]#
SCP Example 5: file copy remotely.
In this type of scp example we will copy file located at remote server to another remote server. Let’s say you have file name “/tmp/ftp-0.17-51.1.el6.x86_64.rpm” on the server “RHEL2” and you want to copy this to local server “rhel1″ at location /tmp, then give below command from another server say”rhel3” as below:
[root@rhel3 ~]# scp root@rhel1:/tmp/name.txt root@RHEL2:/tmp/ root@rhel1's password: root@rhel2's password: name.txt 100% 57KB 56.9KB/s 00:00 Connection to rhel1 closed. [root@rhel3 ~]#
SCP Example 6: using non-default port.
You know that by default scp commands uses port 22. What if you want to use non-default one. So in this type scp example we will be using non default port 3545. To work out this non-default port, you must change default port at the destination server.
Leave a Reply