use scp command in Ubuntu
Commands

Use scp Command in Ubuntu

Sagar Sharma
Sagar Sharma

Table of Contents

The scp (secure copy protocol) is used to copy files between local to remote or remote to remote machines.

It uses SSH to ensure the files are being transferred securely so make sure you have all the credentials to use the SSH like username, IP, and password.

And in this guide, I will walk you through the following:

  • Transfer files from the local machine to the remote machine
  • Transfer files from the remote machine to the local machine
  • Transfer files between the remote machines

So let's start with the first one.

How to transfer files to remote servers using the scp command

First, let's have a look at the syntax:

scp [options] [source] [destination]

Here,

  • [options]: is used to tweak the default behavior of the command such as you can add the -r option and it will copy every file in the selected directory.
  • [source]: is where you'd have to specify the file that you want to copy. And if you are using files from the remote source, you'd have to mention the username and IP address (eg. user@remote:/path/to/file)
  • [destination]: is the destination where you want to paste the copied files. And if you want to paste files in the remote directory, you'll have to mention the username and IP/domain of the host (user@remote:/path/to/file).

Now, let's have a look at some examples of how you can use the scp command.

1. Copy files and directories from the local machine to the remote machine

Let's start with the files.

To copy files from your local system to the remote machine, you'd have to follow the given command syntax:

scp file.txt user@remoteIP:/path/to/destination/

For example, here, I wanted to send a text file named Hello.txt inside the Docs directory of my remote server, so I used the following command:

scp Hello.txt [email protected]:~/Docs/
Copy files from the local machine to the remote machine using the scp command

Now, let's have a look at how you can copy-paste directories.

Copying directories to the remote server

The syntax will remain the same, but this time, you'll have to use the -r flag which will copy files recursively as shown:

scp -r my_directory/ user@remoteIP:/path/to/destination/

For example, here, I copied the Tutorial directory to the remote server:

scp -r Tutorial [email protected]:~/Images/
copy directories to the remote server using the scp command

On the right window, I'm logged in to my remote server using the SSH and as you can see, it copied the entire directory as intended!

What if you are using SSH on a different port?

If you've changed the port for SSH and it is listening to any other port, you'd have to specify the port number using the -P flag:

scp -P [port-no] File user@remoteIP:/path/to/destination/

2. How to copy files from the remote to the local system

To copy files from the remote directory to the local system, all you have to do is execute the scp command in the following syntax:

scp user@remote:/path/to/remote/file /path/to/local/destination/

For your reference, here, I have copied one text file LU.txt from the remote machine to the local:

scp [email protected]:~/LU.txt /home/sagar/Tutorial
How to copy files from the remote to the local system  using the scp command

3. How to copy files from one remote machine to another

⚠️
In my usage, I was given errors using the traditional authentication method (by entering a password to connect through SSH).
And enabling password-less login in SSH worked for me.

If you're a system admin, you must be dealing with multiple systems on a daily basis, and in that case, this will work as a blessing.

To do so, you'd have to use the scp command in the following manner:

scp user1@remote1:/path/to/file user2@remote2:/path/to/destination/

Here, I have copied Hello.txt from one remote server to another one:

How to copy files from one remote machine to another

And there you have it!

Want to harden the SSH? Here are a few tips

By design, SSH is a pretty secure protocol, but what if I tell you that you can make it even more secure?

Yes, and here's the detailed guide for that:

10 Actionable SSH Hardening Tips to Secure Your Linux Server
Worried about the security of your Linux server? Learn some easy to implement tips on securing SSH and make your Linux server more secure.

I hope you will find this guide helpful.

And if you have any queries, feel free to ask in the comments.



Sagar Sharma

Sagar Sharma

A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.