SFTP Command, How to Use it Practically for Secure File Transfer

In this tutorial, we’ll cover the usage of SFTP from the command line.

sftp command

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

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?

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

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 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 myserver.com, enter:

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 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 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 present 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 present 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.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 put command:

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 exit.


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.

1 Comment

  1. 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?

Leave a Reply

Your email address will not be published.

Latest from Linux Knowledge