How to use SFTP to securely transfer files over a remote server
This tutorial will guide you through the process of securely transferring content between two servers. In computing, the SSH File Transfer Protocol (also Secure File Transfer Protocol, or SFTP) is a network protocol that provides file access, file transfer, and file management over any reliable data stream.
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.
where X.X.X.X is the remote server’s IP address. Instead an IP address a host-name can be used as well.
You should not experience any problems if the connection is successful. In this tutorial, We will be using the default SFTP client included in the server configuration.
sftp [email protected]
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 [email protected]
Depending on the SSH authentication method set on the remote server you should be prompted to fill in the password related to user1 or the passphrase related to the SSH private key loaded on the connecting server.
When the connection is made the command prompt will be switched to:
sftp>
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:
1
2 3 4 |
sftp> pwd
Remote working directory: /home/user1 sftp> lpwd Local working directory: /home/currentuser |
For downloading a file using get command , use the following command.
1 | sftp> get testfile.txt |
The output should be similar to the following one:
1
2 |
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:
1 | sftp> get -r testfolder |
The corresponding upload commands for a file and folder are:
1
2 |
sftp> put testfile.txt
sftp> put -r testfile.txt |