sftp - Linux Command - Unix Command

Transfer files securely with the 'sftp' utility

woman using laptop

JGI / Tom Grill / Blend Images / Getty Images

The sftp utility is an interactive file transfer protocol, similar to ftp, but which performs all operations over an encrypted SSH transport. It may also use many features of SSH, such as public-key authentication and compression. Sftp connects and logs into the specified host then enters an interactive command mode.

Synopsis

The command accepts the following general forms:

sftp [-vC1 ] [-b batchfile ] [-o ssh_option ] [-s subsystem | sftp_server ] [-B buffer_size ] [-Fssh_config ] [-P sftp_server path ] [-R num_requests ] [-S program ] host

sftp [[user@]host[:file [file]]]

sftp [[user@]host[:dir[/]]]

Typical Usage

The sftp utility is most often invoked interactively. That is, you launch the utility in a shell session by specifying a specific location and, optionally, your credentials.

For example, to access server.com, execute:

sftp server.com

The utility obtains the security key for server.com and, if it's the first time you've connected, you'll be asked to add it to your list of known hosts. You'll then be prompted to login in, according to the protocol of the SFTP server on the remote host.

It's convenient to bypass interactive logins by specifying your credentials as part of the connection command. To log into server.com as user test, execute:

sftp test@server.com

You'll be prompted for your password.

Once logged in, you're free to traverse the remote filesystem using standard FTP commands.

Specify a password with a colon after the username. For example, execute sftp test:password@sample.com. Storing passwords in plain text is a security risk, so use this shortcut with caution.

Non-Interactive Sessions

If you specify a file as part of the connection string, then as soon as you're authenticated to the remote server, sftp will obtain the remote file and store it in the current working local directory. For example, the following command will retrieve the file download.txt from server.com by user test and password password:

sftp test:password@sample.com:download.txt

When the file downloads, the session automatically terminates.