The “sftp” Command in Linux [6 Practical Examples]

The sftp command in Linux is similar to the ftp command. However, it transfers files from one computer to another computer more securely. The data is end-to-end encrypted. So even if someone gets a hold of the data, he won’t be able to access it. The sftp command in Linux uses the SSH (Secure Shell) protocol. In this article, I will show some primary usage of the sftp command with some necessary concepts.

A. Description

The sftp command in Linux is a part of the SSH protocol suite, which was first developed in the 1990s. In a very short time. It took over the ftp command as it is not a secure way to transfer data. Over time, the sftp command has become more advanced. It is also easy to use. Someone with basic networking knowledge can set up a server and transfer files.

B. Syntax

The syntax of the sftp command in Linux goes like the following:

sftp [OPTION]... username@ip_address
Note: In the above syntax OPTION enclosed by a square bracket, followed by three dots, represents that you can provide options simultaneously. The username donates the username of the SFTP server, and ip_address denotes the IP address of that server.

C. Options

The sftp command in Linux provides many options to satisfy your needs. You can check them by yourself by the following command:

man sftp

I have listed some of the useful options for in down below:

Useful Options

  • -C, compresses.
  • -l, limits bandwidth.
  • -P, specifies a port.
  • -p, preserves original files attributes.
  • -r, is used to copy directories.
  • -4, uses IPv4.
  • -6, uses IPv6.

How to Setup an SFTP server

Previously, I have mentioned that the sftp command in Linux transfers files between two computers- a client and a server. In this article, I will use Ubuntu installed in WSL (Windows Subsystem for Linux) as the client computer. I also used Windows Terminal to customize the output. Secondly, I installed Ubuntu on VMware Workstation Pro and I will use it as the SFTP server. Now follow the steps below to set up a server:

❶ Open an Ubuntu Terminal on the server computer.

❷ (Optional) Copy the following command to update your system and hit ENTER:

sudo apt-get update
Note: Provide a password if necessary.

Your output will be like the one below. The command will update your local package index, which determines what package is available and manages dependencies.Updating the system❸ Now write the command below to install the “openssh-server” package and press ENTER:

sudo apt-get install openssh-server

The installation will be completed in a minute.Installing SFTP server for the sftp command in Linux❹ Now insert the following commands one-by-one and press ENTER:

sudo service ssh start
sudo service ssh status

If you see “active (running)” message in blue, your server is ready.Starting the ssh service and showing its status❺ To know the IP address of your server, copy the following command and hit ENTER:

ifconfig
Note: If your system doesn’t have “ifconfig”, use the following command to install it:

sudo apt-get install net-tools

The IP address of my server is 192.168.235.129, and I will use it in this article. Don’t worry if you get a different IP address.The IP address for the sftp command in Linux

Practical Examples of the “sftp” Command in Linux

In this section, I will show you how to use the sftp command in Linux. Try to understand every concept and follow every step carefully. For your information, I have used both the client and server in these examples. Remember, the client goes like “walid@client” and the server goes like “walid@Ubuntu”.

Example 1: Connect to an SFTP Server Using the “sftp” command in Linux

At first, I will connect to the SFTP server using the sftp command in Linux. To connect to an SFTP server, you need your username, password and the IP address of the server as well. In my case, my username is walid and my IP address is 192.168.235.129. Now do the following to connect to an SFTP server:

Steps to Follow >

➊ At first, open an Ubuntu Terminal in your client computer.

➋ Copy the following command in the Terminal and press ENTER:

sftp [email protected]

➌ Provide the password associated with your username and Hit ENTER again.

Output >

In the output, it is showing “Connected to 192.168.235.129” and “walid@client” is replaced by “sftp>” which denotes that it is ready to take any sftp commands and execute it.Connecting to the server using the sftp command in Linux


Similar Readings


Example 2: Upload a File Using the “sftp” Command in Linux

Let’s upload a file to the server using the sftp command in Linux. I will the “get” command for that. Firstly, I used the following command to view files in the client computer:

ls

You can see a file by the name of local_file in the home directory of my client computer. Now I will upload it to the server.Showing local_fileTo upload a file to the server, follow the steps below:

Steps to Follow >

➊ Firstly, launch an Ubuntu Terminal in the client computer.

➋ Write the following command to connect to the server and hit ENTER:

sftp [email protected]

➌ Give your password and press ENTER again.

❹ Copy the command below to upload a file:

put local_file

❺ Finally, find the ENTER key in your keyboard and hit it.

Output >

You will see “Uploading” followed by the name of the file in your terminal. You will also see the directory where the file will be uploaded. Moreover, the output of the sftp command contains the following five things respectively – filename, percentage, bytes Transferred, speed and estimated time.Uploading a file using the sftp command in LinuxAdditionally, I executed the following command to see if the file is in server computer or not:

ls

See, there is file named local_file in the server. I have successfully uploaded the file to the server.Displaying the local_file in the server

Example 3: Download a File from the SFTP server

Now I will download a file from the server using the sftp command in Linux. I need to use the get command for this purpose. At first, I executed the following command to view the available files in my server:

ls

There is a file by the name of remote_file in the home directory of the server. Now download it to the home directory of the client computer.Showing the remote_file in the serverTo download a file from the server, do the following:

Steps to Follow >

➊ Go to your client computer and open an Ubuntu Terminal.

➋ Insert the following command in the Terminal to connect to the server and hit ENTER:

sftp [email protected]

➌ Provide the password and press the ENTER button again.

❹ Now type the command below to download a file from the server:

get remote_file

❺ Press the ENTER button from your keyboard.

❻ Copy the command below to quit the session and hit ENTER:

exit

Output >

In the output, if you see “Fetching”, your file is downloading. You will also see the path of the remote_file in server. Moreover, I executed the following command in the terminal to check whether the file was downloaded or not:

ls

As you can see, there is a file named remote_file. So I have successfully downloaded the file using the sftp command in Linux.Downloading the remote_file from the server using the sftp command in Linux

Example 4: Copy a Directory Using the “sftp” Command in Linux

In the last two examples, you have seen how to copy files. What if you want to copy a whole directory? You can do that as well using the sftp command in Linux. In this example, I will show you how to copy a directory from the server to the client computer. Firstly, I executed the following command in the server to see my directory:

ls

There is a directory by the name of mydirectory in my server. Now let’s copy it to the client computer.Printing the mydirectory in the serverTo copy a directory from the server, follow the steps below:

Steps to Follow >

➊ At the beginning, open an Ubuntu Terminal on your client computer.

➋ Write the following command to connect to the server and Hit ENTER:

sftp [email protected]

➌ Provide the password associated with the username and press ENTER again.

❹ Type the following command to copy a directory from the server:

get -r mydirectory

❺ Find the ENTER button on the keyboard and press it.

❻ Copy the command below to quit the session and hit ENTER:

exit

Output >

In the output, you will see “Fetching” and “Retrieving” which denote that the sftp command in Linux is copying the mydirectory to the client computer. Then I used the following command in the client computer to list out all the directories:

ls

Sure enough, there is a directory named mydirectory in the client computer.Copy a directory using the sftp command in Linux


Similar Readings


Example 5: Make and Change into a Directory

Unlike the scp command, you can run commands in the SFTP command prompt of the sftp command in Linux. Now, I will make a directory in the home directory of the server and change into that directory by running commands in the SFTP command prompt. To make a directory and go into that directory, do the following:

Steps to Follow >

➊ Launch an Ubuntu Terminal in the client computer firstly.

➋ Insert the command below to connect to the SFTP server and hit ENTER:

sftp [email protected]

➌ Give your password and press the ENTER button.

❹ Now, write the following command and press ENTER to make a directory:

mkdir mydirectory

❺ To go into the previously created directory, copy the command below:

cd mydirectory

❻ Hit the ENTER key on your keyboard.

Output >

Sadly, you won’t see any notifications about whether your directory has been created. I will show you how to list out all the contents from the SFTP command prompt in the next example. Then I went into the newly created mydirectory and executed the following command to the current directory:

pwd

As you can see, the path is “/home/walid/mydirectory” which denotes that the directory has been successfully created in the home directory of the server computer.Making and changing into that directory using the sftp command in LinuxAlso, I used the following command in the server:

ls

As expected, there is a mydirectory in the server.Displaying the mydirectory in the server

Example 6: List the Contents of a Directory Using the “sftp” Command in Linux

Finally, I will list the contents of a directory from the command prompt of the sftp command in Linux. In the beginning, I executed the following command in the server computer:

ls

As you can see, there are three files (myfile1, myfile2, myfile3) in the home directory of the server. So, I would expect to see the same output.Printing all the files in the mydirectoryTo list the contents of a directory, follow the steps below:

Steps to Follow >

➊ Open an Ubuntu Terminal in the client computer first.

➋ Copy the following command to connect to the SFTP server and hit ENTER:

sftp [email protected]

➌ Provide your password and press ENTER.

❹ Write the following command to view all the files of a directory:

ls mydirectory

OR,

dir mydirectory

❺ Press the ENTER button once again.

Output >

Here I executed both the “ls” and “dir” commands and both of them serve the same purpose. And in both cases, I got the same output showing the myfile1, myfile2, and myfile3 files.Listing all the contents in the mydirectory using the sftp command in Linux

Conclusion

If you are interested in networking, then the sftp command in Linux is tool to wrap your head around. It is a convenient tool to transfer files between two computers securely. I hope the examples in this article have helped you to build a strong foundation in the sftp command in Linux.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Walid Al Asad

Hello Everyone! I am Walid Al Asad. Currently, I am working at a tech company named Softeko as a Linux Content Developer Executive. I live in Dhaka, Bangladesh. I have completed my BSc. in Mechanical Engineering from Bangladesh University of Engineering and Technology (BUET). You can find me on LinkedIn, and ResearchGate. Read Full Bio

Leave a Comment