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.
26.1 Legacy FTP & vsftpd Architecture
Section titled “26.1 Legacy FTP & vsftpd Architecture”FTP (File Transfer Protocol, RFC 959) is a plaintext protocol designed to move files between a client and server over TCP/IP.
Common Linux FTP Servers
Section titled “Common Linux FTP Servers”- 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.
The Dual-Port Architecture
Section titled “The Dual-Port Architecture”Unlike standard single-port protocols, FTP uses two distinct connections:
- Command / Control Port (TCP 21): Carries authentication, directory traversal, and status codes.
- Data Port (TCP 20 or high ports): Opens dynamically to transmit file content or directory listings.
Active vs Passive (PASV) Mode
Section titled “Active vs Passive (PASV) Mode”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
PORTcommand 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.
26.2 Deploying and Hardening vsftpd
Section titled “26.2 Deploying and Hardening vsftpd”1. Installation
Section titled “1. Installation”- Ubuntu / Debian:
Terminal window sudo apt update && sudo apt install vsftpd -y - RHEL / Rocky / AlmaLinux:
Terminal window sudo dnf install vsftpd -ysudo systemctl enable --now vsftpd
2. Configuration (/etc/vsftpd.conf)
Section titled “2. Configuration (/etc/vsftpd.conf)”A hardened, production-ready configuration:
# Disallow unauthenticated anonymous accessanonymous_enable=NO
# Allow local system accounts to authenticatelocal_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 directorieschroot_local_user=YESallow_writeable_chroot=YES
# Run standalone daemonlisten=YESlisten_ipv6=NO
# Restrict Passive mode ports to a fixed range for firewall traversalpasv_enable=YESpasv_min_port=40000pasv_max_port=401003. Firewall Rules
Section titled “3. Firewall Rules”# Firewalld:sudo firewall-cmd --permanent --add-service=ftpsudo firewall-cmd --permanent --add-port=40000-40100/tcpsudo firewall-cmd --reload
# UFW:sudo ufw allow 20:21/tcpsudo ufw allow 40000:40100/tcpRestart service: sudo systemctl restart vsftpd.
4. Testing the Server
Section titled “4. Testing the Server”- 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).
1. SFTP (SSH File Transfer Protocol)
Section titled “1. SFTP (SSH File Transfer Protocol)”An interactive file management subsystem running over an encrypted SSH connection:
# Connect interactivelysftp -P 22 user@server.example.com
# SFTP Interactive Shell Commands:sftp> ls # List remote directorysftp> lls # List local directorysftp> cd /var/www # Change remote pathsftp> lcd ~/Downloads # Change local pathsftp> get remote.tar.gz # Download filesftp> put local.tar.gz # Upload filesftp> bye # Disconnect2. SCP (Secure Copy Protocol)
Section titled “2. SCP (Secure Copy Protocol)”Fast, non-interactive point-to-point file copying:
# Copy local file to remote serverscp app.zip user@192.168.1.50:/opt/deploy/
# Copy remote file to local machinescp 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:
# Sync directories over SSH with progress and compressionrsync -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”1. SSH Port Forwarding (Tunneling)
Section titled “1. SSH Port Forwarding (Tunneling)”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 hostssh -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 3000ssh -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:
# Install sshfssudo apt install sshfs -y # or dnf install fuse-sshfs
# Mount remote directorymkdir -p ~/remote_mountsshfs user@server.example.com:/var/www/html ~/remote_mount
# Unmount when donefusermount -u ~/remote_mount26.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:
# 1. Create dedicated group and restricted usersudo groupadd sftpuserssudo useradd -m -g sftpusers -s /sbin/nologin vendor_usersudo passwd vendor_user
# 2. Append chroot jail rule to /etc/ssh/sshd_configsudo tee -a /etc/ssh/sshd_config <<EOFMatch Group sftpusers ChrootDirectory %h ForceCommand internal-sftp X11Forwarding no AllowTcpForwarding no PasswordAuthentication yesEOF
# 3. Enforce strict chroot ownership rules (Chroot parent MUST be owned by root)sudo chown root:root /home/vendor_usersudo chmod 755 /home/vendor_user
# 4. Create writable sub-foldersudo mkdir /home/vendor_user/uploadssudo chown vendor_user:sftpusers /home/vendor_user/uploadssudo chmod 775 /home/vendor_user/uploads
# 5. Restart SSHsudo systemctl restart sshd26.6 File Transfer Protocols Comparison
Section titled “26.6 File Transfer Protocols Comparison”| 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 |