How To Use SFTP to Transfer Files to/from a Remote Server

Can you help?

SFTP is very easy to use, but if you need help or you are unable to connect with SFTP to your ZetServers VPS or dedicated server, just open a ticket and our 24/7 available staff will help you right away! Also don’t forget to check our current SSD VPS , DDoS Protected VPS and Dedicated Servers offer ! We have great discounts and promotions running!  You can get your server running almost for free!

SFTP

SFTP (SSH File Transfer Protocol or Secure File Transfer Protocol) is a protocol packaged with SSH that works like FTP, but over a secure connection. The advantage is the ability to transfer files securely from or to a remote server.

In almost all cases, SFTP is preferable to FTP because of its underlying security features and ability to piggy-back on an SSH connection. FTP is an insecure protocol and should be used only on local networks without public access.

How to use SFTP?

SFTP uses the SSH protocol to authenticate and establish a secure connection. Same authentication methods are available that are present in SSH. If you are able to SSH into your server, then you have completed all of the necessary requirements necessary to use SFTP to manage files.

SSH command to connect and login to your server:

ssh username@IP_OR_Hostname

SSH command to logout and disconnect from your server:

exit

Command to connect, login to SSH and open a SFTP prompt

sftp username@remote_hostname_or_IP

You will connect the the remote system and your prompt will change to an SFTP prompt.

SFTP Commands

 

SFTP Help command

help
?

This will display a list of the available commands:

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
help                               Display this help text
lcd path                           Change local directory to 'path'
. . .

Command to check working directory with SFTP:
ls
Summary.txt     info.html       temp.txt        testDirectory

SFTP Command to check working directory and list files with details

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
. . .

SFTP Command to move to another directory:

cd testDirectory

Print the local working directory with SFTP command:

lpwd
Local working directory: /Users/demouser

SFTP Command to list the contents of the current directory on the local machine:

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

SFTP Command to change the directory we wish to interact with on the local system:

lcd Desktop
 SFTP Command to download files from your remote host:
get remoteFile
Fetching /home/demouser/remoteFile to remoteFile
/home/demouser/remoteFile                       100%   37KB  36.8KB/s   00:01

Copy the remote file to a different name by specifying the name afterwards:

get remoteFile localFile

The “get” command also takes some option flags. We can copy a directory and all of its contents by specifying the recursive option:

get -r someDirectory

We can tell SFTP to maintain the appropriate permissions and access times by using the “-P” or “-p” flag:

get -Pr someDirectory

SFTP “put” command for transferring files from local to the remote system:

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
Check that you have enough space to complete the transfers you are interested in with the “df” SFTP command:
df -h
    Size     Used    Avail   (root)    %Capacity
  19.9GB   1016MB   17.9GB   18.9GB           4%

The “!” command drops us into a local shell, where we can run any command available on our local system. We can check disk usage by typing:

!
df -h
Filesystem      Size   Used  Avail Capacity  Mounted on
/dev/disk0s2   595Gi   52Gi  544Gi     9%    /
devfs          181Ki  181Ki    0Bi   100%    /dev
map -hosts       0Bi    0Bi    0Bi   100%    /net
map auto_home    0Bi    0Bi    0Bi   100%    /home

Any other local command will work as expected. To return to your SFTP session, type:

exit

You should now see the SFTP prompt return.

Change the owner of a file on the remote system with the command:
chown userID file

Notice how, unlike the system “chmod” command, the SFTP command does not accept usernames, but instead uses UIDs. Unfortunately, there is no easy way to know the appropriate UID from within the SFTP interface.

Work around could be accomplished with:

get /etc/passwd
!less passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
. . .

Change the group owner of a file with:

chgrp groupID file

Again, there is no easy way to get a listing of the remote system’s groups. We can work around it with the following command:

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.

Thankfully, the “chmod” command works as expected on the remote file system:

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 on both local and remote systems with “lmkdir” and “mkdir” respectively. These work as expected.

The rest of the file commands target only the remote filesystem:

ln
rm
rmdir

These commands replicate the basic behavior of the shell versions. If you need to perform these actions on the local file system, remember that you can drop into a shell by issuing this command:

!

Or execute a single command on the local system by prefacing the command with “!” like so:

!chmod 644 somefile

SFTP Close or Exit command:

bye

SFTP is very useful for administrating servers and transferring files. SFTP is a good way to leverage the strengths of FTP or SCP.

LEAVE A COMMENT