Console Login

Replacing FTP with chrooted SFTP on CentOS 5 using OpenSSH

The problem

Plain FTP sends the username, the password and every file in cleartext. Anyone who can capture traffic between the client and the server can read the credentials with a packet sniffer such as tcpdump or Wireshark. For a server that holds customer data, this is a security weakness. It can also be a compliance problem, because personal data protection rules, such as the Norwegian Personal Data Act, expect reasonable protection of data in transit.

The usual replacement is SFTP over SSH. It brings a second problem. A normal SSH account gives the user a login shell and a view of the whole filesystem. A web developer who only needs to upload files to one site should not be able to browse /etc or run arbitrary commands.

A typical goal is therefore:

  • Close port 21 and move file transfers to port 22.
  • Give each upload user access to one directory tree only.
  • Give that user no shell, no port forwarding and no X11 forwarding.

Why it happens

FTP dates from a time when networks were trusted. Its control channel and data channel are both unencrypted, and the data channel uses extra ports, which complicates firewall rules. Adding TLS gives FTPS, which is a different thing from SFTP. FTPS is the old FTP protocol wrapped in TLS, and it keeps the separate data connections and the passive-port problem.

SFTP is not FTP at all. It is a separate file-transfer protocol that runs as a subsystem inside an SSH session, so it needs one port and one encrypted channel.

The shell-access problem exists because a stock sshd treats an SFTP login as an ordinary SSH login. Two features change that:

  • The Match block in sshd_config applies settings to selected users or groups.
  • The ChrootDirectory option makes sshd call chroot after authentication. For this to work without copying binaries and device files into the jail, the SFTP server must run inside the sshd process. That is what the internal-sftp subsystem does.

A common correction to older write-ups concerns the version number. ChrootDirectory and internal-sftp were introduced in OpenSSH 4.9, not 4.8. CentOS 5 ships a base version of OpenSSH 4.3, and the vendor backported the feature into its package. The package version number alone therefore does not show whether the directive is available. The man page on the machine is the reliable check.

Source of truth

Three references back the approach:

  • The OpenSSH 4.9 release notes are where the chroot option and the in-process SFTP server first appear. They confirm that the feature belongs to 4.9 rather than 4.8.
  • The sshd_config manual page describes ChrootDirectory, ForceCommand, Match and the internal-sftp subsystem. It confirms the key rule: the chroot path and every component of it must be owned by root and must not be writable by any other user or group. It also confirms that %h expands to the user's home directory.
  • RFC 4217 defines FTP over TLS. It shows that FTPS is an extension of the FTP command and data channel model, which is why it is a different protocol from SFTP.

On the target system, the installed documentation is authoritative for what that build supports:

rpm -q openssh-server
man sshd_config | grep -A8 ChrootDirectory

If the second command prints nothing, the installed OpenSSH has no chroot support and needs an update (or one of the alternatives below).

Fix options

1. OpenSSH ChrootDirectory with internal-sftp

Users in a group are confined to a directory by sshd itself. No extra software is needed, and no binaries or /dev entries are copied into the jail. The cost is a strict ownership rule on the jail path.

2. ChrootDirectory with the external sftp-server

This uses the old Subsystem sftp /usr/libexec/openssh/sftp-server line. Inside the jail, the user then needs the binary, its shared libraries and, for logging, a /dev/log socket. This works but is tedious to maintain.

3. A restricted shell such as rssh or scponly

The user logs in with a special shell that only permits sftp and scp. It does not confine the filesystem view by itself, so it is usually combined with a chroot. It is also another package to maintain, and it has a history of security fixes.

4. FTPS

An FTP daemon such as vsftpd or ProFTPD with TLS keeps FTP clients working. It also keeps the extra data ports, the passive port range and the firewall complexity.

5. Keep FTP and tunnel it

Wrapping FTP in an SSH tunnel is fragile because of the separate data channel. It is not recommended.

Why this fix

Option 1 is chosen for these reasons:

  • It uses only the SSH daemon that is already installed and already allowed through the firewall.
  • The confinement is done by sshd and does not depend on a separate program.
  • The jail needs no copied binaries, so there is nothing to patch inside it.
  • Port 21 can be closed, and a single audited service handles both administration and transfer.
  • The configuration is only a few lines, and per-user or per-group behavior is controlled with Match.

The main trade-off is that the user cannot write to the top of the jail. This is handled with a writable subdirectory.

Fixing it step by step

This procedure assumes CentOS 5.5 or similar with root access. It uses an example user exampleuser whose home directory is the jail. No command output is reproduced below, so check each result on the target system as described.

1. Confirm that the directive is supported

man sshd_config | grep -A8 ChrootDirectory

2. Edit the SSH daemon configuration

Back up the file first:

cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config.orig

In /etc/ssh/sshd_config, replace the external subsystem with the internal one:

#Subsystem sftp /usr/libexec/openssh/sftp-server
Subsystem sftp internal-sftp

Then add the following at the end of the file. A Match block applies to every line after it until the next Match or the end of the file, so global options must stay above it.

Match Group sftponly
    ChrootDirectory %h
    ForceCommand internal-sftp
    AllowTcpForwarding no
    X11Forwarding no

3. Create the group and the user

A nologin shell is used so that the account has no shell. ForceCommand already prevents shell use for this group, and the disabled shell is a second barrier.

groupadd sftponly
useradd -d /var/www/vhosts/exampleuser -g sftponly -s /sbin/nologin exampleuser
passwd exampleuser

On older systems /sbin/nologin may not be listed in /etc/shells. With ForceCommand internal-sftp this does not normally matter, because the forced command is run by the daemon. If logins are refused, check /var/log/secure.

4. Set ownership and permissions

The directory named in ChrootDirectory, and each parent directory above it, must be owned by root and not writable by group or others. Otherwise sshd aborts the session.

mkdir -p /var/www/vhosts/exampleuser
chown root:root /var/www/vhosts /var/www /var/www/vhosts/exampleuser
chmod 755 /var/www /var/www/vhosts /var/www/vhosts/exampleuser

mkdir /var/www/vhosts/exampleuser/html
chown exampleuser:sftponly /var/www/vhosts/exampleuser/html
chmod 755 /var/www/vhosts/exampleuser/html

The chown on /var/www and /var/www/vhosts is only correct if no other service depends on a different owner there. Check with ls -ld first. If the paths have other owners, use a dedicated jail root such as /srv/sftp/exampleuser instead.

5. Validate and reload the daemon

Check the syntax before restarting, and keep the current SSH session open until a second session has logged in successfully.

sshd -t
service sshd restart

6. Test from a client

sftp exampleuser@your-server-ip

The expected result of the test:

  • The login succeeds and pwd shows /, which is the jail root.
  • ls shows only html.
  • Uploads into html succeed.
  • Uploads into / are refused.

An interactive SSH login as the same user should be closed immediately without a shell:

ssh exampleuser@your-server-ip

The exact text printed here depends on the configuration. Do not rely on a specific banner. What matters is that no shell prompt appears.

If the login fails, run tail /var/log/secure on the server. A message about bad ownership or modes on the chroot path means step 4 is incomplete. Then close port 21 in the firewall and stop and disable the FTP daemon, if there is one:

service vsftpd stop
chkconfig vsftpd off

The service name differs if another FTP server is installed.

Looking back

What worked

The method uses the stock daemon and a few configuration lines. It gives encrypted transfers, a single port and a per-group jail without a shell. Most failures come from path ownership, as the sshd_config documentation says.

Limits

  • Encryption costs CPU. On a heavily shared or CPU-starved virtual server, SFTP throughput can be lower than raw FTP. The effect depends on the cipher and the hardware, and should be measured on the actual system rather than assumed.
  • The jail root cannot be writable by the user, so a writable subdirectory is required. Web applications that expect to write to the home directory need adjusting.
  • The user's files must be readable by the web server. Group ownership or ACLs may be needed, and a default umask of 022 on uploaded files can block group access.
  • SFTP logging from within a chroot needs a /dev/log socket inside the jail, which reintroduces some of the setup that internal-sftp avoids.
  • SELinux in enforcing mode can restrict access to some paths. Check /var/log/audit/audit.log if permissions look right but access still fails.
  • Password authentication still exposes accounts to guessing. Public-key authentication and rate limiting add protection.

When to choose another method

If the installed OpenSSH has no ChrootDirectory, update the package or use a restricted shell combined with a manually built jail. If clients can only speak FTP, FTPS is the fallback, with a fixed passive port range and matching firewall rules. Port 21 should not be left open for plain FTP in either case.