Set Up SFTP with Chroot on Ubuntu 24.04
Setting up SFTP with chroot on Ubuntu 24.04 creates a secure way to limit what files specific users can see and access.
SFTP, which stands for SSH File Transfer Protocol, is a safe way to move files using an SSH connection. Chroot is a command that locks a user into a specific directory, like putting them in a virtual sandbox.
When you set up SFTP chroot, you make sure users can only see their own home folder when they connect. This stops them from looking around in other parts of your server’s files.
This is useful if you want someone to only upload and download files for their project without seeing anything else on the computer.
Create a restricted group, add users to it, and then configure the SSH daemon’s `sshd_config` file to use `internal-sftp` and `ChrootDirectory` for that group. Restart the SSH service afterward.
Create restricted group
The SFTP chroot setup on Ubuntu 24.04 starts by creating a restricted group named ‘chgroup’. This ‘chgroup’ group restricts users to accessing only the files and folders assigned to them. The ‘chgroup’ group is created, and users are added using commands like `groupadd chgroup` and `usermod -g chgroup username`.
Run the command below to create a new group.
sudo groupadd chgroup
Next, run the command below and put a user in the chgroup created above.
sudo usermod -aG chgroup username
Replace username in the command above with the account’s actual username.
Configure SSH
SSH configuration in Ubuntu 24.04 enables SFTP chroot. The SSH server’s main configuration file, `/etc/ssh/sshd_config`, is edited to set up SFTP connections. These changes restrict users to their own directories, as the chroot setup intends.
sudo nano /etc/ssh/sshd_config
Adjust the highlighted settings in the file.
# override default of no subsystems
# comment out the line below
#Subsystem sftp /usr/lib/openssh/sftp-server
Subsystem sftp internal-sftp
# add the lines below
Match Group chgroup
X11Forwarding no
AllowTcpForwarding no
ChrootDirectory /home
ForceCommand internal-sftp
Save the file and then exit.
Restart SSH by running the command below.
Testing your SFTP chroot setup on Ubuntu 24.04 confirms that user access correctly restricts access. An SFTP connection is attempted to verify that users see only their assigned folders and cannot access other system parts. This testing step ensures your chroot configuration works as expected.
ssh richard@srv1.example.com's password:
This service allows sftp connections only.
Connection to srv1.example.com closed.
SFTP will succeed.
sftp richard@srv1.example.com's password:
Connected to srv1.example.com.
sftp>
The setup is complete!
Conclusion:
Setting up a chroot restricted SFTP account on Ubuntu 24.04 enhances security by isolating user access to specific directories. Here are the key takeaways:
- Enhanced Security: Users are confined to their directory, minimizing the risk of unauthorized access to other users’ files.
- Group Management: Creating a specific group for SFTP users simplifies the management of user permissions and access.
- SSH Configuration: Proper configuration in the SSH settings is crucial for implementing chroot restrictions effectively.
- Testing: Always test your configuration to ensure that SFTP works as intended without compromising security measures.
Creating a secure file transfer environment using SFTP with chroot is possible. This setup limits user access to specific directories, preventing them from seeing other files on your Ubuntu 24.04 server, which enhances security.
Was this guide helpful?
About the Author
Richard
Tech Writer, IT Professional
Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.
No comments yet — be the first to share your thoughts!