How to Transfer Files using SFTP?
SFTP (Secure File Transfer Protocol) or SSH (Secure Shell) File Transfer Protocol) is a file transfer protocol used to transfer files between client and server. It uses SSH(Secure Socket Shell) and is also known as Secure Socket Shell File Transfer Protocol. It provides secure access to a remote server for the secure transfer of files.
Secure FTP arose to meet the necessities for enhanced security with tunneling. It uses Secure Shell 2 (SSH2) to create a secure tunnel and emulate an FTP connection to provide a firewall-friendly and encrypted channel to transfer files using the popular TCP port 22. SSH offers enhanced security by having the entire file transfer session, including all session control commands, entirely encrypted at all times while only needing a single port to be opened on your firewall instead of the two ports that need to be opened for FTP and SSL connections.
SFTP uses the SSH protocol to transfer files. You’ll need to configure SSH first. To check if you have SSH configured. Do the following on your server
$ ssh geeksforgeeks@your_server_ip_or_remote_hostname.
Replace “geeksforgeeks” with your username and “your_server_ip_or_remote_hostname” with your server IP or hostname.
If that works and you’re logged in. You’re good to go. If not you’ll need to set up SSH access first.
Then exit from the prompt.
Establishing SFTP Session :
We can connect to the SFTP session by using the following command.
$ sftp geeksforgeeks@your_server_ip_or_remote_hostname
This command will connect you to the remote session and the prompt will change to SFTP prompt.
If you’re using a custom SSH port (not the default port 22), then you can use the following command to connect to SFTP.
$ sftp -oPort=customport geeksforgeeks@your_server_ip_or_remote_hostname.
Here, change “customport” to the port number that you are using. This command will connect you to the SFTP with the port you specified.
Transferring Files with SFTP :
1. Transferring Remote Files to Local System –
If we want to transfer files from our remote host, we can do so by using the following command.
sftp> get remote-file
Output Fetching home/geeksforgeeks/remote-file to remote-file /home/geeksforgeeks/remote-file 100% 40KB 39.8KB/s 00:05
Here, the remote file will be the name of the files that you want to transfer. The get command will download the “remotefile” on your local system with the same name as on the server.
We can download the remotefile with a different remote host to our machine by specifying the name after the remote file name.
sftp> get remote-file local-file
The get command can also take some flags as options. For example, if we want to copy a directory with all of its content we can use the “-r” recursive flag.
sftp> get -r some-directory
2. Transferring Local Files to the Remote System –
Transferring files from the Local System to Remote System can be achieved easily by using the put command.
sftp> put localFile
Output Uploading local-file to /home/geeksforgeeks/local-file local-file 100% 7607 7.4KB/s 00:00
Put can use the same options flags that get can use. So, to copy an entire directory will all of its files you can use.
sftp> put -r local-directory
SFTP is a better way to use the strength of FTP or SCP to perform transfers between local and remote files and folders.