Secure File Transfer Using SFTP

In this post, we'll run through the commands that will allow you to use SFTP to securely transfer files from one machine to another using this interactive alternative to SCP.

What is SFTP?

Secure File Transfer Protocol (or sftp as the program is called) is a secure version of the interactive File Transfer Protocol (FTP).
FTP allows you to transfer files to and from a remote machine interactively (i.e. you can interactively change directories on either box, get files from the remote box, and put files on the remote box, list files, create directories etc ...).
Like scp, sftp uses ssh for the underlying data transfer and authentication, so it inherits all the security benefits of ssh.
Also, any auto login keys you've setup for ssh will also apply to sftp, and if you've set up connection sharing, your sftp session will be multiplexed over the master connection.

Starting an SFTP Session

To start an interactive sftp session with a remote host with ip address 192.168.0.7, run:

sftp 192.168.0.7
To start it as a different user on the remote box, (where user is the username you wish to use) run:
sftp user@192.168.0.7
Either of these will start the interactive mode which will show the sftp prompt:
sftp>

Interactive Commands

Below are examples of some of the more common interactive commands.
In interactive mode, all commands are entered at the sftp prompt.
Note that autocompletion (using TAB) will work both locally and remotely.


Changing Directory

To change to a different remote directory, use:

cd
To change to a different local directory, use:
lcd

Print Working Directory

To print the remote working directory, use:

pwd
To print the local working directory, use:
lpwd

Directory Creation

To create a new directory on the remote machine, use:

mkdir /path/to/new_dir_name
To create a new local directory, use:
lmkdir /path/to/new_dir_name
In both cases, leave the path out if you want to create the new directory in the current working directory.

Listing files

To list files in the working directory of the remote machine, use:
ls
To list files in the working directory on the local machine, use:
lls

Get - Retrieving Files from the Remote Machine

To copy files from the remote machine, use the get command. The syntax is:

get [-Ppr] /remote/path/to/file [/local/path/to/save/file]
The remote path is optional, as is the entire local part of the command.
For example to copy a file from the remote working directory to the local working directory (if the file exists), use:
get file_name
To keep the same file permissions and file access times, use the "-p" option:
get -p file_name
To copy files recursively, use the "-r" option:
get -r dir_name

Put - Uploading Files to the Remote Machine

To copy file to the remote machine, use the put command. The syntax is:

put [-Ppr] /local/path/to/file [/remote/path/to/save/file]
The local path is optional, as it the entire remote part of the command.
So for example to upload a file from the current working directory to the remote working directory, use:
put file_name
To keep the same file permissions and file access times, use the "-p" option:
put -p file_name
To upload files recursively, use the "-r" option:
put -r dir_name

Remote Directory and File Removal

To remove a remote directory, use:

rmdir directory_name
To remove a remote file use:
rm file_name

Rename Remote File

To rename a remote file, use:

rename old_file_name new_file_name

Help!

If you get stuck, use the help command:

help
or use the man page

Thanks to T. Ylonen and S. Lehtinen for their work with sftp.