In this tutorial, we will show you practical examples of using the sftp command to transfer files securely.
What Is SFTP
SFTP stands for Secure File Transfer Protocol. The “S” sometimes also stands for SSH (or Secure Shell), 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 to extend the SSH v2.0 to enhance secure file transfer capabilities. Furthermore, the
sftp command-line interface is similar to the
What Is the Difference Between FTP and SFTP
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 client’s identity, and once a secured connection is established, it sends the encrypted data.
So, SFTP is preferable to FTP in almost all cases because of its underlying security features.
What Is the Difference Between SCP and SFTP
You are already asking yourself: What’s the difference between SFTP and SCP? Don’t they both work on SSH?
Related: FTP vs. FTPS vs. SFTP: The Difference Between Them Explained
Here’s the answer. SCP can only be used for transferring files, and it is non-interactive. At the same time, SFTP is more elaborate and allows interactive commands to create directories, delete directories and files, etc.
How to Use the SFTP Command
You can connect to SFTP as mentioned by a client, such as FileZilla; otherwise, it can be used within a command-line interface using basic commands.
Related: How to Connect to SFTP Using FileZilla for Secure File Transfer
SFTP also provides several methods for connection authentication, such as a username and password, SSH keys, or combinations.
SFTP, by default, uses port 22, which is the default port for SSH. It is a subsystem of SSH and supports all SSH authentication mechanisms.
Establishing an SFTP connection
You can use command-line SFTP on Linux systems 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 works 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 current 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 current 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 SFTP commands shown in the table above.
Close the connection by typing
exit once you are done with your work.
That covers the basics of how to use SFTP for secure file transfer. This tutorial shows you how to use the sftp command to download and upload files to your remote SFTP server.
We hope this article will help you understand the usage of SFTP to some extent. For more about the
sftp command in Linux, consult its manual page.
Feel free to leave a comment if you have any questions or feedback.
I have a key file but I don’t want anyone who uses the computer to be able to connect. How do i use sftp to connect using a ssh key file specified at the command line?