How to Use Secure File Transfer using SFTP Command Print

  • SFTP, Secure File Transfer
  • 4

Secure File Transfer Protocol is what SFTP stands for. Sometimes the "S" also stands for SSH (or Secure Shell), which is the secure, encrypted tunnel through which the file transfer service runs.

SFTP is one of the most popular methods for securely transferring files over remote networks. The sftp command is typically part of the OpenSSH package. It was designed as an extension of SSH v2.0 to enhance secure file transfer capabilities. The sftp command-line interface was designed to be similar to the ftp command.

The Difference Between FTP and SFTP is, SFTP offers all the functionality of the traditional FTP protocol, but it is more secure and easier to configure than FTP. Unlike FTP, SFTP uses a single data and control channel. Before sharing the information between two computers SFTP verifies the identity of the client and once a secured connection is established it sends the encrypted information.SFTP is preferable to FTP because of its underlying security features.

The Difference Between SCP and SFTP, 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.

Using the SFTP Command, SFTP can be used via any client, such as FileZilla, Also, it can be used from a command-line interface using basic commands.
SFTP also provides several methods to fulfill the authentication of a connection such as a username and a 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 the username is johnd, to connect to your account on the host, enter:


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

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

Command Function
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 get command:

sftp> get website-logo.jpg
Fetching /home/johnd/website-logo.jpg
/home/johnd/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-header.jpg
Uploading website-logo.jpg to /home/john/website-logo.jpg
dogtoman-tmp-logo-header.png 100% 11KB 154.73KB/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.

Was this answer helpful?

« Back

<-- removed to be added again -->