SFTP (SSH File Transfer Protocol) is a secure file protocol that is used to access, manage, and transfer files over an encrypted SSH transport.

When compared with the traditional FTP protocol, SFTP offers all the functionality of FTP, but it is more secure and easier to configure.

Unlike SCP , which supports only file transfers, the SFTP allows you to perform a range of operations on remote files and resume file transfers.

In this tutorial, we will show you how to use the Linux sftp command.

Before you Begin

To be able to transfer files via SFTP you must have write permission on the remote system.

When transferring large files, it is recommended to run the sftp command inside a screen or tmux session.

The directory from where you run the sftp command is the local working directory.

Don’t confuse SFTP with FTPS. Both protocol serve the same purpose. However, FTPS stands for FTP Secure, and it is an extension to the standard FTP protocol with support for TLS.

Establishing an SFTP connection

SFTP works on a client-server model. It is a subsystem of SSH and supports all SSH authentication mechanisms.

To open an SFTP connection to a remote system, use the sftp command followed by the remote server username and the IP address or domain name:

sftp remote_username@server_ip_or_hostname

If you are connecting to the host using password authentication, you will be prompted to enter the user password.

Once connected, you will be presented with the sftp prompt, and you can start interacting with the remote server:

Connected to remote_username@server_ip_or_hostname.
sftp>

If the remote SSH server is not listening on the default port 22 , use the -P option to specify the SFTP port:

sftp -P custom_port remote_username@server_ip_or_hostname

SFTP Commands

Most of the SFTP commands are similar or identical to the Linux shell commands.

To get a list of all available SFTP commands, type help, or ?.

help

This will output a long list of all available commands, including a short description of each command:

Available commands:
bye                                Quit sftp
cd path                            Change remote directory to 'path'
...
...
version                            Show SFTP version
!command                           Execute 'command' in local shell
!                                  Escape to local shell
?                                  Synonym for help

Navigating with SFTP

When you are logged in to the remote server, your current working directory is the remote user home directory. You can check that by typing:

pwd
Remote working directory: /home/remote_username

To list the files and directories, use the ls command:

ls

To navigate to another directory, use the cd command. For example, to switch to the /tmp directory you would type:

cd /tmp

The above commands are used to navigate, and work on the remote location.

The SFTP shell also provides commands for local navigation, information and file management. The local commands are prefixed with the letter l.

For example, to print the local working directory, you would type:

cd lpwd
Local working directory: /home/local_username

#linux

How to Use SFTP Command to Transfer Files
2.45 GEEK