In this guide, we will explain how to setup SFTP server on Ubuntu 24.04 LTS system step-by-step.
Secure File Transfer Protocol (SFTP) is a secure and reliable method for transferring files between systems over a network. Setting up an SFTP server on Linux allows you to securely transfer files, manage permissions, and control access.
Background of SFTP & chroot :
SFTP stands for SSH File Transfer protocol or Secure File Transfer Protocol. SFTP provides file access, file transfer, and file management functionalities over any reliable data stream. When we configure SFTP in chroot environment , then only allowed users will be limited to their home directory , or we can say allowed users will be in jail like environment where they can’t even change their directory.
Prerequisites
- Pre Install Ubuntu 24.04 Instance
- Local with sudo rights
- Basic knowledge of Linux command-line operations.
- Internet connectivity or locally configured repository.
How to Setup SFTP Server on Ubuntu 24.04
Without any delay, let’s jump into SFTP Server installation and configurations steps. Refer the following steps one after the another.
1) Install OpenSSH Server
SFTP is a part of the OpenSSH package,so install openssh server package on your Ubuntu system. If it is already installed then you skip this step.
Open the terminal, run the following commands:
$ sudo apt update $ sudo apt install openssh-server -y
Once openssh server installed, start and enable it’s service using following systemctl commands.
$ sudo systemctl start sshd $ sudo systemctl enable sshd
2) Create SFTP Group and User
To manage access to the SFTP server, it is a good practice to create a dedicated SFTP group and user.
Create a sftp_users group using groupadd command:
$ sudo groupadd sftp_users
Add a new user and assign it to the sftp_users group. This user will only have SFTP access:
$ sudo useradd -m -G sftp_users -s /usr/sbin/nologin sftpuser
In above command, ‘-s /usr/sbin/nologin‘ option ensures that the user cannot log in via SSH and can only use SFTP.
Next, set a password for the new SFTP user, run passwd command
$ sudo passwd sftpuserNew password: Retype new password: passwd: password updated successfully $
3) Configure SSH for SFTP-Only Access
Now, we need to configure SSH to restrict the SFTP users to SFTP-only access. Edit sshd_config file using your favorite editor, add the following lines at the end of the file to configure SFTP-only access for users in the sftp_users group:
# vi /etc/ssh/sshd_config #comment out the below line and add a line like below #Subsystem sftp /usr/libexec/openssh/sftp-server Subsystem sftp internal-sftp # add Below lines at the end of file Match Group sftp_users ChrootDirectory %h ForceCommand internal-sftp AllowTcpForwarding no X11Forwarding no
Where :
Match Group sftp_users – This indicates that the following lines will be matched only for users who belong to group sftp_users
ChrootDirectory %h – This is the path(default user’s home directory) that will be used for chroot after the user is authenticated. So, for sftpuser, this will be /home/sftpuser
ForceCommand internal-sftp – This forces the execution of the internal-sftp and ignores any command that are mentioned in the ~/.ssh/rc file.
After making the above changes, restart the ssh service using following command,
$ sudo systemctl restart sshd // For RHEL based Distributions $ sudo systemctl restart ssh // For Debian based Distributions
4) Set the required permissions
Adjust the permissions of the user’s home directory using chown and chmod commands.
$ sudo chown root:root /home/sftpuser $ sudo chmod 755 /home/sftpuser
Create a directory where the sftpuser can upload files,
$ sudo mkdir /home/sftpuser/uploads $ sudo chown sftpuser:sftp_users /home/sftpuser/uploads
5)Â Test Sftp server
First try to access the system using ssh.
$ ssh sftpuser@your_server_ip
As we can see in output above sftpuser is not allowed to ssh.
Now let’s try to login using sftp,
As you can see above, sftpuser is logged in via sftp and but can’t change the directory because of chroot environment.
Now do the uploading and downloading testing as shown below:
As we can see above , both uploading & downloading working fine for sftpuser user. That’s all from this guide, We hope you are able to setup sftp server on Ubuntu 24.04 using above steps.You are most welcome to share your feedback and queries in the comments sections below.Â
Also Read: How to Install AWS CLI on Linux Step-by-Step