In this tutorial we’re going to show you with practical examples how to use the sftp command to securely transfer files.
What is SFTP
SFTP stands for Secure File Transfer Protocol. The “S” sometimes also stands for SSH (or Secure Shell) which is the secure, encrypted tunnel that the file transfer service runs through.
SFTP is a popular method for securely transferring files over remote systems. The
sftp command is typically part of the OpenSSH package. It was designed as an extension of the SSH v2.0 to enhance secure file transfer capabilities. The
sftp command line interface was designed to be similar to the
What is the Difference Between FTP and SFTP
When compared with the traditional FTP protocol, SFTP offers all the functionality of FTP, but it is more secure and easier to configure. Unlike FTP, SFTP uses a single data and control channel. Before sharing the information between two computers SFTP verifies the identity of client and once a secured connection is established it sends the encrypted information.
In almost all cases, SFTP is preferable to FTP because of its underlying security features.
What is the Difference Between SCP and SFTP
I guess you are already asking yourself: What’s the difference between SFTP and SCP? Don’t they both work on SSH?
Here’s the answer. SCP can only be used for transferring files, and it is non-interactive. SFTP is more elaborate, and allows interactive commands to do things like creating directories, deleting directories and files, etc.
How to Use the SFTP Command
SFTP also provides several methods to fulfill the authentication of a connection such as username and password, SSH keys, or combinations of these.
SFTP by default uses port 22, which is the default port for SSH. It is basically a subsystem of SSH and supports all SSH authentication mechanisms.
Establishing an SFTP connection
You can use command-line SFTP on systems running Linux, or from the macOS Terminal.
For example, if your username is
john, to connect to your account on the host
sftp [email protected]
Enter your password when prompted. If all goes as expected, you will be presented with the
sftp prompt, and you can start interacting with the remote server.
Connected to myserver.com. sftp>
SFTP to Port Other Than 22
If the remote SSH server working on a custom SSH port (not the default port 22), use the
-P option to specify the SFTP port.
sftp -P 2222 [email protected]
Useful SFTP Commands
If you need a quick cheat sheet, here’s a list of all the available SFTP commands. You can find this list yourself by simply entering the
|cd||Change the directory on the remote host.|
|chmod||Change the permissions of files on the remote host.|
|chown||Change the owner of files on the remote host.|
|exit||Close the connection to the remote host, and exit SFTP.|
|get||Copy a file from the remote host to the local computer.|
|lcd||Change the directory on the local computer.|
|lls||List the contents of the current directory on the local computer.|
|lmkdir||Create a directory on the local computer.|
|lpwd||Show the present working directory on the local computer.|
|ls||List the contents of the current directory on the remote host.|
|mkdir||Create a directory on the remote host.|
|put||Copy a file from the local computer to the remote host.|
|pwd||Show the present working directory on the remote host.|
|rename||Rename a file on the remote host.|
|rm||Delete a file on the remote host.|
|rmdir||Remove a directory on the remote host, but the directory has to be empty.|
Transferring Files with the SFTP Command
SFTP allows you to transfer files between two machines securely.
To download a single file from the remote server, use the
sftp> get website-logo.jpg
Fetching /home/john/website-logo.jpg /home/john/website-logo.jpg 100% 11KB 154.7KB/s 00:00
To download a directory from the remote system, use the
-r (recursive) option.
sftp> get -r images/
To upload a file from the local machine to the remote SFTP server, use the
sftp> put website-logo.jpg
Uploading website-logo.jpg to /home/john/website-logo.jpg dogtoman-tmp-logo.png 100% 11KB 235.4KB/s 00:00
To upload a local directory to the remote SFTP server:
sftp> put -r images/
Of course, you can experiment with any of the SFTP commands shown in the table above.
Once you are done with your work, close the connection by typing
That pretty much covers the basics of how to use SFTP for secure file transfer. In this tutorial, we have shown you how to use the
sftp command to download and upload files to your remote SFTP server.
We hope this article will help you to understand the usage of SFTP in some extent. For more about
sftp command in Linux, consult its manual page.
Feel free to leave a comment if you have any questions or feedback.