Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How to Use SFTP to Securely Transfer Files with a Remote Server

Table Of Contents


    ## Table of Contents

    – [Introduction](#introduction
    – [Prerequisites](#prerequisites
    – [How to Connect with SFTP](#how-to-connect-with-sftp
    – [Getting Help in SFTP](#getting-help-in-sftp
    – [Navigating with SFTP](#navigating-with-sftp
    – [Transferring Files with SFTP](#transferring-files-with-sftp
    – [Simple File Manipulations with SFTP](#simple-file-manipulations-with-sftp
    – [Conclusion](#conclusion

    ## Introduction

    FTP stands for “File Transfer Protocol”. It is a protocol used to transfer files between two remote systems. SFTP stands for SSH File Transfer Protocol or Secure File Transfer Protocol. It is a protocol packaged with SSH and works similarly over a secure connection. The benefit is the ability to use a secure connection to transfer files and traverse the filesystem on both local and remote system.

    Mostly SFTP is better than FTP due to its security features and piggy-back features over SSH connections. Because FTP is an insecure protocol and has to be used only on networks you trust.

    In this guide, we will demonstrate how to use SSH through its Command-line Interface.

    ## Prerequisites
    You must have configured [SFTP server](https://systemongrid.com/guides/how-to-setup-sftp-server-on-ubuntu-18.04 and [FTP server](https://systemongrid.com/guides/how-to-setup-ftp-server-on-ubuntu-18.04 on Ubuntu 18.04.

    You have to set up SSH keys to connect to the machine. You can do this by following our guide, [How to Setup SSH Keys on Ubuntu 18.04](https://systemongrid.com/guides/how-to-setup-ssh-keys-on-ubuntu-18.04.

    ## How to Connect with SFTP

    Basically SFTP uses the SSH Protocol for authentication and for establishing a secure connection also it uses the same Protocol. So that the same authentication methods are available that are in SSH.

    Even though passwords are easy to set up and use, we suggest to create SSH keys.
    And you have to transfer your public key to any system that you want to access because this is more secure and save your time.

    To establish an SSH connection and open up an SFTP session using that connection by entering below command.
    “`
    sftp sam@your_server_ip_or_remote_hostname
    “`
    Then you will redirect to the remote system and you can see the SFTP prompt.
    If you are working on a custom SSH port, then you can open an SFTP session by using below command.
    “`
    sftp -p=custom_port sam@your_server_ip_or_remote_hostname
    “`
    Now you can connect to the remote system as the way of your specified port.

    ## Getting Help in SFTP

    If you want any information regarding SFTP access then you can follow the below command.
    “`
    help
    ?
    “`
    You will get the list of available commands as below output.

    ![help](https://grid.media/assets/images/help.png

    ## Navigating with SFTP

    Now you can use some commands to navigate through the remote system’s file hierarchy. For example, If you want to find the current directory in a remote system, then you can use the below command.
    “`
    pwd
    “`

    ![pwd](https://grid.media/assets/images/pwd.png

    If you want to see the contents of the directory, then you can enter the below command.

    “`
    ls
    “`

    ![ftpoutput](https://grid.media/assets/images/ftp-output-02132019.png

    If you want to know some essential options use the below command.
    “`
    ls -la
    “`

    ![lsla](https://grid.media/assets/images/ls-la.png

    If you want to change from one directory to another directory then use the below command.
    “`
    cd ftp
    “`
    Now you can access the remote system, but if you need to access your local system, then you can use commands towards the local system by preceding them with “l”.

    All commands are having equivalent local commands. If you want to print the local working directory then enter the below command.
    “`
    lpwd
    “`

    ![lpwd](https://grid.media/assets/images/lpwd.png

    If you want to list the contents of the current directory on the local machine use the below command.
    “`
    lls
    “`

    ![lls](https://grid.media/assets/images/lls.png

    Use the below command to change the directory on the local machine.
    “`
    lcd Desktop
    “`
    ## Transferring Files with SFTP

    Navigation between local and remote systems is of two types

    ## Transferring Remote Files to the Local System

    If you want to download files from our remote host, you can enter the below command
    “`
    get remoteFile
    “`

    ![getremotefile](https://grid.media/assets/images/get-remotefile.png

    Basically “get” command downloads a remote file to a file with same in the local file system. You can copy the remote file to another name by specifying the name afterward.
    “`
    get remoteFile localFile
    “`
    The “get” command takes some option flags. For example, you can copy a directory and all of its contents by specifying the r option stands for recursive.
    “`
    get -r someDirectory
    “`
    You can direct SFTP to maintain the appropriate permissions and access times by using the “-p” flag.
    “`
    get -Pr someDirectory
    “`
    ## Transferring Local Files to Remote System

    To transfer files to remote system use the below command.
    “`
    put localFile
    “`

    ![putlocalfile](https://grid.media/assets/images/put-localfile.png

    You can apply “put” command that works with “get”. To copy an entire local directory, you can enter the below command.
    “`
    put -r localDirectory
    “`
    If you want to check how much space is available to transfer, use the below command.
    “`
    df -h
    “`

    ![dfh](https://grid.media/assets/images/df-h.png

    The “!” command will direct into a local shell. So you can run any command. If you want to check disk usage, enter the below command.
    “`
    !
    df -h
    “`
    Then you will get output like this.

    ![diskusage](https://grid.media/assets/images/disk-usage-02132019.png

    Enter “exit” command to return to your SFTP session.
    “`
    exit
    “`
    Now you can see the SFTP prompt.

    ## Simple File Manipulations with SFTP

    SFTP performs the type of basic file maintenance, which uses when working with file hierarchies. If you want to change the owner of a file which is on the remote system, Use the below command.
    “`
    chown userID file
    “`
    SFTP command will not accept usernames, it accepts UIDs. There is no way to know the appropriate UID in the SFTP Interface.
    By using the below command you may get to know UID.
    “`
    get /etc/passwd
    !less passwd

    “`

    The “!” command used as a prefix for local shell command in place of giving it by itself. It works to run any command available on our local machine and also been used with local “df” command earlier.

    The UID will be in the third column of the file and delimited by a colon. If you want to change the owner of the group file use the below command.
    “`
    chgrp groupID file
    “`
    If you want to know the list of remote system’s groups, use the below command.
    “`
    get /etc/group
    !less group
    “`

    Group ID, which is in the third column merged with the name in the first column as below. The “chmod” command works as same as on the remote file system.
    “`
    chmod 777 publicFile
    Changing mode on /home/demouser/publicFile
    “`
    Here, there is no specific command for manipulating local file permissions, but you we can set the local unmask which can copy the files to the local system so that it will have appropriate permissions.

    To so that use the below command.
    “`
    lumask 022
    Local umask: 022
    “`
    Now all standard files downloaded may have 644 permissions.
    SFTP can allow you to create directories on both local and remote systems with “lmkdir” and “mkdir” respectively.

    The remaining commands target only remote filesystem.
    “`
    ln
    rm
    rmdir
    “`
    If you want to perform these actions on local file system then you can drop a shell by using the below command.
    “`
    !
    “`
    If you want to execute the single command on the local system then use “!” preceding every command like this.
    “`
    !chmod 644 somefile
    “`
    If you want to exit from the session then use “exit” or “bye” to close the connection.
    “`
    bye
    “`
    ## Conclusion

    Even though SFTP is a simple tool, it is useful for maintaining servers and transferring files between them.