How to Setup SFTP Server on Ubuntu – 9 Steps

How to Setup SFTP Server on Ubuntu

Ubuntu is a Linux operating system with a secure and reliable for setting up an SFTP server. This Linux distribution has integrated tools and packages that you can use for SFTP. In this blogpost guide, we will explain How to Setup SFTP Server on Ubuntu to send data securely. This guide will take you through creating Ubuntu SFTP server on your Ubuntu 22.04 system. Whether you want to secure a way to share files by establishing a remote system, this step-by-step tutorial will empower you to achieve your goals.

What is SFTP?

SFTP stands for Secure File Transfer Protocol. SFTP is a secure and encrypted file transfer protocol designed to provide the safest way to share data between a client and a server. SFTP protocol is built upon the SSH (Secure Shell) protocol, making it the best choice for securely sharing files over the internet or within a local network.

Features of SFTP

Here we are listing features of SFTP protocol:

  • Encryption: SFTP protocol encrypts the data being transferred and the authentication process, ensures that your files are safe.
  • Authentication: SFTP uses SSH keys or passwords for user authentication that make your SFTP server more secure and easy to access.
  • Integrity: Files sharing via SFTP are checked for integrity to make sure that they have not been copied during the transfer process.
  • Portability: SFTP is supported on various OS, which makes it a versatile choice for cross-platform file transfers.
  • Remote Access: SFTP allows you to access and manage your SFTP server files remotely, which is particularly useful for remote workers.

How to Setup SFTP Server on Ubuntu

Setting up an SFTP server on Ubuntu 20.04 is very easy and affordable here we are listing the requirements for How to Setup SFTP Server on Ubuntu.

Ubuntu SFTP Server Setup Requirements

Requirements for setting up SFTP server on Ubuntu. To install an SFTP server on the current Ubuntu version 22.04 your system should meet the following minimum requirements:

  • Memory (RAM): 4GB
  • Processor (CPU): 2GHz (Dual Core)
  • HDD: Depends on Data size
  • OS: Ubuntu, Users with Root rights
  • Software Package: OpenSSH
  • Internet Connection: This is for downloading packages and connecting to the SFTP server.

Step to Installing an Ubuntu SFTP Server

To set up an FTP server that supports SFTP you should first check whether OpenSSH is installed. Usually, OpenSSH is included as standard on Ubuntu. If this is not in your system, you can download the package from OpenSSH’s official repository.

Open the terminal on your system and follow the commands listed here:

Step 1: Check the OpenSSH Package

Enter the following to view all installed packages on your system and filter for SSH:

$ dpkg -l | grep ssh
Bash

Step 2: Install SSH

You can install or update the OpenSSH server using the following command:

$ sudo apt install ssh
Bash

Step 3: Modify the SSHD Configuration for the SFTP Group

After installing the SSH server, your next step is to modify the SSHD configuration. SSHD configuration file located at /etc/ssh/sshd_config. You can open it using the Nano editor, For example:

$ sudo nano /etc/ssh/sshd_config
Bash

Now paste the following bash line at the end of the file:

Match Group sftpgroup
ChrootDirectory %h
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
How to Setup SFTP Server on Ubuntu

sftp server setup on ubuntu

Save the file and exit the text editor.

These few lines of code will allow the SFTP group users to access your home directory via SFTP.

Step 4: Restart SSH Services

Once you have made the changes to the sshd_config file you need to restart the SSH services:

$ sudo systemctl restart sshd
Bash

Step 5: Create a SFTP Users and Groups

After restarting the SSH service, the next step is to create a new group sftpgroup, and a new user sftpuser, this user and group can only access the Ubuntu SFTP server and not the SSH service. All the SFTP users will be part of this common group.

$ sudo groupadd sftpgroup
Bash

Now the new user is added to the SFTP group with the -G, -d sets the home directory and -s sets the shell access rules.

$ sudo useradd -G sftpgroup -d /srv/sftpuser -s /sbin/nologin sftpuser
Bash

This will create a new user and it will be added to the above-created group by using this command you also have to set the password for the newly created SFTP user by typing the following command:

$ passwd sftpuser
Bash

Step 6: Set up Chroot

Chroot directory helps you create a sandbox for currently running processes. Firstly you need to make a new folder:

$ mkdir -p /srv/sftpuser
Bash

Give the ownership of this folder to the root user using chown:

$ sudo chown root /srv/sftpuser
Bash

Add read and execute group rights to the user:

$ sudo chmod g+rx /srv/sftpuser
Bash

Add Subdirectory and set certain sftpuser as owner:

$ mkdir -p /srv/sftpuser/data
$ chown sftpuser:sftpuser /srv/sftpuser/data
Bash

We create a subdirectory “data” here SFTP users can upload files, but they will only have limited rights in the sftpuser directory. SFTP users just have reading rights but for security concerns, they don’t have writing rights.

Step 8: Connect to the Ubuntu SFTP Server

You can make a connection to the SFTP server via the SFTP command or through an FTP client with GUI. For connection with the Ubuntu SFTP server enter the command sftp, followed by the user and hostname or it can be IP address of the SFTP server.

$ sftp sftpuser@SERVER-IP
Bash

for a user-defined port, you have to specify it

$ sftp -P PORT ftpuser@SERVER-IP
Bash

After entering this it will be asked to enter the password of the SFTP user.

Step 9: Upload Files to the SFTP Server

For uploading files to the SFTP server we have to use the put command with folder path.

put /path/to/file/on/local /
Bash

It can generate errors because the SFTP user doesn’t have writing rights in this chroot directory.

Try with the data folder:

put /path/to/file1/on/local1 /data/
Bash

You can view the files on the Ubuntu SFTP server with the command ls:

ls /data/
Bash

Using this command you can see which files are on the SFTP server. That’s it

Related Post:

>> Encoding and Decoding Using Base64 Strings in Python

>> Odoo.sh VS Odoo Online VS Odoo On-Premises

Conclusion: How to Setup SFTP Server on Ubuntu

In this guide, we have discussed about essential steps on how to setup SFTP server on Ubuntu, focusing on securing your file transfer through SSH. Setting up an SFTP server ensures that your data is transmitted in a safe and encrypted manner, providing a reliable way to share files remotely or within a local system network.

If you have any queries about this article feel free to ask in the comment section we are there for you.

Happy Security!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.