FTP, or “File Transfer Protocol” is a protocol of transferring files between two remote systems.

Secure File Transfer Protocol is a variant of the FTP protocol that “tunnels” the session through a Secure Shell (SSH) connection to secure it. Because FTP uses multiple TCP connections (which is an exception in the list of TCP / IP protocols), it is particularly difficult to perform this tunneling.

The name of Secure FTP should not be confused with other methods of securing FTP, such as FTPS (SSL) with certificate of authentication and encryption.

Although SFTP is integrated into many graphical tools, this tutorial will demonstrate how to use it through its interactive command line interface.

Connect with SFTP

Test SSH access with the following command:

ssh username@remote_hostname_or_IP

Type exit if it works.

Connect the the remote system and your prompt will change to an SFTP prompt:

sftp username@remote_hostname_or_IP

Navigating with SFTP

A number of specific command is available to allow you navigating through SFTP.

Get the current directory:

Remote working directory: /home/demouser

Display the contents of the current directory:

Readme.txt     index.html       temp.txt        rootDirectory

The commands within the SFTP interface implement some optional flags :

ls -la
drwxr-xr-x    5 demouser   demouser       4096 Aug 13 15:11 .
drwxr-xr-x    3 root     root         4096 Aug 13 15:02 ..
-rw-------    1 demouser   demouser          5 Aug 13 15:04 .bash_history
-rw-r--r--    1 demouser   demouser        220 Aug 13 15:02 .bash_logout
-rw-r--r--    1 demouser   demouser       3486 Aug 13 15:02 .bashrc
drwx------    2 demouser   demouser       4096 Aug 13 15:04 .cache
-rw-r--r--    1 demouser   demouser        675 Aug 13 15:02 .profile
. . .

Jump to another directory :

cd testDirectory

You can print the local working directory:

Local working directory: /Users/demouser

List All the contents of the current directory on the local machine:

Desktop			local.txt		test.html
Documents		analysis.rtf		zebra.html

Transferring Files with SFTP

Downloading files from the remote host

get remoteFile
Fetching /home/demouser/remoteFile to remoteFile
/home/demouser/remoteFile                       100%   37KB  36.8KB/s   00:01

Note: the “get” command downloads a remote file to a file with the same name on the local file system.

Specifying different name:

get remoteFile localFile

The “get” command also takes some option flags : -r, -Pr (see details with the help command)

Transferring Local Files to the Remote System

The “put” command:

put localFile
Uploading localFile to /home/demouser/localFile
localFile                                     100% 7607     7.4KB/s   00:00

The same flags that work with “get” apply to “put”. So to copy an entire local directory, you can issue:

put -r localDirectory

You can check if you have enough space to complete the transfer before using the following command :

df -h
    Size     Used    Avail   (root)    %Capacity
  19.9GB   1016MB   17.9GB   18.9GB           4%

Return to your SFTP session, type: exit.

Simple File Manipulations with SFTP

Change the owner of a file on the remote system:

chown userID file

Similarly, we can change the group owner of a file with:

chgrp groupID file

Get a listing of the remote system’s groups :

get /etc/group
!less group
. . .

The third column holds the ID of the group associated with name in the first column. This is what we are looking for.

“chmod” command :

chmod 777 publicFile
Changing mode on /home/demouser/publicFile

There is no command for manipulating local file permissions, but you can set the local umask, so that any files copied to the local system will have the appropriate permissions.

That can be done with the “lumask” command:

lumask 022
Local umask: 022

Now all regular files downloaded (as long as the “-p” flag is not used) will have 644 permissions.

SFTP allows you to create directories

“lmkdir” and “mkdir” commands :

For example, the following command would create three directories within the current directory (i.e., the directory in which the user is currently working) with the names dir_1, dir_2 and dir_3:

mkdir dir_1 dir_2 dir_3

Leave a Reply