How to use SFTP to securely transfer files over a remote server
The SSH file transfer protocol is a network protocol that allows users to view, pass, and navigate files through an encrypted data stream (also known as the Secure File Transfer Protocol).
This tutorial will walk you through the process of moving content between two servers in a safe manner.
You will be needing
- SSH access
- Basic skills for working on Linux
Connecting to a server using SFTP
To make sure that you will be able to access the server with SFTP you can first try to establish an SSH connection.
ssh user1@X.X.X.X (where X.X.X.X is the IP address of the remote server).
Instead of an IP address, a hostname may be used.
If the link is successful, you should have no problems. In this tutorial, we’ll be using the default SFTP client that comes with the server.
This will establish a connection in default SSH port22. Due to security reasons if the port is configured with a non default SSH port, you can use the oPort option to specify the exact port:
sftp -oPort=2222 user1@X.X.X.X
Depending on the remote server’s SSH authentication procedure, you should be prompted to enter the password for user1 or the password for the SSH private key loaded on the connecting server.
The command prompt is turned on when the link is connected:
Transferring Files with SFTP
You can use the pwd and lpwd commands to find out which are the working folders on the current and remote server:
Remote working directory: /home/user1
Local working directory: /home/currentuser
Using the following command to download a file with the get command.
|sftp> get testfile.txt
The display should look something like this:
|Fetching /home/user1/testfile.txt to testfile.txt
/home/user1/testfile.txt 100% 3209 3.1KB/s 00:00
We can initiate folder download using the -r option:
|sftp> get -r testfolder
The corresponding upload commands for a file and folder are:
|sftp> put testfile.txt
sftp> put -r testfile.txt