Skip to content

26. Secure File Transfer

Transferring files between remote hosts is a daily administrative task. While legacy FTP is still encountered in legacy infrastructure and firmware management, modern production environments rely on encrypted SSH-backed protocols like SFTP, SCP, and SSHFS.

FTP (File Transfer Protocol, RFC 959) is a plaintext protocol designed to move files between a client and server over TCP/IP.

  • vsftpd (Very Secure FTP Daemon): The default FTP server for most enterprise Linux distributions; highly regarded for security, performance, and simplicity.
  • ProFTPD / Pure-FTPd: Feature-rich alternatives often used in specialized hosting environments.

Unlike standard single-port protocols, FTP uses two distinct connections:

  1. Command / Control Port (TCP 21): Carries authentication, directory traversal, and status codes.
  2. Data Port (TCP 20 or high ports): Opens dynamically to transmit file content or directory listings.

Understanding this distinction is vital because client-side firewalls and NAT routers frequently block active transfers.

Active Mode (Server initiates data connection):
Client (:1050) ──► Port 21 (Commands) ──► Server
Client (:1051) ◄── Port 20 (Data) ─── Server (Blocked by client NAT/firewalls!)
Passive Mode (Client initiates both connections):
Client (:1050) ──► Port 21 (Commands) ──► Server
Client (:1051) ──► Port 40000+ (Data) ──► Server (Firewall friendly!)
  • Active Mode: The client sends the PORT command telling the server to connect back to the client. Modern firewalls reject unsolicited inbound connections from the server.
  • Passive Mode (PASV): The server sends a high ephemeral port to the client, and the client initiates the data connection outbound.
  • Ubuntu / Debian:
    Terminal window
    sudo apt update && sudo apt install vsftpd -y
  • RHEL / Rocky / AlmaLinux:
    Terminal window
    sudo dnf install vsftpd -y
    sudo systemctl enable --now vsftpd

A hardened, production-ready configuration:

# Disallow unauthenticated anonymous access
anonymous_enable=NO
# Allow local system accounts to authenticate
local_enable=YES
# Enable write operations (upload, rename, delete)
write_enable=YES
# Set standard umask for uploaded files (results in 644 files / 755 directories)
local_umask=022
# Jail local users inside their own home directories
chroot_local_user=YES
allow_writeable_chroot=YES
# Run standalone daemon
listen=YES
listen_ipv6=NO
# Restrict Passive mode ports to a fixed range for firewall traversal
pasv_enable=YES
pasv_min_port=40000
pasv_max_port=40100
Terminal window
# Firewalld:
sudo firewall-cmd --permanent --add-service=ftp
sudo firewall-cmd --permanent --add-port=40000-40100/tcp
sudo firewall-cmd --reload
# UFW:
sudo ufw allow 20:21/tcp
sudo ufw allow 40000:40100/tcp

Restart service: sudo systemctl restart vsftpd.

  • CLI Client: ftp <server-ip>
  • GUI Client: FileZilla or Cyberduck configured for FTP with explicit Passive Mode.

26.3 Modern Encrypted Transfers: SFTP & SCP

Section titled “26.3 Modern Encrypted Transfers: SFTP & SCP”

[!WARNING] FTP transmits usernames, passwords, and file contents in unencrypted cleartext. In modern DevOps and cloud architectures, use SFTP or SCP over SSH (Port 22).

An interactive file management subsystem running over an encrypted SSH connection:

Terminal window
# Connect interactively
sftp -P 22 user@server.example.com
# SFTP Interactive Shell Commands:
sftp> ls # List remote directory
sftp> lls # List local directory
sftp> cd /var/www # Change remote path
sftp> lcd ~/Downloads # Change local path
sftp> get remote.tar.gz # Download file
sftp> put local.tar.gz # Upload file
sftp> bye # Disconnect

Fast, non-interactive point-to-point file copying:

Terminal window
# Copy local file to remote server
scp app.zip user@192.168.1.50:/opt/deploy/
# Copy remote file to local machine
scp user@192.168.1.50:/var/log/nginx/access.log ./local_logs/
# Recursive directory copy with custom port (-P)
scp -P 2222 -r ./src user@192.168.1.50:/var/www/

3. Synchronizing Directories over SSH (rsync)

Section titled “3. Synchronizing Directories over SSH (rsync)”

rsync provides delta-transfers over encrypted SSH tunnels:

Terminal window
# Sync directories over SSH with progress and compression
rsync -avz -e 'ssh -p 22' /local/dir/ user@server:/remote/dir/

26.4 Advanced SSH Capabilities: Tunneling & SSHFS

Section titled “26.4 Advanced SSH Capabilities: Tunneling & SSHFS”

Forward network traffic through an encrypted SSH tunnel to access protected internal databases or services:

  • Local Port Forwarding (-L): Access a remote internal service (like a database on port 5432) through your local machine’s port:
    Terminal window
    # Forward local port 5433 to internal database 10.0.0.5:5432 via bastion host
    ssh -L 5433:10.0.0.5:5432 user@bastion.example.com
  • Remote Port Forwarding (-R): Expose a local development port to a remote server:
    Terminal window
    # Forward remote server's port 8080 to your local machine's port 3000
    ssh -R 8080:localhost:3000 user@remote-server.com

2. SSHFS (Mounting Remote Filesystems Locally)

Section titled “2. SSHFS (Mounting Remote Filesystems Locally)”

Mount a remote Linux server directory to a local mount point using SSH:

Terminal window
# Install sshfs
sudo apt install sshfs -y # or dnf install fuse-sshfs
# Mount remote directory
mkdir -p ~/remote_mount
sshfs user@server.example.com:/var/www/html ~/remote_mount
# Unmount when done
fusermount -u ~/remote_mount

26.5 Creating a Restricted SFTP Chroot Jail

Section titled “26.5 Creating a Restricted SFTP Chroot Jail”

For external contractors or automated clients who require file upload privileges without interactive shell terminal access:

Terminal window
# 1. Create dedicated group and restricted user
sudo groupadd sftpusers
sudo useradd -m -g sftpusers -s /sbin/nologin vendor_user
sudo passwd vendor_user
# 2. Append chroot jail rule to /etc/ssh/sshd_config
sudo tee -a /etc/ssh/sshd_config <<EOF
Match Group sftpusers
ChrootDirectory %h
ForceCommand internal-sftp
X11Forwarding no
AllowTcpForwarding no
PasswordAuthentication yes
EOF
# 3. Enforce strict chroot ownership rules (Chroot parent MUST be owned by root)
sudo chown root:root /home/vendor_user
sudo chmod 755 /home/vendor_user
# 4. Create writable sub-folder
sudo mkdir /home/vendor_user/uploads
sudo chown vendor_user:sftpusers /home/vendor_user/uploads
sudo chmod 775 /home/vendor_user/uploads
# 5. Restart SSH
sudo systemctl restart sshd

| Protocol | Transport | Port | Encryption | NAT / Firewall Traversal | Primary Use Case | ||||||| | FTP | Plaintext TCP | 21 + Data | None (Cleartext) | High complexity (Active/Passive) | Legacy firmware, local mirrors | | FTPS | SSL/TLS over TCP | 990 / 21 | TLS Encrypted | Complex | Encrypted legacy FTP | | SFTP | SSH Subsystem | 22 (SSH) | SSH Ciphers | Single port 22 (Firewall friendly) | Enterprise secure file exchange | | SCP | SSH Exec | 22 (SSH) | SSH Ciphers | Single port 22 | Fast automated CLI scripts | | SSHFS | FUSE over SSH | 22 (SSH) | SSH Ciphers | Single port 22 | Mounting remote disks locally |