How to install and setup SFTP server on Ubuntu 22.04

SFTP (Secure File Transfer Protocol) is a file transfer protocol that provides secure access to a remote computer. Think of it as a tunnel provider, whenever you want to connect remotely, you use SFTP protocol to ensure your connection is secure from eavesdropping, we use Secure Shell (SSH) for that.

FTP protocol is a client/server protocol that transfers files over the internet. FTP clients are used to sending and retrieving files to and from servers storing files and also responding to the clients’ needs.

To get started with the installation, make sure ssh is installed, then set up sftp user group, configure ssh service, and lastly connect via sftp service.

1. Update system repositories.

To start off, update your system repositories in order to make them up to date.

sudo apt update && apt upgrade -y

2. Install ssh server

SFTP depends on ssh protocol for communication, we have to install it if not already installed on your system.

sudo apt install ssh 

The following will be the sample output.

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  ssh
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 4834 B of archives.
After this operation, 133 kB of additional disk space will be used.
Get:1 http://mirrors.linode.com/ubuntu jammy/main amd64 ssh all 1:8.9p1-3 [4834 B]
Fetched 4834 B in 0s (199 kB/s)
Selecting previously unselected package ssh.
(Reading database ... 108902 files and directories currently installed.)
Preparing to unpack .../ssh_1%3a8.9p1-3_all.deb ...
Unpacking ssh (1:8.9p1-3) ...
Setting up ssh (1:8.9p1-3) ...
Scanning processes...                                                           
Scanning candidates...                                                          
Scanning linux images...                                                        

Restarting services...
Service restarts being deferred:
 /etc/needrestart/restart.d/dbus.service
 systemctl restart networkd-dispatcher.service
 systemctl restart systemd-logind.service
 systemctl restart unattended-upgrades.service
 systemctl restart user@0.service

If you want to install FTP over OpenSSH, you need to edit the sshd configuration file.

sudo vim /etc/ssh/sshd_config

You will need to add this below the file.

Match group sftp
ChrootDirectory /home
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp

Restart the ssh server for the changes to take effect.

sudo systemctl restart ssh

3. Create SFTP user account.

Set up STTP user and group which will log in here.

To set the group use the following command.

$ sudo addgroup sftpgroup
Adding group `sftpgroup' (GID 1000) ...
Done.

Then we need to create sftpuser and you need to assign it to sftp group

sudo useradd -m sftpuser -g sftpgroup

Set the password for sftpuser.

$ sudo passwd sftpuser
New password: 
Retype new password: 
passwd: password updated successfully

Lastly, grant the new user access home directory.

sudo chmod 700 /home/sftpuser/

4. Connect to sftp

To connect to sftp we use the command

$ sftp sftpuser@localhost

For instance, to login to the sftp I have created, I need to type sftp sftpuser@127.0.0.1

$ sftp sftpuser@127.0.0.1
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ED25519 key fingerprint is 
This key is not known by any other names
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.
sftpuser@127.0.0.1's password: 
Connected to 127.0.0.1.
sftp> 
sftp> 

5. Useful SFTP commands

Whenever you need help with any command using the following command.

sftp> help

If you want to navigate through your SFTP, use pwd

sftp> pwd
Remote working directory: /home/sftpuser

To download files with sftp command.

sftp> get filename.zip

Conclusion

We have successfully installed sftp on Ubuntu 22.04, happy learning.

Leave a Reply

Your email address will not be published.