Sunday 14 February 2016

SCP (Secure Copy) Command Tutorial with Practical Examples


SCP (Secure Copy) Command Tutorial with Practical Examples

 

Overview :

This tutorial will help the system administrators to know how to securely copy files to remote linux servers from local linux machine & vice versa in UNIX like operating system.  SCP stands for Secure Copy ,it copies files between hosts over a  network.  It uses ssh for data transfer &  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.

Below is the Syntax of copying File from Local Machine To Remote Machine

# scp examplefile username@Remote-Server:/home/

Above command will copy examplefile to remote under /home folder , it will ask the password of username mentioned in the command.

Syntax for Copying files from remote server to your local Machine

# scp username@Remote-Server:/home/examplefile /home

Above command will copy examplefile from remote Server’s /home folder to local machine’s home folder.

Some of the commonly used options in scp command.

  •  -r : Recursively copies the contents of source files or directories.
  • -p : Preserves the access time, modification time, permissions of the source files in the destination.
  • -q : disables the progress bar as well as warning & diagnostic messages from ssh.
  • -v : verbose mode. Displays debugging messages.
  • -P : copy files using the specified port number.
  • -l limit :  Limits the used bandwidth, specified in Kbit/s.
    Example:1 Copy the file “itstuff.txt” from a remote host to the local machine
    [root@linux ~]# scp root@mail.nextstep4it.com:/opt/itstuff.txt  /home
    root@mail.nextstep4it.com's password:
    itstuff.txt                                          100% 1024KB   5.0KB/s   03:23
     
    Example:2 Copy the file “itstuff.txt” from the local machine to a remote host
    [root@linux ~]# scp  /home/itstuff.txt   root@mail.nextstep4it.com:/opt/
    root@mail.nextstep4it.com's password:
    itstuff.txt                                          100% 1024KB   1.0MB/s   00:00
     
    Example:3 Copy the directory “data” from the local host to a remote host’s directory /opt using ‘-r’ option.
    [root@linux opt]# scp -r data  root@mail.nextstep4it.com:/opt
     root@mail.nextstep4it.com's password:
     file5                                                 100%    0     0.0KB/s   00:00
     file3                                                 100%    0     0.0KB/s   00:00
     file1                                                 100%    0     0.0KB/s   00:00
     file2                                                 100%    0     0.0KB/s   00:00
     file4                                                 100%    0     0.0KB/s   00:00
     
     Example:4 Copy multiple files from the remote host to your current directory on the local machine
    [root@linux ~]# scp  root@mail.nextstep4it.com:/opt/data/\{file1,file2,file3} .
     root@mail.nextstep4it.com's password:
     file1                                                  100%    0     0.0KB/s   00:00
     file2                                                  100%    0     0.0KB/s   00:00
     file3                                                  100%    0     0.0KB/s   00:00
     
     Example:5 Copy the file “itstuff.txt” from the local machine to a remote host using port 2751.
    [root@linux ~]# scp -P 2751 /home/itstuff.txt root@mail.nextstep4it:/opt
     
    Example:6 Copy files from local machine to remote host using blowfish options.
    By default scp uses the Triple-DES cipher to encrypt the data being copied. Using the Blowfish cipher we can increase speed. This can be done by using option -c blowfish in the command line.
    [root@linux ~]# scp -c blowfish /home/itstuff.txt root@mail.nextstep4it.com:/opt/
    root@mail.nextstep4it.com's password:
    itstuff.txt                                           100% 1024KB   1.0MB/s   00:00
     
    Example:7 Limit the bandwidth used by the scp command using the ‘-l’ option.
    We can limit the bandwidth used by the scp command using ‘-l’ option as shown in the below syntax
    # scp -l bandwidth_limit filename username@remote-host:/folder-name
    where bandwidth_limit is numeric to be specified in kilobits per second.