Home  >  Article  >  System Tutorial  >  Use the sftp command for secure file transfer under Linux

Use the sftp command for secure file transfer under Linux

PHPz
PHPzforward
2024-02-11 11:21:021331browse

sFTP (Secure File Transfer Program) is a secure, interactive file transfer program that works similarly to FTP (File Transfer Protocol). However, sFTP is more secure than FTP; it handles all operations through encrypted SSH transfers.

It can be configured to use several useful SSH features, such as public key authentication and compression. It connects and logs into the specified remote machine and then switches to interactive command mode, where the user can execute various commands.

In this article, we will show you how to upload/download an entire directory (including its subdirectories and subfiles) using sFTP.

How to transfer files/folders using sFTP in Linux

By default, the SFTP protocol uses the same method as the SSH transport protocol to establish a secure connection to the remote server. Although user authentication uses a password similar to the SSH default, it is recommended to create and use SSH passwordless logins to simplify and more securely connect to remote hosts.

To connect to a remote sftp server, establish a secure SSH connection and create an SFTP session as follows:

$ sftp tecmint@192.168.56.10

After logging in to the remote host, you can run the interactive sFTP command as follows:

sftp> ls            #list directory 
sftp> pwd           #print working directory on remote host
sftp> lpwd          #print working directory on local host
sftp> mkdir uploads     #create a new directory
Use the sftp command for secure file transfer under Linux

How to use sFTP to upload a folder

To upload the entire directory to the remote Linux host, use the put command. However, if the directory name does not exist in the working directory on the remote host, you will receive an error as shown in the screenshot below.

So, first create a directory with the same name on the remote host and then upload it from the local host, the -r parameter allows copying subdirectories and subfiles:

sftp> put -r  Tecmint.com-articles
sftp> mkdir Tecmint.com-articles
sftp> put -r Tecmint.com-articles

Use the sftp command for secure file transfer under Linux
To preserve the modification time, access time, and mode of the transferred file, use the -p flag.

sftp> put -pr Tecmint.com-articles

How to use sFTP to download a folder

To download the entire fstools-0.0 folder from the remote Linux host to the local machine, use the get command with the -r flag as shown below:

sftp> get -r fstools-0.0
Use the sftp command for secure file transfer under Linux

Use sFTP to download the directory

If the folder has been downloaded, then check the working directory of the local machine.

To exit the sFTP shell, enter:

sftp> bye或者sftp> exit

Please note that to prevent a user from accessing the entire file system on the remote host, you can use a chroot Jail to restrict the sFTP user to their home directory for security reasons.

That’s it! In this article, we show you how to upload/download an entire directory using sFTP.

The above is the detailed content of Use the sftp command for secure file transfer under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete