How to Use SFTP Command for Secure File Transfer

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 ftp command.

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.

sftp [REMOTE_USER]@[REMOTE_SERVER]Code language: CSS (css)

For example, if your username is john, to connect to your account on the host, enter:

sftp john@myserver.comCode language: CSS (css)

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
sftp>Code language: CSS (css)

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 john@myserver.comCode language: CSS (css)

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 help or ? command.

cdChange the directory on the remote host.
chmodChange the permissions of files on the remote host.
chownChange the owner of files on the remote host.
exitClose the connection to the remote host, and exit SFTP.
getCopy a file from the remote host to the local computer.
lcdChange the directory on the local computer.
llsList the contents of the current directory on the local computer.
lmkdirCreate a directory on the local computer.
lpwdShow the current working directory on the local computer.
lsList the contents of the current directory on the remote host.
mkdirCreate a directory on the remote host.
putCopy a file from the local computer to the remote host.
pwdShow the current working directory on the remote host.
renameRename a file on the remote host.
rmDelete a file on the remote host.
rmdirRemove 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 get command:

sftp> get website-logo.jpgCode language: JavaScript (javascript)
Fetching /home/john/website-logo.jpg
<meta http-equiv="content-type" content="text/html; charset=utf-8">/home/john/website-logo.jpg        100%  11KB  154.7KB/s  00:00Code language: HTML, XML (xml)

To download a directory from the remote system, use the -r (recursive) option.

sftp> get -r images/Code language: JavaScript (javascript)

To upload a file from the local machine to the remote SFTP server, use the put command:

sftp> put website-logo.jpgCode language: CSS (css)
Uploading <meta http-equiv="content-type" content="text/html; charset=utf-8">website-logo.jpg to /home/john/<meta http-equiv="content-type" content="text/html; charset=utf-8">website-logo.jpg
dogtoman-tmp-logo.png              100%  11KB  235.4KB/s  00:00Code language: HTML, XML (xml)

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.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%