scp copies files between hosts on a network. It uses SSH for data transfer, and uses the same authentication and provides the same security as SSH. Unlike rcp, scp will ask for passwords or passphrases if they are needed for authentication.
SCP command syntax
scp [options] Source Destination
15 scp commands advantages
- scp use SSH as dependant
- SCP use cipher systems AES, Blowfish, 3DES, CAST128, and Arcfour which are supported by SSH
- We can also use passphrase to authenticate
Environment for demonstration
- Server (Source) 192.168.4.20
- Desktop (Destination) 192.168.4.200
1. Copy File from Source to Destination ( Server1 –> Server2)
A simple way to copy the file from server to desktop use below command.
[root@TechTutorials ~]# scp test root@192.168.4.200:/root/Desktop
The authenticity of host '192.168.4.200 (192.168.4.200)' can't be established.
ECDSA key fingerprint is da:58:0e:40:bc:6b:4c:d2:d1:fc:3f:c6:4f:64:93:2e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.4.200' (ECDSA) to the list of known hosts.
root@192.168.4.200's password:
test.rpm 100% 14KB 14.2KB/s 00:00
2. Copy multiple files from source to destination
In this below example we just copy multiple files to remote host, as custom selected files (required files)
[root@TechTutorials ~]# scp initial-setup-ks.cfg file1 file2 root@192.168.4.200:/root/Desktop/
3. Copy directory instead of copying single / multiple files
If we have an multiple files in a directory instead of copying all the files separately we can just copy entire directory as it is to destination. Let’s see an example command
[root@TechTutorials ~]# scp -r test root@192.168.4.200:/tmp/
root@192.168.4.200's password:
ABD 100% 0 0.0KB/s 00:00
uyyo1 100% 0 0.0KB/s 00:00
123ks 100% 0 0.0KB/s 00:00
yupsd45 100% 0 0.0KB/s 00:00
56767 100% 0 0.0KB/s 00:00
UYSISDNF 100% 0 0.0KB/s 00:00
hyie 100% 0 0.0KB/s 00:00
ABD 100% 0 0.0KB/s 00:00
UYSISDNF 100% 0 0.0KB/s 00:00
As we see an above example we copied an ‘test’ directory to destination server. To copy directory ‘-r’ option have to use along with scp command
4. Preserving the time stamp as like source
When you copy files / directories from Source to Destination, we get an latest time stamp in destination side. Copy the files / directories exact as like source (all attributes will be copied) so exact time stamp and permissions will be copied.
[root@TechTutorials ~]# scp -rvp test root@192.168.4.200:/root/Desktop/
preserving attributes
5. Compress and copy files / directories faster – 15 scp commands
Using ‘-C’ option will compress and copy the files / directories to destination. Data compression will happens in Network level and destination receives data size as it is in source.
[root@TechTutorials ~]# scp -C linux-nrpe-agent.tar.gz root@192.168.4.200:/root/
root@192.168.4.200's password:
linux-nrpe-agent.tar.gz 100% 6840KB 6.7MB/s 00:00
6. Know your copy status
If you would like to see an copy status, we have to use ‘-v’ option along with scp command. Verbose will be displayed
[root@TechTutorials ~]# scp -v file1 root@192.168.4.200:/root/
Executing: program /usr/bin/ssh host 192.168.4.200, user root, command scp -v -t /root/
OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013
root@192.168.4.200's password:
debug1: Authentication succeeded (password).
Authenticated to 192.168.4.200 ([192.168.4.200]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: scp -v -t /root/
Sending file modes: C0644 32 file1
Sink: C0644 32 file1
file1 100% 32 0.0KB/s 00:00
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 2624, received 2308 bytes, in 0.1 seconds
Bytes per second: sent 19033.1, received 16741.0
debug1: Exit status 0
7. Use random port to along with scp command
We can also specify particular port to copy data from source to destination
[root@TechTutorials ~]# scp -P 22 file2 root@192.168.4.200:/root/Desktop/
root@192.168.4.200's password:
file2 100% 32 0.0KB/s 00:00
8. Suppress scp command output (verbose) warning and error messages Just copy
Quiet mode: disables the progress meter as well as warning and diagnostic messages from ssh
[root@TechTutorials ~]# scp -vq file1 root@192.168.4.200:/tmp/
Executing: program /usr/bin/ssh host 192.168.4.200, user root, command scp -v -t /tmp/
root@192.168.4.200's password:
Sending file modes: C0644 32 file1
Sink: C0644 32 file1
9. copy/scp files/directories without using password
[root@TechTutorials ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
ff:37:20:a2:52:a3:88:f8:23:de:ef:ef:79:ed:a8:49 root@TechTutorials
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| |
| S |
| o ... . |
|.. . o E .o. . |
|+ + o o o..o o |
|.+.oo+o*o..... . |
+-----------------+
[root@TechTutorials ~]# ssh-copy-id root@192.168.4.200
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.4.200's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.4.200'"
and check to make sure that only the key(s) you wanted were added.
Now initiate an scp will not ask any password
[root@TechTutorials ~]# scp key stdin root@192.168.4.200:/root/
without password copy files
10. Instead of password use key file
To use key file we have to mention ‘-i’ option along with 15 scp commands
[root@TechTutorials ~]# scp -i privatekey.key anaconda-ks.cfg root@192.168.4.200:/root/
11. Use SSH config file to scp
Here in 15 scp commands we can also use predefined parameters to make scp copy easier
All possible values with scp command
AddressFamily
BatchMode
BindAddress
CanonicalDomains
CanonicalizeFallbackLocal
CanonicalizeHostname
CanonicalizeMaxDots
CanonicalizePermittedCNAMEs
ChallengeResponseAuthentication
CheckHostIP
Cipher
Ciphers
Compression
CompressionLevel
ConnectionAttempts
ConnectTimeout
ControlMaster
ControlPath
ControlPersist
GlobalKnownHostsFile
GSSAPIAuthentication
GSSAPIDelegateCredentials
HashKnownHosts
Host
HostbasedAuthentication
HostKeyAlgorithms
HostKeyAlias
HostName
IdentityFile
IdentitiesOnly
IPQoS
KbdInteractiveAuthentication
KbdInteractiveDevices
KexAlgorithms
LogLevel
MACs
NoHostAuthenticationForLocalhost
NumberOfPasswordPrompts
PasswordAuthentication
PKCS11Provider
Port
PreferredAuthentications
Protocol
ProxyCommand
PubkeyAuthentication
RekeyLimit
RhostsRSAAuthentication
RSAAuthentication
SendEnv
ServerAliveInterval
ServerAliveCountMax
StrictHostKeyChecking
TCPKeepAlive
UsePrivilegedPort
User
UserKnownHostsFile
VerifyHostKeyDNS
[root@TechTutorials ~]# scp -F .ssh/config anaconda-ks.cfg root@192.168.4.200:/root/
12. Execute remote to remote host copy
Scenario is copy files from Server2 –> Server3 execute scp command from Server1
[root@TechTutorials ~]# scp root@192.168.2.20:/root/file1 root@192.168.4.200:/tmp/
13. Use different Encryption algorithm to copy
As i mentioned in above of this article SSH will support not only AES encryption it will also support more algorithms, we can also specify which algorithm yo want to use
[root@TechTutorials ~]# scp -c 3des nagios-plugins-2.1.1.tar.gz root@192.168.4.200:/tmp/
nagios-plugins-2.1.1.tar.gz 100% 2615KB 2.6MB/s 00:00
14. Use specified bandwidth
Whereas we may not have much bandwidth to copy files from source to destination, if we push numbers of files yet time with low bandwidth Network will choke/breaks. Instead of breaking we take an action to limit and copy. All the values of bandwidth we mention in Kilo-bits.
[root@TechTutorials ~]# scp -l 500 nagios-plugins-2.1.1.tar.gz root@192.168.4.200:/tmp/
nagios-plugins-2.1.1.tar.gz 100% 2615KB 63.8KB/s 00:41
15. Shell script to copy files / directories to multiple Servers with single command
To make 15 scp commands simple i made an simple scripts which help you to copy files/directories to multiple servers using single command. Use passwordless connection which makes your work more easier
To use this script we have to write list of servers in /tmp/destfile.txt
# vi /tmp/destfile.txt
192.168.4.200
192.168.4.2
192.168.4.90
#chmod 777 /tmp/destfile.txt
Now Create a file anywhere where to keep script file. In this example i want to keep my script in /scripts/ directory
# vi /scripts/multiscp.sh
#!/bin/bash
## Author: Ankam Ravi Kumar
## Purpose: Copy files to multiple Server using single script
## Date: 21st July 2016
echo -e "Please Enter the file path which you want to copy:\c"
read file
for dest in `cat /tmp/destfile.txt`; do
scp -rC $file ${dest}:/tmp/
done
How to execute this file
# chmod u+x /scripts/multiple.sh
# sh /scripts/mutiple.sh
Output of the script
[root@Techtutorials ~]# sh multiscp.sh
Please Enter the file path which you want to copy:/root/test
root@192.168.4.129's password:
test 100% 550 0.5KB/s 00:00
Conclusion
15 scp commands which makes our life easy to copy files/directories instantly to remote servers.
Thanks for the read please write your feedback on the same.
No comments:
Post a Comment