Using sftp from the Linux command line

29/12/2020
Chưa phân loại
In this article sftp, an alternative to scp will be explained. As said in our SCP’s article, both scp and sftp work through the ssh protocol in contrast to FTPS (MS Windows) encrypted through SSL. The ssh protocol is encrypted and the information traveling from a device to another through it is safe in contrast to protocols like telnet or ftp which are unencrypted allowing sniffers to interfere the data as shown previously when we explained how to sniff images being transferred within an unencrypted network or protocol.

While scp only allow us to transfer files from a device to other with sftp we can edit file attributes on the remote device, create and remove directories, to stop and to resume the transference, to browse remote directories and to use it through graphical and friendly interfaces, of course to share files with other users a SFTP server would be advantageous over scp.

Getting started with sftp

SFTP’s syntax is very similar to FTP. In contrast to SCP we first need to connect to the server to give instructions. To connect to a server without specifying any parameters on the console type:

sftp <username>@<host/ip-addess>

Where:

sftp = calls the program
<username> = replace it for your username (e.g root)

@<host/ipaddress> = replace it for your server ip or host.

Note: replace the instructions between < > for the proper information and type the password when asked.

As you can see in the screenshot sequence, after running “ls” I found no available files or directories within the server. Therefore, let’s upload a file from the local device to the server, type:

put </path/to/file>

Where:

put = instructs to upload a file from the local computer to the server.

</path/to/file> = specify the file and it’s location on the local device.

If we want to download a file, type:

get <filename>

The file will be downloaded to our local directory unless we specify a different one. To know what is our local directory from sftp type:

lpwd

To check the current directory on the server type:

pwd

We are on the home directory for the user shared on the local device and on the home directory for the user linuxhint on the remote device.

To download a file specifying a different location for it to be saved type:

get <fileOnServer> </local/directory/to/save>

As you see files now were transferred to the directory Downloads.

Sftp also allow us to transfer directories recursively (including it’s subdirectories), to transfer a directory recursively we only need to specify the parameter -r, if there is not an available directory on your server create it typing “mkdir testdirectory” , then, to download the directory type:

get -r <Directory>  </path/to/save>

As you can see in the screenshot, we downloaded the directory called testdirectory to our local directory called Downloads within shared home. If we don’t specify a local location for the directory or file we download it will be saved on the local directory (the one you check with lpwd) like in the next example:

We can also save files and directories with a different name than the original, type:

get -r <Directory>  <DirectoryNewName>

We can also upload directories from our local device using put, let’s upload the directory testdirectory2, a copy of the source with a different name by typing:

mkdir <nameOfDirectoryToUpload>
put <<nameOfDirectoryToUpload>>

 

Those are the basic commands to use sftp, by running “man sftp” you can see all available options and parameters. You can also type <help> once logged into a server.

I hope you found this tutorial useful to start using sftp from the command line. Keep following LinuxHint for additional tips and updates on Linux.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Install Piggydb – A Flexible Diary Notebook DB on Linux

Piggydb is a flexible and scalable knowledge building platform that supports a heuristic or bottom-up approach to discover...
28/12/2020

How to Change IP Address on Ubuntu

In this post we will try to change the IP address of a Ubuntu based Linux system from the command line. Though we will be...
12/02/2020

Install Jetbrains GoLand Go IDE on Ubuntu

GoLand is an amazing Go IDE by JetBrains. If you love other JetBrains IDEs then, you will love GoLand as well. GoLand will...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Thuê mua proxy V6 uy tín, chất lượng tại đâu?
11/05/2024

Tổng hợp mẹo thuê mua proxy Tiktok đạt hiệu quả cao
11/05/2024

Thuê mua proxy xoay ở đâu uy tín, chất lượng, giá tốt?
11/05/2024

Nâng cao trải nghiệm trò chơi khi thuê mua proxy game tại Onet
10/05/2024

Thuê mua proxy Airdrop tốc độ nhanh, hiệu quả cực tốt
10/05/2024