In this tutorial we will explain how to use SFTP to securely transfer content between two servers.

Requirements

  • SSH access (Command line access to the server)
  • Basic skills for working on a Linux environment

The SFTP abbreviation stands for Secure File Transfer Protocol or FTP over SSH. When the SFTP protocol is used the connection between the two servers is encrypted. The same authentication rules are present as in SSH connection so you can use both username/password based authentication or key based one.

Connecting to a server using SFTP

In order to make sure that you will be able to access the server with SFTP you can first try to establish an SSH connection (How to setup SSH keys):

where X.X.X.X is the remote server's IP address. Instead an IP address a hostname can be used as well.

If the connection is successful then you should not experience problems accessing the server using SFTP. In this tutorial we will use the default SFTP client included in the server configuration:

In this case the connection will be made on the default SSH port 22. If due to security reasons the remote server is configured with non-default SSH port we can use the oPort option to specify the exact port:

Depending on the SSH authentication method set on the remote server you should be prompted to fill in the password related to user1 or the passphrase related to the SSH private key loaded on the connecting server.

When the connection is made the command prompt will be switched to:

Basic SFTP commands

We can use the help command to list all available SFTP commands:

Available commands:

  • bye - Quit sftp
  • cd path - Change remote directory to 'path'
  • chgrp grp path - Change group of file 'path' to 'grp'
  • chmod mode path - Change permissions of file 'path' to 'mode'
  • chown own path - Change owner of file 'path' to 'own'
  • df [-hi] [path] - Display statistics for current directory or filesystem containing 'path'
  • exit - Quit sftp
  • get [-P] remote-path [local-path] - Download file
  • help - Display this help text
  • lcd path - Change local directory to 'path'
  • lls [ls-options [path]] - Display local directory listing
  • lmkdir path - Create local directory
  • ln oldpath newpath - Symlink remote file
  • lpwd - Print local working directory
  • ls [-1aflnrSt] [path] - Display remote directory listing
  • lumask umask - Set local umask to 'umask'
  • mkdir path - Create remote directory
  • progress - Toggle display of progress meter
  • put [-P] local-path [remote-path] - Upload file
  • pwd - Display remote working directory
  • quit - Quit sftp
  • rename oldpath newpath - Rename remote file
  • rm path - Delete remote file
  • rmdir path - Remove remote directory
  • symlink oldpath newpath - Symlink remote file
  • version - Show SFTP version
  • !command - Execute 'command' in local shell
  • ! - Escape to local shell
  • ? - Synonym for help

We can use the pwd and lpwd commands to find out which are the working folders on the current and remote server:

We can download a file using the get command:

The output should be similar to the following one:

We can initiate folder download using the -r option:

The corresponding upload commands for a file and folder are: