What is SFTP?

The full form of SFTP is the Secure file transfer protocol. It is also named as SSH FTP. SSH is for secure connection and FTP is for file transfer protocol. This clearly says that two protocols work together to transfer a file between two computers connected via an IP network.  According to the OSI model, both are examples of application layer protocols.  Here we will explain the SFTP in detail along with the use case examples.

What is FTP?

FTP is the protocol for file transfer. In an IP network, multiple computer machines connect each other over the IP network.  A machine may be physically reachable or might be a remote machine.  If it is a remote machine then the file can be transferred over the IP network using an application. This results in the requirements for developing a mechanism for file transfer. FTP is the protocol that runs over TCP/IP for file transfer. A file transfer application can use the services of protocol for sending and receiving a file.  It may perform more functions other than just transfer. E.g during the FTP session, the remote directories can be browsed, file on a remote machine can be deleted, etc. This makes  FTP a kind of terminal where many commands can be executed.

What is SSH?

SSH is the protocol that sets up secure communication between the two hosts on an IP network. Before ssh (with FTP), all the information was transferred over the wire in plain text. Plain text always opens security threats.  To make file transfer more secure.  The development of the ssh protocol was done. With ssh the remote login becomes secure. SSH uses key-based authentication with the server and it encrypts data between the hosts.

As ssh becomes very popular protocols for secure communication. Other protocols start using ssh for secure communications. SFTP is one of them when FTP starts using SSH it becomes SFTP.  So FTP works as a user of SSH.

Sftp port number :

SFTP uses TCP for network communication, like other TCP users SFTP also needs a port number. A client connects to the distance SFTP server. So there should be a standard port too for the service. Yes, there is a standard port for the service. The standard SFTP port number is 22. But the port number is configurable. This adds more security to the server. Only people to whom the detail has been shared can connect to the server. Port number 22 is the port for SSH. As SFTP runs over SSH, so ssh port is used for all communications while transferring a file.

How to use SFTP?

Till now we have discussed the theoretical part of the protocol.  Now it is a good time to start with a few examples for the usage of SFTP. Here we will use the CentOs Linux for demonstration.  Each example covers the commands and theory behind the command.

Check if SFTP is running? 

This is the first step. If no FTP and SSH software is installed, then we can not run the protocol.  So checking first.  Most of the Linux systems come with the SFTP server installed. Server start running with the system startup. But still, chances that there are missing packages for FTP and SSH.

Following is the command for checking the SFTP server status.

[[email protected]_6_64]# ps -ef | grep ftp root 12497 12495 0 01:47 ? 00:00:00 /usr/libexec/openssh/sftp-server root 13144 12443 0 02:37 pts/0 00:00:00 grep ftp

The output of the above command shows that the FTP server is running.  If no server is running then we can check two packages installed from the following commands.

[[email protected]_6_64-159 ~]# rpm -qa | grep ssh
openssh-clients-5.3p1-104.el6.x86_64
trilead-ssh2-213-6.2.el6.noarch
openssh-5.3p1-104.el6.x86_64
libssh2-1.4.2-1.el6.x86_64
openssh-askpass-5.3p1-104.el6.x86_64
openssh-server-5.3p1-104.el6.x86_64
For FTP
[[email protected]_6_64-159 ~]# rpm -qa | grep ftp
gvfs-obexftp-1.4.3-18.el6.x86_64
ftp-0.17-54.el6.x86_64

If any of the commands don’t give output then the corresponding (ssh or FTP) can be installed from yum.  Now the server is ready with accepting SFTP connection from the remote clients.

SFTP client Linux:

The secure file transfer model is client/server-based. We have shown that there the server is running. Now its time to show how an SFTP client will connect to the server. We are using an example from Linux. Which provides a command-line based interface for an SFTP client. For Windows operating system, we have easy to use GUI based clients. We will discuss later or in another post about the SFTP client for Windows.  To show easy to use an example, we have two Centos 6 machines, one has IP address 192.168.1 159 and the other is 192.168.1.160. The first machine will work as an SFTP server and other as a client. The following are the commands for secure file transfer.

SFTP help:

Like many other commands, there is a help to list all possible options in the command.  There are two kinds of help options, one is before connecting and another is after connecting.  For the first one just run the SFTP client with no parameters.

[[email protected]_6_64-160 ~]# sftp
usage: sftp [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]
[-o ssh_option] [-P sftp_server_path] [-R num_requests]
[-S program] [-s subsystem | sftp_server] host
sftp [[email protected]]host[:file ...]
sftp [[email protected]]host[:dir[/]]
sftp -b batchfile [[email protected]]host
[[email protected]_6_64-160 ~]#

For help after clients connected to the server.

 sftp [email protected]
Connecting to 192.168.1.159...
[email protected]'s password:
sftp> ?
Available commands:
bye Quit sftp
cd path Change remote directory to 'path'
chgrp grp path Change group of file 'path' to 'grp'
chmod mode path Change permissions of file 'path' to 'mode'
chown own path Change owner of file 'path' to 'own'
df [-hi] [path] Display statistics for current directory or
filesystem containing 'path'
exit Quit sftp
get [-P] remote-path [local-path] Download file
help Display this help text
lcd path Change local directory to 'path'
lls [ls-options [path]] Display local directory listing
lmkdir path Create local directory
ln oldpath newpath Symlink remote file
lpwd Print local working directory
ls [-1aflnrSt] [path] Display remote directory listing
lumask umask Set local umask to 'umask'
mkdir path Create remote directory
progress Toggle display of progress meter
put [-P] local-path [remote-path] Upload file
pwd Display remote working directory
quit Quit sftp
rename oldpath newpath Rename remote file
rm path Delete remote file
rmdir path Remove remote directory
symlink oldpath newpath Symlink remote file
version Show SFTP version
!command Execute 'command' in local shell
! Escape to local shell
? Synonym for help

The command for connecting the server  with no file transfer :

[[email protected]_6_64-160 ~]# sftp [email protected]
Connecting to 192.168.1.159...
[email protected]'s password:
sftp>

sftp> exit
[[email protected]_6_64-160 ~]#

The above command connects to the server, during a connection it prompts for the password.

The command for sending a file from the command line:

[[email protected]_6_64-160 tmp]# sftp [email protected]
Connecting to 192.168.1.159...
[email protected]'s password:
sftp> cd /tmp
sftp> put file.txt
Uploading file.txt to /tmp/file.txt
file.txt 100% 0 0.0KB/s 00:00
sftp> exit
[[email protected]_6_64-160 tmp]#
In the above example file.txt, present in /tmp on the client machine is transferred to the /tmp directory on the remote server machine.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top