File Transfer over SSH

Posted by

Introduction

FTP, or ā€œFile Transfer Protocolā€ is a popular method of transferring files between two remote systems.

sFTP, which stands for SSH File Transfer Protocol, or Secure File Transfer Protocol, is a separate protocol packaged with SSH that works in a similar way over a secure connection. 

In almost all cases, sFTP is preferable to FTP because of its underlying security features and ability to be implemented within an SSH connection. FTP is an insecure protocol that should only be used in limited cases or on networks you trust.

Although sFTP is integrated into many graphical tools, this guide will demonstrate how to use it through its interactive command line interface as a continuation of using SSH to administer a remote machine.

How to Connect with sFTP

The easiest way to connect to an SSH server is through an FTP application such as FileZilla. You just enter the IP of the remote machine, your username and password and you should be good to go. sFTP port should be Port 22.

If you want to connect to an SSH server that has no graphical user interface (if you have a Raspberry Pi for example running as a headless server) you need to know how to use sFTP through SSH via the command line.

By default, sFTP uses the SSH protocol to authenticate and establish a secure connection. Because of this, the same authentication methods are available that are present in SSH. If you already enabled SSH on the remote machine you should be ready to go.If not then you need to install the openssh package on Arch based systems which includes both the client and server software. On Debian based systems such as Ubuntu you need to specify the package by installing either openssh-client or openssh-server.

To test you have SSH access to the remote machine just enter:

ssh username_remote_server@remote_server_ip

Enter the remote user password. If you connect successfully just type exit to close the connection. If not make sure that you have enabled SSH access on the remote machine and Port 22 (the default SSH port) is correctly forwarded on your router to the remote machine.

Now to establish an SSH connection and then open an sFTP session using that connection you can use the following command:

sftp username_remote_server@remote_server_ip

The following table lists essential sftp commands.

lsLists the contents of the remote working directory
pwdDisplays the name of the remote working directory
cdChanges the remote working directory
mkdirCreates a directory on the remote system
rmdirDeletes a directory on the remote system
rmDeletes a file from the remote working directory

Transferring Files with sFTP

You can use the commands above to navigate the remote and local filesystems.

Transferring Remote Files to the Local System

In order to download files from a remote host you need to issue the following command:

get path_to_remote_file

By default, the get command downloads a remote file to a file with the same name on the local file system.

To copy the remote file to a different name you can use:

get path_to_remote_file name_of_local_file

The get command also takes some option flags. For instance, we can copy a directory and all its contents by specifying the recursive option:

get -r someDirectory

For SFTP to maintain the appropriate permissions and access times use the ā€œ-Pā€ or ā€œ-pā€ flag:

get -Pr someDirectory

Transferring Local Files to the Remote System

Transferring files to the remote system is just as easily accomplished by using the appropriately named put command:

put path_to_local_file

The same flags that work with get command apply to put. So, to copy an entire local directory:

put -r localDirectory

Conclusion

Although sFTP is a simple tool, it is very useful for server administration and transferring files between them. Especially if you are using a headless remote server such as a Raspberry Pi and wish to transfer files to and/or from the device.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments