How to transfer files remotely via SSH

Disclosure
Some of the links in this post are affiliate links and if you go through them to make a purchase I will earn a commission. Keep in mind that I link these companies and their products because of their quality and not because of the commission I receive from your purchases. The decision is yours, and whether or not you decide to buy something is completely up to you. Read more

SSH or Secure Shell is a protocol that allows a secure connection to the remote computer. There are multiple methods to transfer files remotely via SSH. The most common method is via scp utility which comes with the SSH implementation. Other methods, such as sftp and rsync, are file transfer applications which secure there connection using SSH protocol.

By using one of the previously mentioned methods, you can securely transfer a file from your local machine to a remote server and vise versa.

We will discuss each method in the section below

Keep in mind that you need to make sure that you have access right and correct permission to the remote server’s files and folders in order for these methods to work.

By the way, if you like to master command-line, Codecademy offers one of the best courses to learn command-line.

Transfer files using SCP

scp or secure copy is considered to be the easiest among other methods. To work with this command, you need to specify the remote host’s DNS name or IP Address with the remote username. After hitting Enter, you will be prompted to enter the password for the remote user or you can setup key-based authentication, to avoid entering the password every time you copy a file. See the examples below:

1. Coping single file from local to remote.

Bash/Shell
$ scp localfile.txt remoteuser@remoteserver:/folder/

In the above example, f the target folder (/folder/) is not specified, it will copy the file to the remote user’s home directory.

2. Coping single file from remote to local.

Bash/Shell
$ scp remoteuser@remoteserver:/folder/remotefile.txt localfile.txt

3. Coping single file from remote to local current working directory with the same filename(remotefile.txt) by using . as the copy target.

Bash/Shell
$ scp remoteuser@remoteserver:/folder/remotefile.txt .

4. Coping multiple files from local to remote.

Bash/Shell
$ scp localfile1.txt localfile2.txt remoteuser@remoteserver:/folder/

5. Coping all files from local to remote.

Bash/Shell
$ scp * remoteuser@remoteserver:/folder/

6. Coping all files and folders recursively from local to remote.

Bash/Shell
$ scp -r * remoteuser@remoteserver:/folder/
Note

remoteuser must exist and have write permission to folder/ in the remote system, otherwise the copy operation will be denied.

Transfer files using SFTP

sftp or Secure FTP is just like ftp but with secure connection. So, most of the commands are similar and can used the same way as with ftp. As you can see in the example below:

Bash/Shell
$ sftp remoteuser@192.168.1.1
remoteuser@192.168.1.1's password:
Connected to remoteuser@192.168.1.1.
sftp> dir
file1.txt  file2.txt  file3.txt   
sftp> pwd
Remote working directory: /home/remoteuser
sftp> get file1.txt
Fetching /home/remoteuser/file1.txt to file1.txt
/home/remoteuser/file1.txt                                  100%  633KB  12.0MB/s   00:00
sftp> bye
$ 

Transfer file using rsync

In this method, you are going to secure your rsync session with ssh. This is achieved by using --rsh=ssh or -e "ssh" with your normal rsync command. In case you didn’t specify these options, rsync will first try to connect to rsyncd but it will automatically fallback to SSH if rsyncd is not running on the remote system.

Bash/Shell
$ rsync -av --delete --rsh=ssh file.txt remoteuser@192.168.1.1:/folder/
$ rsync -av --delete -e "ssh" file.txt remoteuser@192.168.1.1:/folder/

The above 2 commands will give the same result.


At the end of this article, I would like to mention that there are GUI applications that can also be used to transfer files remotely, such as WinSCP that support scp method and sftp too. The other tool is FileZilla, which is a very popular FTP solution that also supports sftp. And there are many others.


Other articles you may find interesting

managed vs unmanaged switches
In general, network switches are the main building block of wired computer networks. It’s the networking hardware that connects other…
Read More
how to turn ESP32 into NAT router repeater
ESP32 development board can be used as WiFi router/repeater with NAT(Network Address Translation), thanks to martin-ger, who developed this great…
Read More
turn any website into separate native application
As a Windows user, I do most of my work on my PC using a browser, not because I prefer…
Read More
In this article you will learn how to link a domain to Vultr server. So, you can connect to it…
Read More
Secure Shell supports various authentication mechanisms. The two most used methods are: Password based authenticationPublic SSH key authentication In this…
Read More
Secure Shell (SSH) is a cryptographic network protocol for operating network services securely such as login, remote command-line execution, and…
Read More

Advertisment

Leave a Reply

Your email address will not be published. Required fields are marked *