Connect with us

Website

How to Use SCP: Transfer Files from Local to Remote

Published

on

How to Use SCP: Transfer Files from Local to Remote

While SSH is a way to log into a remote server from your local machine, it’s not a way to transfer a file from your local computer to the remote server. An SSH session works within the context of the remote server’s file system, and it doesn’t have any access to the files on your desktop. However, the SSH protocol is very useful, and we can use it to transfer files securely.

The way to do this is by using the SCP – Secure Copy Protocol. While it’s a separate tool, it leverages the underlying SSH protocol to securely transfer files in an encrypted manner, in a way that can’t be intercepted by anyone en route.

Using SCP on Windows, Linux, and macOS

Traditionally, the “scp” command was only available on Linux and macOS by default, and Windows users had to either install the OpenSSH suite or use another program like “pscp” from PuTTY. However, the latest versions of Windows 11 allow you to use the “scp” command directly from the command line. This makes scp easily accessible to anyone without the need for 3rd part software.

Allowing Passwords on the Remote Server Makes it Easy

In an earlier series of articles, I had extolled the virtues of disabling passwords on your root server and forcing users to use SSH keys to log in instead. That advice still holds today. Using public/private keys to log in is safer, and more convenient once you’ve set up the process.

However, when connecting to the server for ad-hoc purposes like transferring files from a local to the remote directory, the wiring up of the public/private keys can present a headache that’s not worth the effort – particularly if you’re not going to be doing it often. So for this tutorial, I’m going to assume that your remote server is protected with a password, and not a pair of SSH keys. If your server uses SSH keys to log in, see if you can temporarily disable them so that you can log in with passwords only for the time it takes you to transfer the files.

Transferring Files from Local to Remote via SCP

To make the transfer, you’ll need the following pieces of information:

Advertisement
  1. Full path to the file on the local computer
  2. Your login credentials on the remote server
  3. The IP address or the domain name of the remote server
  4. The path to the folder on the remote destination server

For this example, I’ve created a file called “Test-File.txt” on my desktop. On Windows 11, I can open the command line and start using the “scp” command right away. Here’s the syntax:

 scp Test-File.txt [Username]@[IP Address]:[Destination Directory]

In the above command replace the following:

  1. [Username]: Your username on the remote server
  2. [IP Address]: Your remote server’s IP address
  3. [Destination Directory]: The path to the destination folder on the remote server

In this example, I’ve opened the terminal in the same location as the file I want to transfer – “Test-File.txt”, so I don’t need to specify the origin path. To make things easy on yourself, I suggest you do the same. Open the terminal either directly at the location of your files or first navigate to the file’s location. This way you won’t have to enter more information in the terminal than you have to.

Confirming the Key Fingerprint

If this is the first time that you’re connecting to the remote server, Windows will throw a warning saying that it doesn’t recognize the key fingerprint of the remote server. This is a precautionary measure meant to protect you against man-in-the-middle attacks, where a 3rd party tries to spoof the destination server, making you think that you’re connected when in reality you’re not.

If you’re sure that you’ve entered the IP address of the server correctly, type “yes” to confirm to your local machine that the destination server is safe.

Enter your Password

Once you’ve confirmed that the key signature you see is the one belonging to the remote server to which you want to connect, then you have to enter the password corresponding to the username that you entered while executing the SCP command. If everything goes well, the command will execute like this:

Using SCP on Windows
Using SCP on Windows

As you can see, the system first asked me to confirm the key fingerprint of the server, and then asked me for my password. The file I was trying to transfer was just an empty text file, so it didn’t have any size. However, you can see from the screenshot below that the file now exists on the remote server after I SSH into it:

File Transferred
File Transferred

It’s that easy!

Removing Windows’ Memory of the Server

As mentioned above, when you connect to the server for the first time, the system asks you if you recognize the key fingerprint of the remote server. This is to defend you against man-in-the-middle attacks, where a server spoofs the real one and pretends to be something else. If you know what you’re doing, and have checked the IP address, you’re probably fine, so you just type “yes” and carry on. Windows will then save the key fingerprint of the remote server in the “.ssh/known_hosts file so that it won’t have to ask you the same question the next time you log in.

Advertisement

However, there are times when you might want to purge the memory of your local PC so that it “forgets” the remote server and forces it to ask you the next time you connect. I wanted to do this myself to write this tutorial so that I could take screenshots of the scp tool asking me if I recognized the remote server. Fortunately, removing the key signature is easy. Just enter the following command on Windows:

ssh-keygen -R <remote_server_ip_or_hostname>

This will generate the following output:

Removing the Memory of the Server
Removing the Memory of the Server

The ssh-keygen tool will remove the entry from the .ssh/known_hosts file and will force the system to ask you for permission the next time you connect using SSH or SCP.

Using PuTTY Instead of SCP

As an alternative to the in-built SCP tool that comes with Windows, you can use PuTTY. This isn’t the first time I’ve referenced putty on this SirsteveHQ blog. It’s even part of the toolkit in the article for adding SSH keys which I linked earlier. PuTTY has an SCP tool called “pscp.exe”, and you can download it as a standalone file on the download page and invoke it when you want to transfer files using the SCP protocol.

PuTTY was particularly useful on older versions of Windows that didn’t have access to the OpenSSH tools. But it has already acquired such a large user base that many people will continue to use it out of habit. The syntax is more or less identical to that used by the SCP tool on Windows, so switching between the two is easy.

If you’re using a later version of Windows, though, I would suggest sticking to the default scp tool that comes with the command line. That way you won’t have to keep track of another program.

Advertisement

Conclusion

Using SCP to transfer files from your local to the remote system is easy. The SCP protocol is built on top of SSH, which means that it provides a fully encrypted channel for file transfers.

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.