sFTP or Secure File Transfer Protocol is a convenient upgrade from the traditional FTP (File Transfer Protocol). The sFTP program boasts of improved security layers since it mirrors encrypted SSH transport protocols in its file transfer operations.
The convenience of sFTP is in its flexible adaptation to useful SSH features like compression and public key authentication.
Once you successfully gain access to your remote machine or server via sFTP, this program takes its users to an interactive command line/terminal mode session where you can comfortably use its defined command syntax to execute the needed FTP-related operations.
This article seeks to demonstrate the security and convenience of sFTP in downloading a directory (folders and subfolders) from a remote machine/server.
sFTP and File Transfer
There are two sFTP approaches to securely connect to your remote machine via the SSH transport layer. One is through password authentication and the other through SSH passwordless login.
Going with SSH passwordless login is a recommended option since it is more secure and you won’t have to keep remembering your login password whenever you need access to your remote server.
Configuring Remote SSH Passwordless Access
1. Generate an SSH key on your local/current machine.
$ sudo ssh-keygen -t rsa
Skip the Enter passphrase: step by hitting [Enter] on your keyboard.
2. Next, copy generated SSH key to the remote machine.
$ sudo ssh-copy-id [email protected]
3. Test SSH passwordless access to your remote server.
$ sudo ssh [email protected]
SSH passwordless login was a success!
Attempting sFTP Passwordless Access
Log out from the server and attempt remote access via sFTP.
$ sudo sftp [email protected]
As expected, sFTP server access was a success. From the resulting sFTP console, you can execute common Linux terminal-based interactive commands like ls, pwd, lpwd, and mkdir.
sftp> ls
You should be able to navigate to & identify the targeted directory on the remote server which you wish to download. Since it is a directory, there is a high possibility of more folders and subfolders with files existing within it.
sftp> ls -l LinuxShellTips_Backup
Download a Linux Remote Directory Using sFTP
On my end, the targeted remote directory for download is highlighted as LinuxShellTips_Backup. To download this whole directory to my local machine, I will make use of the get command.
On its own, the get command is effective in downloading remote files and folders (without subfolders). Since we are dealing with a directory that might have multiple files and folders within folders, the get command needs the assistance of the -r
(recursive) option to ensure all files, folders, and subfolders are downloaded.
sftp> get -r LinuxShellTips_Backup
The final step is to exit sFTP and check for the existence of the downloaded directory on the current working directory of the local machine you are using.
sftp> exit $ ls -l LinuxShellTips_Backup
Upload Directory to Remote Server Using sFTP
If you are curious about uploading a file to a remote server via sFTP, first make sure the remote server has the same directory name as the one you are about to upload.
If you are certain that the directory names are a match, use the put command together with the recursive option -r
for successful remote directory upload.
sftp> put -r LinuxShellTips_Backup
You can now comfortably and securely download and upload directories via sFTP in Linux.