Create a Secure SFTP Server with chroot on Ubuntu 22.04
Set up an SFTP server on Ubuntu 22.04 that locks each user into their own directory with chroot.
Welcome to our comprehensive tutorial on setting up a highly secure SFTP server with chroot on Ubuntu 22.04. In this step-by-step guide, we’ll walk you through the entire process, ensuring that you can securely transfer files while restricting user access to their designated directories.
🔐 What You’ll Learn in This Tutorial 🔐
- Step-by-step installation of Ubuntu 22.04 for your SFTP server.
- Configure OpenSSH for secure SFTP connections.
- Implement chroot to confine users to their home directories.
- Fine-tune permissions and user access for maximum security.
By the end of this tutorial, you’ll have a robust SFTP server that ensures data integrity and confidentiality, making it ideal for personal use, small businesses, or even enterprise-level applications.
Create root directory for sftp users
You can change directory name to anything you like
mkdir /sftpuserschmod 701 /sftpusersCreate sftp user group
Change the group name to anything you like
groupadd sftpgroupCreate sftp user
Change mysftpuser to username you like
useradd -g sftpgroup -s /sbin/nologin mysftpuserpasswd mysftpuserCreate sftp user directory
mkdir /sftpusers/mysftpuserchown mysftpuser:sftpgroup /sftpusers/mysftpuserchmod 700 /sftpusers/mysftpuserEdit ssh config file
SSH config file is located at /etc/ssh/sshd_config, edit it with nano or any other text editor.
Uncomment the following line
Subsystem sftp /usr/lib/openssh/sftp-serverAdd the following text at the end of the ssh config file
Match Group sftpgroup ChrootDirectory /sftpusers/ ForceCommand internal-sftp -d /%u AllowAgentForwarding no AllowTcpForwarding no X11Forwarding no PermitTunnel no PasswordAuthentication yesSave and restart SSH
systemctl restart sshdSetup SFTP authentication with SSH key
Create directory for ssh keys
mkdir /etc/ssh/authorized_keyschown root:root /etc/ssh/authorized_keyschmod 755 /etc/ssh/authorized_keysecho 'PUBLIC_KEY' >> /etc/ssh/authorized_keys/usernamechmod 644 /etc/ssh/authorized_keys/usernameEdit ssh config file
Open /etc/ssh/sshd_config file with nano or any other text editor and change the following text from this
Match Group sftpgroup ChrootDirectory /sftpusers/ ForceCommand internal-sftp -d /%u AllowAgentForwarding no AllowTcpForwarding no X11Forwarding no PermitTunnel no PasswordAuthentication yesTo this
Match Group sftpgroup ChrootDirectory /sftpusers/ ForceCommand internal-sftp -d /%u AuthorizedKeysFile /etc/ssh/authorized_keys/%u .ssh/authorized_keys PermitRootLogin no PermitEmptyPasswords no AllowAgentForwarding no AllowTcpForwarding no X11Forwarding no PermitTunnel no PasswordAuthentication yesSave and restart SSH
systemctl restart sshdDisable SFTP password authentication
Edit file /etc/ssh/sshd_config and change PasswordAuthentication yes to PasswordAuthentication no