How to Configure an SFTP Chroot Jail on CentOS Stream 9: Restrict Directory Access Safely Without Shell Access

CentOS tutorial - IT technology blog
CentOS tutorial - IT technology blog

Last week, our team received a ticket from a partner requesting an account to upload nightly .csv report files to our server. Granting a regular SSH user is too risky—they could browse the filesystem or run arbitrary commands. Setting up traditional FTP, on the other hand, leaves security gaps and requires opening an entire range of passive ports, which is a hassle. The cleanest solution is to configure an SFTP Chroot Jail on CentOS Stream 9: lock the user into a single designated directory and disable shell access completely.

At our company, a few legacy servers still run CentOS 7, and migrating them to AlmaLinux was a project we wrapped up recently. For newer machines spun up for staging environments or partner file transfers, we use CentOS Stream 9 across the board. SFTP on version 9 still runs over OpenSSH on the familiar port 22, but there are a few differences with drop-in config files and SELinux that you need to watch out for to avoid wasting time debugging.

Quick Start: Configure an SFTP Chroot Jail in 5 Minutes

Need to spin up an SFTP account quickly for a partner to test right away? It only takes about 5 minutes with the 5 steps below on CentOS Stream 9 (using root or sudo):

# 1. Create a dedicated group for SFTP-only users
sudo groupadd sftpusers

# 2. Create a user without shell access (-s /sbin/nologin) and without a default home directory (-M)
sudo useradd -g sftpusers -s /sbin/nologin -M partner01
sudo passwd partner01

# 3. Create the chroot directory structure and set proper permissions
sudo mkdir -p /var/sftp/partner01/upload
sudo chown root:root /var/sftp /var/sftp/partner01
sudo chmod 755 /var/sftp /var/sftp/partner01
sudo chown partner01:sftpusers /var/sftp/partner01/upload
sudo chmod 750 /var/sftp/partner01/upload

Once the user and directories are ready, create a separate configuration file in OpenSSH’s drop-in directory so you don’t touch the main config file:

sudo tee /etc/ssh/sshd_config.d/99-sftp-chroot.conf > /dev/null <<'EOF'
Match Group sftpusers
    ChrootDirectory /var/sftp/%u
    ForceCommand internal-sftp
    PermitTunnel no
    AllowAgentForwarding no
    AllowTcpForwarding no
    X11Forwarding no
EOF

Test the configuration syntax and reload the SSH service:

sudo sshd -t && sudo systemctl reload sshd

Test connecting from another machine to verify immediately:

# Try regular SSH -> should be rejected
ssh [email protected]
# Output: This service allows sftp connections only.

# Try SFTP -> logs in successfully and stays locked inside the virtual root directory
sftp [email protected]

Once you reach the sftp> prompt, running pwd will show the path as /. Trying to create files or upload directly here will be blocked; you can only write files inside /upload.

Detailed Breakdown of Each Component

Getting it working is one thing, but understanding how it works under the hood helps you troubleshoot when things go wrong. Let’s break down each part of the configuration above.

1. ChrootDirectory Ownership Rules

This is where 90% of newcomers hit the Connection closed by remote host error right after entering the password. OpenSSH is very strict: the entire directory tree from / down to the path specified in ChrootDirectory must be owned by root:root. The maximum allowed permission is 755—never grant write permissions to group or others.

That is why we split it into two clear layers:

  • /var/sftp/partner01: owned by root:root with 755 permissions — acts as the “jail”. From here, the user can view the directory but cannot write files directly to the virtual root.
  • /var/sftp/partner01/upload: owned by partner01:sftpusers with 750 permissions — the only location where the user is allowed to upload, modify, or delete files like report_20261201.csv.

2. Why Use ForceCommand internal-sftp?

In the past, setting up a chroot for the traditional sftp-server required copying a bunch of libraries from /lib64 and /bin into the jail directory, which was clunky. Using internal-sftp is much lighter. The SFTP process is built directly into the sshd daemon, so it doesn’t need any system dependencies inside the chroot.

Placing the configuration in /etc/ssh/sshd_config.d/99-sftp-chroot.conf takes advantage of CentOS Stream 9’s default Include mechanism. This approach is much cleaner than editing /etc/ssh/sshd_config directly. The Match Group block is also completely isolated and won’t affect admin accounts.

3. Completely Disabling Shell Access with /sbin/nologin

The -s /sbin/nologin flag acts as a second layer of security. Even if someone somehow bypassed ForceCommand, they still couldn’t open an interactive shell like bash. Combining this with disabled forwarding (AllowTcpForwarding no, PermitTunnel no, X11Forwarding no) prevents partners from turning the SFTP account into a pivot tunnel into your internal network.

Advanced: SSH Keys, Logging, and SELinux on CentOS Stream 9

Relying solely on passwords in production is a bit thin. Our team usually enables the 3 additional configuration layers below to improve both security and auditability.

Using SSH Keys While Keeping the Chroot Clean

Placing keys in ~/.ssh/authorized_keys inside the chroot directory easily leads to permission errors. Worse, a user could even modify the key file if you accidentally set loose permissions. The cleanest trick is to store all public keys in a separate directory owned by root:

sudo mkdir -p /etc/ssh/sftp_keys
sudo chmod 755 /etc/ssh/sftp_keys
# Copy the partner's public key into a file matching the username
sudo tee /etc/ssh/sftp_keys/partner01 < partner01.pub
sudo chmod 644 /etc/ssh/sftp_keys/partner01

Then add these 2 lines to the Match Group sftpusers block in 99-sftp-chroot.conf:

    AuthorizedKeysFile /etc/ssh/sftp_keys/%u
    PasswordAuthentication no

Enable Detailed Logging for All Upload/Download Activity

By default, SFTP only logs logins without recording which files a user uploaded or deleted. To make auditing possible if files go missing, update the ForceCommand line to:

    ForceCommand internal-sftp -l INFO -f LOCAL6

Next, configure rsyslog to route SFTP logs to /var/log/sftp.log (note: install the rsyslog package first if you’re using CentOS Stream 9 minimal):

echo "local6.* /var/log/sftp.log" | sudo tee /etc/rsyslog.d/sftp.conf
sudo systemctl restart rsyslog

SELinux Considerations for Directories Outside /home

CentOS Stream 9 enables SELinux in Enforcing mode out of the box. If you place the chroot under /var/sftp or /data/sftp and run into Permission denied errors despite having the right chmod settings, SELinux is almost certainly blocking writes. Enable the following boolean to allow chroot write access:

sudo setsebool -P ssh_chroot_rw_homedirs on

Practical Tips for Operations

  1. Always keep an active SSH session open: Never close your current terminal before running systemctl reload sshd. Open a new tab to test logging in; if a typo in the config breaks SSH, you still have your existing session to fix it.
  2. Check logs in /var/log/secure or journalctl: OpenSSH logs every error clearly, from incorrect ChrootDirectory permissions to wrong key paths. Run sudo tail -f /var/log/secure (or sudo journalctl -u sshd -f) to see the exact cause instead of guessing.
  3. Use the %u variable in paths: Setting ChrootDirectory /var/sftp/%u lets you write the configuration once. When you onboard 10 more partners later, you just create the users in the sftpusers group and set up their corresponding directories.
  4. Automate old file cleanup: The upload directory can quickly fill up if partners push logs or backups continuously. Set up a cronjob or systemd-tmpfiles to automatically delete files older than 30 days to avoid unexpected disk exhaustion.
Share: