15 scp commands securely copy files to remote servers Linux

Basically to copy files/directories from one Server/Machine to another we have to use file sharing services such as Samba(SMB) OR NFS, but we don’t want to create an Samba server / NFS server just to copy required files/directories one time. Here is an solution where we can make use of 15 scp commands securely copy files to remote server. This scp command examples works an all the Unix / Linux flavors.

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 example 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

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/
15 scp commands

scp commands

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/

 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

preserving attributes

5. Compress and copy files / directories faster – 15 scp commands examples

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/

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:

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/

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

Generate an ssh key and copy to destination which makes passwordless connection

[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/
scp commands wihtout password copy files

without password copy files

10. Instead 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/

14. Use specified bandwidth scp command examples

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/

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

Conclusion

15 scp commands which makes our life easy to copy files/directories instantly to remote servers. scp command examples are most useful for transferring files and directory content from one server to another server.

Thanks for the read please write your feedback on the same.

Related Articles

SSH to remote server without password

File System Usage Monitoring using Shell Script

How to Monitor CPU utilization using Shell Script

Take Remote Desktop of Linux Machine

Thanks for your wonderful Support and Encouragement

blank

Ravi Kumar Ankam

My Name is ARK. Expert in grasping any new technology, Interested in Sharing the knowledge. Learn more & Earn More

5 Responses

  1. It is very nice dude..

  2. blank Bharat Khaneja says:

    You are really doing awesome job Ravi… 🙂

  3. blank Shanmukha says:

    Thx for sharing. Please share rsync link as well, bcos I need it very badly now.

  4. blank Phani Akkina says:

    Good explanation with examples.

Leave a Reply

Your email address will not be published. Required fields are marked *