Easily Set up SFTP Server on Ubuntu 22.04

In this guide, we want to teach you to Set up SFTP Server on Ubuntu 22.04.

Secure File Transfer Protocol (SFTP) is a network protocol for securely accessing, transferring, and managing large files and sensitive data.

SFTP enables access, transfer, and management of files over a network. It’s used for secure file transfers over Transport Layer Security and the transfer of data for virtual private network (VPN) applications.

SFTP uses SSH to transfer files and requires that the client be authenticated by the server. Commands and data are encrypted to prevent passwords and other sensitive information from being exposed to the network in plain text.

Steps To Set up SFTP Server on Ubuntu 22.04

To Set up SFTP Server on Ubuntu 22.04, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Ubuntu 22.04.

Install SSH on Ubuntu 22.04

You should have SSH installed on your server to Set up SFTP Server on Ubuntu 22.04. First, update your local package index with the command below:

sudo apt update

Then, use the following command to install SSH:

sudo apt install ssh -y

Start and Enable SSH Service

When your installation is completed, use the commands below to start and enable the SSH service to start on boot:

# sudo systemctl start ssh
# sudo systemctl enable ssh

Verify your SSH service is active and running on Ubuntu 22.04:

sudo systemctl status ssh
Output
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: e>
     Active: active (running) since Thu 2023-03-02 10:23:53 UTC; 1min 7s ago
       Docs: man:sshd(8)
             man:sshd_config(5)
   Main PID: 2922 (sshd)
      Tasks: 1 (limit: 4575)
     Memory: 1.8M
        CPU: 129ms
     CGroup: /system.slice/ssh.service
....

Configure SFTP User Account on Ubuntu 22.04

At this point, you need to create a group for the SFTP to grant some mutual permissions to a group of users.

First, create a group named “sftp” by using the command below: You can choose your desired name.

sudo addgroup sftp
Output
Adding group `sftp' (GID 1000) ...
Done.

Then, create a user who will have the same privileges as the group. To do this, run the command below: You can choose your desired name.

sudo useradd orca

Verify that your user has been created by using the command below:

less /etc/passwd | grep orca
Output
orca:x:1000:1001::/home/orca:/bin/sh

Then, create a password for your user by using the following command:

sudo passwd orca
Output
New password:
Retype new password:
passwd: password updated successfully

Now you need to add your user to the SFTP group on Ubuntu 22.04:

sudo usermod -a -G sftp orca

At this point, verify the SFTP’s group details by using the command below:

grep sftp /etc/group
Output
sftp:x:1000:orca

As you can see from the output, user orca is added successfully to the SFTP group.

Configure a Transfer File for SFTP on Ubuntu 22.04

At this point, you need to have a directory that the users can access instead of accessing the entire machine. 

To create the directory under /var/sftp/ run the command below:

sudo mkdir -p /var/sftp/Document

Set the ownership of the above directory to the root user by using the following command:

sudo chown root:root /var/sftp

Also, set the correct permissions for it:

sudo chmod 755 /var/sftp

At this point, you need to allow access to the “Documents” directory to the SFTP user (orca):

sudo chown orca:orca /var/sftp/Document

Now you need to edit the SSH configuration file. Open the file with your favorite text editor, here we use vi:

sudo vi /etc/ssh/sshd_config

Find the Subsystem sftp /usr/lib/openssh/sftp-server line and the following content under it:

Subsystem sftp  /usr/lib/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       PermitTTY no
#       ForceCommand cvs server
Match User orca
ChrootDirectory /var/sftp
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
...

When you are done, save and close the file.

Restart SSH, to apply the changes:

sudo systemctl restart ssh

Login to SFTP Server on Ubuntu 22.04

First, connect to the user orca using the SSH service only for the testing purpose:

ssh orca@localhost
Output
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'localhost' (ED25519) to the list of known hosts.
orca@localhost's password:
This service allows sftp connections only.
Connection to localhost closed.

To test from the same system as the one you just configured SFTP on, connecting to the loopback address 127.0.0.1 will work just fine.

sftp orca@127.0.0.1
Output
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '127.0.0.1' (ED25519) to the list of known hosts.
orca@127.0.0.1's password:
Connected to 127.0.0.1.
sftp> 

At this point, list down the directories of SFTP by using the command below:

sftp> ls
Document

To exit from the SFTP Server, just run the exit command.

sftp> exit

Uninstall SFTP From Ubuntu 22.04

At this point, if you want to remove the SFTP from your server, you can easily delete the SSH with all its associated files:

sudo apt purge ssh -y

This command will remove SFTP and all of its data.

Conclusion

At this point, you have learned to Set up SFTP Server on Ubuntu 22.04.

Hope you enjoy it. Also, you may be interested in these articles on the Orcacore website:

Install and Configure Odoo 16 on Ubuntu 22.04

Install Nginx with Brotli Compression on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!