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.
$ 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.
$ 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.
$ scp remoteuser@remoteserver:/folder/remotefile.txt .
4. Coping multiple files from local to remote.
$ scp localfile1.txt localfile2.txt remoteuser@remoteserver:/folder/
5. Coping all files from local to remote.
$ scp * remoteuser@remoteserver:/folder/
6. Coping all files and folders recursively from local to remote.
$ scp -r * remoteuser@remoteserver:/folder/
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:
$ 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.
$ 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.
Share
Other articles you may find interesting
Advertisment