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:
Type exit if it works.
Connect the the remote system and your prompt will change to an SFTP prompt:
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 :
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 :
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
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:
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 :
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
root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4: tty:x:5: disk:x:6: lp:x:7: . . .
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:
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