Ubuntu

How to Setup SFTP Server in Ubuntu

SFTP is the acronym of “Secure File Transfer Protocol” which is used to transfer files from one place to another place using the internet and it works similarly to FTP (File Text Protocol), but the difference in both of them is that SFTP also ensures the privacy of the file using SSH service, which secures the data of the file by encrypting it, so no one can access the data.

Though it has the advantage that it encrypts the data and transfers it to the destination location more securely, it has a disadvantage as well; on reaching the destination location, any user can access it. So to make it more secure, we should restrict the other users to access it, other than the allowed users.

In this article, we will discuss how to install SFTP in Ubuntu, if it is not already installed, and how to set it up by using the simple commands of Ubuntu.

How to install SFTP server in Ubuntu

SFTP server is usually installed in Ubuntu by default, but if it is not already installed, one can install SSH by using the simple commands of Ubuntu. Because SFTP uses the SSH server, so to check the SFTP server is already installed or not we will run the below-mentioned command:

$ sudo systemctl status ssh

The output is shows, SSH is not installed in our Ubuntu, to install it, run the following command of Ubuntu:

$ sudo apt install ssh -y

Once it is installed, we will enable it using the systemctl command:

$ sudo systemctl enable ssh

Now, again using the systemctl command, start it:

$ sudo systemctl start ssh

To verify the installation of ssh checks its status using the systemctl command:

$ sudo systemctl status ssh

The running status of SSH shows it has been installed successfully.

How to create an SFTP user account

We can either create users directly or by creating a group of SFTP and then create users to give them access to the SFTP. To create a group, for example, we name it “sftp”, you can name it according to your choice, execute the following in a terminal:

$ sudo addgroup sftp

To create a user, “John”, you can rename “John” with your own username, run the following command:

$ sudo useradd John

For the verification of newly user-created, execute:

$ less /etc/passwd | grep John

To add a password for this new user, run:

$ sudo passwd John

To move user, “John” to the group “sftp” run the command:

$ sudo usermod -a -G sftp John

To verify that the user, John, has been added to the group,sftp, run the statement:

$ grep sftp /etc/group

How to create a directory for file transfers

To understand how to restrict access to a directory for other users, we will create a directory “Document” in path /var/sftp which will be owned by root. Then only the “John” user which we have created can access this directory and upload files in it.

To do so, first, we will create a directory of “Document” using the mkdir command:

$ sudo mkdir -p /var/sftp/Document

Allow the root, to recognize the owner of /var/sftp:

$ sudo chown root:root /var/sftp

Grant write permissions of this directory to the same directory, and allow other users to read-only:

$ sudo chmod 755 /var/sftp

Now grant the permissions of the Document to the user John:

$ sudo chown John:John /var/sftp/Document

How to configure SSH daemon

Once the installation is completed, we will open the configuration file of the SSH server with any editor and configure it. In this configuration, we are restricting the user, John, to the directory /var/sftp, and also none of the other users can access this directory. We are using a nano text editor to configure it so we will run the following command:

$ sudo nano /etc/ssh/sshd_config

Once the file opened, type the following text in it:

Match User John

ChrootDirectory /var/sftp

X11Forwarding no

AllowTcpForwarding no

ForceCommand internal-sftp

When the file is opened, at the bottom you will see the text “Subsystem sftp /usr/lib/openssh/sftp-server”, write those lines after this line. The explanation of these five lines are:

  • Match User tells the SSH to implement these changes on the mentioned user, in our case, it is “John”
  • ChrootDirectory ensures the user will not have access to any other directory except that whose part is being provided, in our case it is “/var/sftp”
  • AllowTcpForwarding and X11Forwarding will enable or disable the port tunneling and  X11forwarding, in our case, both are being disabled by typing “no”
  • ForceCommand ensure the SSH run the SFTP server only after the login

Once the changes are made, save the changes by pressing CTRL+S, and then terminate the editor by pressing CTRL+X.

To apply these new changes, restart the SSH server using:

$ sudo systemctl restart ssh

How to verify the restriction configurations

To verify the restriction configurations, we will try to access the files using the normal SSH command:

$ ssh John@localhost

The output is showing that John cannot be accessed by any other SSH.

How to login SFTP server using command line

We can log in to the user protected by the SFTP server in the command line by the use of loop address which is by default 127.0.0.1, once the command has been executed it will ask for the password set for the user:

$ sftp John@127.0.0.1

It has been connected to the SFTP server. List down the directories of this user using the ls command:

$ ls

The output showing the directory, Document which was restricted for this user. Now, this user cannot access any other directory other than this one.

How to login SFTP server using GUI

We can also access SFTP using the GUI mode, simply go to the home folder, choose other locations, type sftp://[email protected] and click on the connect.

It will ask the password of the user, type the password, and click on Unlock:

Finally, It will open the directory:

How to Remove SFTP server

To remove or uninstall the SFTP server, just remove the ssh by using the command:

$ sudo apt purge ssh -y

Conclusion

SFTP server encrypts the file and transfers it to the destinations, and decodes the file there to communicate the data safely. Safety is necessary for transferring files as sometimes files contain sensitive pieces of information which should only be delivered to the authorized person, this can be done with the help of SFTP.  In this article, we have learned how to install SFTP if it is not installed by default in Ubuntu, then we learned how to create a user of SFTP directly or with the help of the group. We also learned the feature of restricting the files to only selected users as well as restricting the user to that particular file so that the user cannot move to any other directory. Finally, we learned how to remove the ssh server from Ubuntu 20.04.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.