How to Transfer Files using SFTP

September 18, 2023 / How-to Guide

In this article, we will explain how to transfer your files using  SFTP.

SFTP is known as SSH File Transfer Protocol or Secure Socket Shell File Transfer Protocol. It is a secure method for transferring files between a client and a server. It uses SSH (Secure Shell) to provide a secure means of accessing a remote server and transferring files securely.

  1. Before proceeding, it’s important to set up SSH. To confirm whether SSH is configured on your server, follow these steps-
    $ ssh eukhost@your_server_ip_or_remote_hostname.

    Note: You need to replace “eukhost” with your username and “your_server_ip_or_remote_hostname” with your server IP or hostname

    1. If it does not log in, then you need to set up SSH access first then you need to exit from the prompt.
      $ exit
    2. Creating an SFTP Session: You can connect to the SFTP session by using the following command.
      $ sftp eukhost@your_server_ip_or_remote_hostname
    3. If you are using a custom SSH port (not the default port 22), then you can also use the following command to connect to SFTP.
      $ sftp -oPort=customport eukhost@your_server_ip_or_remote_hostname.

      Here, you need to change “customport” to the port number that you are using and this command will connect you to the SFTP with the port you have specified.

  2. Transferring Files with SFTP
    1. How to Transfer Remote Files to Local System
      To transfer files from a remote host, you can do so using the following command.

      sftp> get remote-file

      Here, the remote file will be the name of the files that you need to transfer. When using the “get” command, it will download the “remote file” to your local system, preserving the same name as it had on the server.

    2. To download the “remote file” from a different remote host to your machine, you can give a different name after the remote file’s name.
      sftp> get remote-file local-file
      Output 
      
      Fetching home/eukhost/remote-file to remote-file
      
      /home/geeksforgeeks/remote-file 100% 100KB 50.0KB/s 12:05
      
      
    3. Talking about the “get” command it also supports various flags as options. For example, if you want to copy any directory along with all its contents, you need to use the “-r” recursive flag.
  3. How to Transfer Local Files to the Remote System
    You can easily transfer files from the local system to the remote system by using the “put” command.

    sftp> put localFile
    Output
    
    Uploading local-file to /home/eukhost/local-file
    
    local-file 100% 8080 10.4KB/s 12:00
    1. If you see, the “put” command supports the same optional flags as “get.” This means that to copy a directory with all its files, you can use the following command:
      sftp> put -r local-directory

      SFTP is a simple way where you can use the FTP or SCP to transfer between local and remote files and folders.

Spread the love