Kahibaro
Discord Login Register

FTP/SFTP

Understanding FTP and SFTP in a Modern Linux Environment

FTP and SFTP both move files over a network, but they are fundamentally different technologies with very different security properties and deployment patterns. For modern Linux servers, SFTP (via SSH) is usually the default choice, while FTP is maintained mainly for legacy compatibility or specific workflows.

This chapter focuses on:

Networking, firewalls, and general SSH concepts are assumed to be known from other chapters; here we focus on the file-transfer aspects.


FTP vs SFTP: Key Differences

Protocol and Transport

When to Use Which

FTP on Linux: Basic Concepts

Active vs Passive Mode (High Level)

FTP uses separate connections for control and data:

Understanding this matters mainly when configuring your FTP server’s passive port range and firewall rules.

Common FTP Server Software

On Linux, popular FTP daemons include:

Below, examples will primarily use vsftpd, as it’s common on major distributions.


Installing and Enabling an FTP Server (vsftpd)

Packages and commands vary slightly per distribution, but the pattern is similar.

Installation

On Debian/Ubuntu:

sudo apt update
sudo apt install vsftpd

On RHEL/Fedora:

sudo dnf install vsftpd

On Arch Linux:

sudo pacman -S vsftpd

Basic Service Management

sudo systemctl enable vsftpd --now      # enable at boot and start immediately
sudo systemctl status vsftpd

If your firewall is enabled, open the FTP control port (21) and any passive ports you will configure (details below). Firewall setup is covered in firewall-related chapters; here just note which ports you will need.


Basic vsftpd Configuration

The main configuration file is usually /etc/vsftpd.conf.

Always back it up before changing:

sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.backup

After any change:

sudo systemctl restart vsftpd

Disabling Anonymous Access

For a non-public server, you usually want authenticated access only:

In /etc/vsftpd.conf, ensure:

anonymous_enable=NO
local_enable=YES
write_enable=YES

Explanation:

Restricting Local Users to Their Home Directories (Chroot)

A common requirement: users should only see their own directory.

In /etc/vsftpd.conf:

chroot_local_user=YES
allow_writeable_chroot=YES

Notes:

Example:

sudo mkdir /home/alice/ftp
sudo chown alice:alice /home/alice/ftp

Then have Alice upload only into /home/alice/ftp, while /home/alice remains non-writable for vsftpd’s chroot rules if you turn off allow_writeable_chroot.

Enabling Passive Mode

To work well with firewalls/NAT, configure a port range for passive connections:

In /etc/vsftpd.conf:

pasv_min_port=40000
pasv_max_port=40100

Then open those ports in your firewall and (if needed) in your router / NAT configuration.

If your server sits behind NAT, many setups also require:

pasv_address=your.public.ip.address

Where your.public.ip.address is the external IP clients will connect to.


FTP User Management Patterns

System Users as FTP Users

Simplest approach: existing Linux users log in with their system username/password.

You can restrict shell access for “FTP-only” users by giving them a nologin shell:

sudo useradd -m -s /usr/sbin/nologin ftpuser1
sudo passwd ftpuser1

This user can authenticate for FTP but cannot log into a shell (e.g., via SSH).

FTP-Only User List (User Allow/Deny)

vsftpd supports user whitelists/blacklists for extra control:

In /etc/vsftpd.conf:

userlist_enable=YES
userlist_file=/etc/vsftpd.userlist
userlist_deny=NO

With userlist_deny=NO, only users listed in /etc/vsftpd.userlist can log in.

Create/edit the file:

echo "ftpuser1" | sudo tee -a /etc/vsftpd.userlist

SFTP: File Transfers via SSH

SFTP vs SCP

Both sftp and scp transfer files over SSH, but:

For modern use, SFTP is generally preferred over SCP.

Basic SFTP Usage (Client Side)

From a Linux client:

sftp user@server

You’ll be dropped into an interactive SFTP shell, with commands like:

Non-interactive usage:

sftp user@server:/remote/path/file /local/path/
sftp /local/path/file user@server:/remote/path/

Key-based authentication, SSH configuration, and tunneling are covered in SSH-related chapters; they apply equally to SFTP.


Configuring an SFTP-Only Subsystem (OpenSSH)

By default, OpenSSH’s SFTP support allows full SSH shell access (unless restricted). Many deployments need SFTP without shell login, often with chrooting.

This is controlled in /etc/ssh/sshd_config.

Always back it up first:

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup

After changes:

sudo systemctl restart sshd   # or ssh, depending on distro

Confirming the SFTP Subsystem

Most modern sshd configs already have something like:

Subsystem sftp /usr/lib/openssh/sftp-server

or

Subsystem sftp internal-sftp

Prefer internal-sftp when possible; it runs SFTP code inside the sshd process and is easier to combine with chroot.

If not present, add:

Subsystem sftp internal-sftp

Creating SFTP-Only, Chrooted Users

Goal: Users can:

1. Create a Group for SFTP Users

sudo groupadd sftpusers

2. Add a User Restricted to SFTP

sudo useradd -m -g sftpusers -s /usr/sbin/nologin sftpuser1
sudo passwd sftpuser1

3. Prepare the Chroot Directory

OpenSSH has strict requirements: the chroot directory must be owned by root and not writable by others.

Common pattern:

sudo mkdir -p /sftp/sftpuser1/upload
sudo chown root:root /sftp
sudo chmod 755 /sftp
sudo chown root:root /sftp/sftpuser1
sudo chmod 755 /sftp/sftpuser1
sudo chown sftpuser1:sftpusers /sftp/sftpuser1/upload
sudo chmod 755 /sftp/sftpuser1/upload

The user will be chrooted to /sftp/sftpuser1 but will actually upload into upload/, which is owned by them.

4. Configure sshd_config Match Block

Append something like this to /etc/ssh/sshd_config:

Match Group sftpusers
    ChrootDirectory /sftp/%u
    ForceCommand internal-sftp
    X11Forwarding no
    AllowTcpForwarding no

Explanation:

Restart SSH:

sudo systemctl restart sshd

5. Testing

From a client:

sftp sftpuser1@server

Attempting ssh sftpuser1@server should fail with a “This account is currently not available” or similar message, due to the nologin shell and ForceCommand internal-sftp.


Security and Hardening Considerations

For FTP

For SFTP

Integrating FTP/SFTP with Other Services

Typical integration patterns include:

Troubleshooting Common FTP/SFTP Issues

FTP-Specific Issues

SFTP-Specific Issues

By understanding the distinct roles and mechanics of FTP and SFTP, and by leveraging vsftpd and OpenSSH’s configuration features, you can provide secure, controlled file-transfer services that integrate cleanly with the rest of your Linux-based network services.

Views: 22

Comments

Please login to add a comment.

Don't have an account? Register now!