How to transfer files securely using sftp (examples included)

The sftp command is a secure alternative to ftp and is functionally the same as ftp. Use sftp instead of ftp when logging on to a server that is running the OpenSSH daemon, sshd. The major difference between sftp and ftp is that the former uses encryption to transfer password over network whereas the later does not.

sftp syntax

The format to connect to a remote system is:

# sftp [options] [user@]host

Enter help or ? to display a list of sftp commands.

sftp> help
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 [-Ppr] remote [local]          Download file
reget remote [local]  Resume 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 [-s] oldpath newpath            Link remote file (-s for symlink)
lpwd                               Print local working directory
ls [-1afhlnrSt] [path]             Display remote directory listing
lumask umask                       Set local umask to 'umask'
mkdir path                         Create remote directory
progress                           Toggle display of progress meter
put [-Ppr] local [remote]          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
sftp> 

Connecting remote system using sftp

The following example assumes that you are logged on to your local system as user testuser and are connecting to a remote system 192.168.219.149:

$ sftp testuser@192.168.219.149
Connecting to 192.168.219.149...
testuser@192.168.219.149 password:
Connected to 192.168.219.149.
sftp>

After providing the correct password, you are presented with an sftp> prompt as shown. Enter help or ? to display a list of available commands.

Navigating directories and listing files

1. To find the current directory on the local server:

sftp> lpwd
Local working directory: /root

2. To find the current working directory on the remote host:

sftp> pwd
Remote working directory: /root

3. Similarly, to change the directory on local server use lcd command :

sftp> lcd /tmp

4. To change the directory on remote server use cd command:

sftp> cd /tmp

5. To list files in the current directory on the remote server :

sftp> ls
anaconda-ks.cfg         initial-setup-ks.cfg    test

6. To list files in the current directory on the local server :

sftp> lls
file1  file2  file3

Creating and removing directories

1. To create a new directory on the remote server:

sftp> mkdir data

2. To create a new directory on the local server:

sftp> lmkdir testdir

Upload files and directories using sftp

1. The following example uploads a file, or copies the file from the local system to the remote system:

sftp> put file1

2. To transfer more than one files to remote host use the mput (multiple put) command.

sftp> mput file1 file2 file3

3. To be able to transfer directory to remote host, you have to first create a directory on the remote host and the start the transfer.

sftp> mkdir /dir
sftp> put -r dir/

Download files or directories using sftp

1. To Download a single file from the remote host use the get command.

sftp> get file1

2. To download multiple files use the below command.

sftp> mget file1 file2 file3

3. To download a directory recursively (with all its contents) :

get -r dir

Exit or quit sftp

Enter exit, quit, or bye to close the connection and exit sftp.

sftp> bye

or

sftp> quit

or

sftp> exit
Related Post