How to Use SFTP Command to Transfer Files

No offense, but are you still using the same old standard FTP (File Transfer Protocol) to exchange files from a remote system?

It’s been a five-decade since FTP came, now we have a more appropriate alternative with good security algorithms without compromising the speed.

SFTP synonyms (Secure File Transfer Protocol) are among the most acceptable applications in the market, providing a secure connection to exchange your file remotely. It works on top of SSH protocol, which is recognized for its security and uses the same protocol and port.

Although many GUI tools and applications are available across the operating systems, today, we will demonstrate it using the classical CLI method.

Installation

To start using SFTP, you need to set up ssh in both local and remote systems. If SSH is not installed in both systems, use the reference link below.

Read: How to install OpenSSH server in Ubuntu

Read: How To Secure The OpenSSH Server?

Now execute the below command to check whether it is running or not.

$ sftp
usage: sftp [-46aCfpqrv] [-B buffer_size] [-b batchfile] [-c cipher]
          [-D sftp_server_path] [-F ssh_config] [-i identity_file]
          [-J destination] [-l limit] [-o ssh_option] [-P port]
          [-R num_requests] [-S program] [-s subsystem | sftp_server]
          destination

How to Connect with SFTP

Make sure ssh is running as a background service in remote systems to set up the connection. After that, please take note of the remote system username and its host IP or domain name.

After getting a username and IP/Domain, use the below command in the local system to connect with the remote system.

$  sftp [email protected]
The authenticity of host '192.168.0.106 (192.168.0.106)' can't be established.
ED25519 key fingerprint is SHA256:N1/BpC453dvqVhXCJ0FCZIQfiXIdHkLd6oq361jcCPs.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])?

Don’t forget to replace the above username and host. Now press “yes” to allow save host information on the local system.

Using a different port other than default 22 while configuring a remote ssh server, use “-P” flags to specify the custom port, as shown below.

$ sftp -P 22 [email protected]

Now, if you set up an ssh key (recommended) on the remote system, it will not ask you the user password; otherwise, it will ask you the remote system user password to authenticate, as shown below.

Warning: Permanently added '192.168.0.106' (ED25519) to the list of known hosts.
[email protected]'s password:

Enter your user password (ex: trendoceans) for the remote system. Then it will authenticate and give “sftp>” terminal to communicate with the remote server, as shown below.

Connected to 192.168.0.106.
sftp>

Downloading Files from the Remote Server

Now that you are connected with the remote system, you can start downloading files from the server. To transfer files from remote to local, we will use the get command, which helps us download files from the remote system.

Let’s run the ls command to list all the files and directories on my remote server, as shown below.

sftp> ls
Desktop    Documents  Downloads  Music      Pictures   Public     Templates  Videos     to.txt

Right now, I am in my home directory on the server. You can use the PWD command to check.

Using the below command, let’s download “to.txt” from the remote system to my local system.

sftp> get to.txt
Fetching /home/trendoceans/to.txt to to.txt
to.txt                                                                                          100%   18    15.0KB/s   00:00

The get to.txt command will download the file in your local current working directory, which was before connecting with the server.

If you want to download files in a different location, then specify the path as shown below.

sftp> get to.txt Documents/
Fetching /home/trendoceans/to.txt to Documents/to.txt
to.txt                                                                                          100%   18    13.9KB/s   00:00

Instead of the single file, if you were downloading a directory containing many files. Then you can use the "-r" flag along with the get command, as shown below.

sftp> get -r Pictures/
Fetching /home/trendoceans/Pictures/ to Pictures
Retrieving /home/trendoceans/Pictures
Screenshot.png                                                                                  100%   89KB   9.9MB/s   00:00

Transferring Local Files to the Remote System

To upload any file from the local system to the remote system, you need to use the put command. The working of the put is very similar to get command with a minor difference.

In the put command, you need to specify the local file’s path and then upload it on the server, as shown below.

sftp> put file.txt
Uploading file.txt to /home/trendoceans/file.txt
file.txt                                                                                        100%    0     0.0KB/s   00:00
sftp> ls
Desktop    Documents  Downloads  Music      Pictures   Public     Templates  Videos     file.txt   image.jpg  to.txt

Above, you can see “file.txt” uploaded to the remote home directory. If you don’t specify any destination on the remote server, it will mostly upload the file in the home directory.

As shown below, to change the upload path, specify it after the local file path.

sftp> put file.txt Documents/
Uploading file.txt to /home/trendoceans/Documents/file.txt
file.txt                                                                                        100%    0     0.0KB/s   00:00
sftp> ls Documents/
Documents/file.txt

If you were uploading a folder, then use “-r” flags for recursive to upload directory from local to the remote server as shown below.

sftp> put -r dir
Uploading dir/ to /home/trendoceans/dir
Entering dir/
file.txt                                                                                        100%    0     0.0KB/s   00:00
sftp> ls
Desktop    Documents  Downloads  Music      Pictures   Public     Templates  Videos     dir        file.txt   image.jpg
to.txt

Wrap Up

I hope this changes your mind and on sftp, but if you still insist on using the old standard FTP protocol, we suggest using that only when you trust the network.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments