Secure copy (SCP) Unix Networking Command

Discussion in 'Unix' started by poornaMoksha, Oct 8, 2011.

  1. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    There are times, when sitting on your Linux box, working on a command line terminal, you suddenly feel a need of copying a file to/from a remote host. At this point you desire a command line utility that could do this task for you in just one line. Well, this is where 'scp' saves your day :)

    This article makes an attempt to describe the 'scp' command on Linux.

    • 'scp' stands for secure copy and is used to copy a file to/from a remote server.
    • This utility provides encryption.
    • This command comes along with the 'ssh' package.ie, you need to have ssh on your linux box in order to use scp.
    • To install ssh package on your ubuntu or ubuntu-like system, run the following command :
      Code:
       sudo apt-get install ssh
    • 'scp' is better than 'rcp' because 'scp' uses encryption.
    • 'scp' depends upon authentication. So, one needs to provide the username and password of the linux/unix box to/from where the file is being copied.

    Following is the syntax of a basic 'scp' command :

    Code:
     scp [[user-name@]source-host:]source-file-path [[user-name@]dest-host:][dest-file-path] 
    • Since the scp command provides authentication. So, 'user-name' is the user login ID which has the rights to access the file at that particular path
    • source-host/dest-host is the host name or IP address of the host from/to where one wants to copy the file/Directory.
    • One can omit the '[[user-name@]source-host:]' if the source host is the host from where the scp command is being issued.
    • source-file-path or dest-file-path is the complete path from/to where in the source/dest host the file is desired to be copied.
    Some basic options used with scp command:

    -p
    Preserves the modification and access times, as well as the permissions of the source-file in the destination-file​
    -q
    Do not display the progress bar​
    -r
    Recursive, so it copies the contents of the source-file (directory in this case) recursively​
    -v
    Displays debugging messages​

    Examples



    Following is the syntax of Linux scp command to send file or directory to a remote computer:

    Code:
    scp -r [/<path>/filename] [user-login-name@ip address] : .
    Following is the syntax of Linux scp command to retrieve file or directory from a remote computer:

    Code:
    scp -r [user-login-name@ip address] : [/<path>/filename] .
    Following is the syntax of Linux scp command to copy file or directory from a remote host to a remote destination computer:

    Code:
    scp -r [user-login-name@src ip address] : [/<path>/filename]  [user-login-name@dest ip address] : [/<path>/filename] 
    In the above commands :

    • The option -r stands for recursively copying( Like in case of a directory).
    • '.' means current directory.
    • <path> is the complete path where the file/directory resides.

    • In command 1 : A file kept at /<path>/filename at the host from where the 'scp' command is being typed is being copied to a remote host '[user-login-name@ip address]' in its current directory '.'
    • In Command 2 : A file at remote host '[user-login-name@ip address]' which is kept at path '/<path/filename' is being copied to the host from where 'scp' command is being issued. The destination path of the file is the current directory.
    • In Command 3 : A file at a remote host '[user-login-name@src ip address]' which is kept at '/<path>/filename' is being copied to the destination host '[user-login-name@dest ip address]' at a path '/<path>/filename'

    Lets look at a real time example. Since I do not have a LAN setup, so I will use source/destination as my machine only :

    Code:
    sudo scp /home/himanshu/practice/main.c /home/himanshu/Desktop/main.c
    The above command copies main.c from '/home/himanshu/practice' to '/home/himanshu/Desktop'.

    I know it seems more like a cp command here but the copy operation that took place here was encrypted. Also, if you are on LAN or if you have access to a remote server then scp command comes in real handy.

    Some points to remember


    • Multiple names or wild-card character '*' can be used to copy more than one files in one go to a destination computer. For example :

      Code:
      $ scp file1.txt file2.txt <user-name>@<remote-IP>:.
      OR

      Code:
      $ scp *.txt <user-name>@<remote-IP>:.
    • One can use 'putty' to login from windows OS to Linux OS and then run the scp command from putty shell to copy file from Linux box to Windows box.

    • To remove scp from your ubuntu or ubuntu-like box, run the following command :

      Code:
       sudo apt-get autoremove ssh
      We removed ssh package from system in order to remove scp because it was with ssh package through which we installed scp as it comes along bundled with
      ssh package.
    • Some times people face problems where in scp says that data has been successfully transferred but actually the file/directory has not been copied to the
      destination. I have never encountered this problem but from others experiences, this problem may occur due to the the 'echo' commands in the .bashrc file
      being executed at the remote machine. The problem is well known but the root cause is yet to be pointed out. :)

    Conclusion



    To conclude, scp command is useful utility for remote copying of files or directories just like ftp. It uses ssh and provides encryption and authentication for data transfer.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice