Friday, 3 May 2013

Secure CoPy (SCP)- Secure Option to Copy Files in Linux

8 comments

 

1. What is Secure CoPy (SCP) ?

            SCP is a command used to transfer files or folders securely to a particular remote user. In windows sharing of folders or files can be done in LAN only. There is no facility provided to transfer data securely from one user to another remote user, we require to use service from some third party software to do so. In Windows you can get this facility by installing WinSCP software and transfer the files directly to the Linux OR you can install OpenSSH on Windows machine using cygwin which is a collection of tools which provide a Linux like environment for Windows operating system. After OpenSSH installation you can transfer files using SCP from Windows to Linux and from Linux to Windows.

2. How to Use SCP ?

           To use SCP, first you have to start ssh service on the other side where you want to transfer the data. To start the service in red hat based OS switch to root user and give the command as " service sshd start ", for debian based OS you can use " sudo /etc/init.d/ssh start " or " sudo service ssh start ". SCP requires password of the remote machine to copy files or folder, so you can keep ssh server running all time, no one can copy irrelevant material without prior authentication.
          After starting the ssh service of target system you can give following command to start copy.
Syntax to copy single file:
         scp file1.txt user@target_IP_Address:~/Destination_PATH 

Here, you require to provide user name with IP address of remote system as shown above. 
Here ~ is used to represent home directory of a particular user in Linux.
After entering this command you need to provide the password of the target system , if password is correct then your copying files get started.
To copy multiple files using SCP, you can give multiple file names separated by space. Files may have different extensions.

Syntax to copy multiple files:
          scp file.txt file2.avi file3.iso user@target_IP_Address:~/Desktop

To copy particular folder you can use SCP command with " -r " option which indicates recursive operation.

Syntax to copy single folder:
          scp -r folder_name user@target_IP_Address:~/Desktop


You can also copy multiple folders using SCP like we did with multiple files as given above. You can use following command to copy multiple folders using SCP command.

Syntax to copy multiple folders:
         scp -r folder1 folder2 user@target_IP_Address:~/Desktop 


3. Errors and solutions

  • One common error is " port 22: Connection refused " which occurs when ssh service of the destination machine is stopped. You can eliminate  this error by starting ssh service at the destination machine.
  • Another error is that your file or folder get copied to your own directory with user name followed with IP address like " user@target_IP_Address ". This error happens if " :PATH " is not mentioned in command.

8 comments:

  1. Nice one sir. If i want to transfer file from Linux OS to Windows OS, then how it is possible using SCP?

    ReplyDelete
    Replies
    1. You can simply install CYGWIN as shown in video https://www.youtube.com/watch?v=DHAEsUL6rsw. After installing CYGWIN you can transfer files or folders from Linux to Windows as we transfer it from Linux to Linux.

      Delete
  2. sir, i am not able to send any file. it says port 22 connection refused. then i typed "sudo start ssh" still it doesn't work.

    ReplyDelete
    Replies
    1. you are starting ssh service on your machine, but the problem is on other side which is refusing connection.
      Please start the ssh service on other machine to which you want to send data by using same command. You may get this type of error if ssh server is not present on other machine, in such condition you have to install ssh server using " sudo apt-get install ssh ".

      Delete
  3. Better is

    rsync -e ssh -avz source target

    ReplyDelete
  4. @Jan de Haan

    enhance this :

    rsync -ahz --progress --stats source target

    ReplyDelete