Thursday 14 June 2018

Upload/Download a file using SFTP with a private key

If we want to copy/transfer the file from Windows to Linux then we can use the popular WinScp software. But if we want to transfer files between Linux machines then we can either use FTP (File Transfer Protocol) or SFTP (SSH File Transfer Protocol). This article tells the steps of transferring files using the SFTP protocol with a private key


STEPS



  • Keep the private key of the remote host in a folder in local Linux machine
eg: /home/venkatesh/openssh.pk


  • Change your directory
cd /home/venkatesh/


  • We can use SFTP(SSH File Transfer Protocol) to connect to the remote Linux server and download/upload the files
Usage of sftp command
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 [user@]host[:file ...]
sftp [user@]host[:dir[/]]
sftp -b batchfile [user@]host
 



  • Execute the below command to connect to the remote server
-sh$ sftp -o "IdentityFile=openssh.pk" user@remote.domain.com
We need to pass the private key to SSH command, so whatever we need to give to SSH, we need to include it as a part of -o command line argument.

          -sh$  sftp -oPort=10124 -oPreferredAuthentications=password admin@190.35.180.156



  • Once we execute the above command, we will be connected and will see the sftp prompt like below
sftp>


  • For downloading the file to the local Linux machine, 
sftp> get testFile.txt

This would download the file to your directory from where we have connected to the sftp.


  • Similarly, if we want to upload a file, we need to execute
sftp> put uploadMyFile.txt

No comments:

Post a Comment