Linux Administrator

SCP: Secure Copy in Linux

We all know what FTP is for, but do we all know what SCP is for? Well, SCP stands for secure copy, and is a method used to transfer files between hosts on the same network. SCP uses ssh in the background. Unlike RCP, you need to know the password of the remote machine in order for the transfer to proceed. In this tutorial, we’ll be reviewing the secure copy command.

Sending a file from a computer to a remote computer

This is the simplest case. Suppose that there are two computers – one on which you’re working called CLIENT, and a remote computer called REMOTE. Suppose further that you wish to transfer a file from CLIENT to REMOTE, you would use the following code:

scp {FILENAME} {USERNAME}@{IP ADDRESS}:{DIRECTORY PATH}

Here, {FILENAME} is the file you want to transfer. The {USERNAME} is the username of the REMOTE machine, and the {IP ADDRESS} is the ip address of the REMOTE machine. The {DIRECTORY PATH} is the directory where you want the file to be copied to (in other words, the location where you want the file on the REMOTE machine).

For example:

scp kalyani.txt [email protected]:/home/server/Desktop

In this case, I’m writing that I want the file called kalyani.txt to be sent to the server (username of my server) at 10.0.2.5 on the /home/server/Desktop directory. You can ignore the path information, in which case, it will copy it to the default home directory.

You may get the following error: “ssh: connect to host 10.0.2.5 port 22: Connection refused lost connection”. In this case, please refer to this article: https://linuxhint.com/fix_connection_refused_ubuntu/.

Sending multiple files from a computer to a remote computer

In the second case, one might want to send multiple files over to the remote computer. In my case, I have 3 files called file1.txt, file2.txt, and file3.txt.

In order to send them, you would use the following syntax:

scp {FILENAME_1} {FILENAME_2} {FILENAME_3} {USERNAME}@{IP ADDRESS}:{DIRECTORY PATH}

In this case, we just put three file names consecutively.

Coping directories from a computer to a remote computer

Thus far, we’ve only looked at copying files not folders, but what if you wanted to copy entire folders? Well, that’s possible as well.

scp -r {DIRECTORY} {USERNAME}@{IP ADDRESS}:{DIRECTORY PATH}

In this case, the first {DIRECTORY} is the directory that you want to copy over. The second {DIRECTORY PATH} is the location to which to you want to copy it.

For example:

scp -r directory/ [email protected]:/home/server/Desktop

Grabbing a file from a remote computer to a computer

Now, suppose that we want to do the opposite. Suppose still that we have two machines – CLIENT and REMOTE. Now suppose that there is a file on REMOTE that you wish to copy to CLIENT, then you’d use the following code:

scp {USERNAME}@{IP ADDRESS}:{FILE YOU WANT TO COPY} {DIRECTORY PATH}

In this case, the {USERNAME} and {IP ADDRESS} are very straight forward, and the {FILE YOU WANT TO COPY} is the file that is located on your REMOTE machine that you want to copy over onto your CLIENT machine. {DIRECTORY PATH} is the location where the file will be copied to.

For example:

scp [email protected]:/home/server/Desktop/special.txt /home/kalyani/Desktop

Here, we have a file called special.txt on the server at 10.0.2.5 which we would like to transfer over to the CLIENT machine called kalyani and in particular, on the Desktop.

SCP is a very useful linux command that relies on ssh to securely transfer/copy files from one machine to another. In fact, you can copy files to and from a remote machine.

Happy Coding!

FAQs

What is SCP in Linux?

SCP stands for secure copy, and is a method used to transfer files between hosts on the same network. SCP uses ssh in the background.

How do you send a file from a computer to a remote machine using scp?

scp {FILENAME} {USERNAME}@{IP ADDRESS}:{DIRECTORY PATH}

{FILENAME} is the file you want to transfer.
{USERNAME} is the username of the REMOTE machine.
{IP ADDRESS} is the ip address of the REMOTE machine.
{DIRECTORY PATH} is the directory where you want the file to be copied to

How do you copy directories using scp on a linux machine?

scp -r {DIRECTORY} {USERNAME}@{IP ADDRESS}:{DIRECTORY PATH}

{USERNAME} is the username of the REMOTE machine.
{IP ADDRESS} is the ip address of the REMOTE machine.
{DIRECTORY} is the directory that you want to copy over.
{DIRECTORY PATH} is the location to which to you want to copy it.

How do you copy a file from a remote machine to your client machine using scp?

scp {USERNAME}@{IP ADDRESS}:{FILE YOU WANT TO COPY} {DIRECTORY PATH}

{USERNAME} is the username of the REMOTE machine.
{IP ADDRESS} is the ip address of the REMOTE machine.
{FILE YOU WANT TO COPY} is the file that is located on your REMOTE machine that you want to copy over onto your CLIENT machine.
{DIRECTORY PATH} is the location where the file will be copied to.

Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author

mm

Kalyani Rajalingham

I'm from Sri Lanka (live in Canada), and am a Linux and code lover.

Leave a Comment