Securing the File Transfer Protocol (FTP) port requires upgrading from unencrypted FTP (Port 21) to secure protocols like SFTP (Port 22) or FTPS (Port 21 with explicit TLS), restricting port access through firewalls, restricting passive port ranges, enforcing strong authentication, and deploying intrusion prevention tools like Fail2ban to block brute-force attacks.
Understanding the Risks of Unsecured FTP (Port 21)
The standard File Transfer Protocol, defined in RFC 959 by the Internet Engineering Task Force (IETF), was designed decades ago without native encryption. By default, standard FTP communicates over TCP Port 21 for control commands and TCP Port 20 for active data transfers. Because all traffic on these default channels travels in cleartext, network eavesdroppers can capture sensitive data, including login credentials, session tokens, and confidential file contents using basic packet analyzers.
In modern network security, leaving standard TCP Port 21 open and unencrypted exposes servers to multiple attack vectors:
- Credential Sniffing: Attackers executing man-in-the-middle (MitM) or ARP spoofing attacks on local or routed networks can easily extract plain-text username and password combinations.
- Brute-Force and Dictionary Attacks: Port 21 is heavily targeted by automated internet botnets scanning public IPv4 ranges for weak passwords or default administrative credentials.
- FTP Bounce Attacks: Vulnerable FTP servers operating in active mode can be manipulated to relay network traffic to third-party targets, disguising the attacker's true IP address.
- Data Interception and Tampering: Unencrypted data channels allow attackers to intercept, read, or modify files in transit without raising integrity alerts.
Securing the FTP port requires a defense-in-depth strategy: replacing outdated legacy protocols, applying strict network firewalls, hardening server configurations, and isolating file transfer services behind secure network perimeters.
Step 1: Transition from Plain FTP to SFTP or FTPS
The most effective step in securing file transfer services is replacing unencrypted FTP with a secure alternative. Standard FTP over TCP Port 21 should be disabled entirely whenever possible.
Option A: SSH File Transfer Protocol (SFTP) — Recommended
SFTP is not an extension of traditional FTP; it is an entirely separate protocol running over the Secure Shell (SSH) framework, governed by TCP Port 22.
- Single Port Simplicity: SFTP handles authentication, command transmission, and file transfers over a single encrypted TCP port (Port 22 by default). This eliminates the complexity of opening dynamic passive port ranges in firewalls.
- Strong Encryption: All data, commands, and credentials are protected by SSH key exchange algorithms and modern symmetric ciphers such as AES-256-GCM or ChaCha20-Poly1305.
- Key-Based Authentication: SFTP supports SSH public key authentication, allowing administrators to disable password-based logins completely.
To enforce SFTP on a Linux server using OpenSSH, open /etc/ssh/sshd_config and ensure the internal SFTP subsystem is enabled while password authentication is restricted:
Subsystem sftp internal-sftp Match Group sftpusers ChrootDirectory /var/www/sftp/%u ForceCommand internal-sftp PasswordAuthentication no PermitTunnel no AllowAgentForwarding no AllowTcpForwarding no X11Forwarding no
Option B: FTP over TLS (FTPS)
If legacy systems require an authentic FTP command structure, FTPS (FTP Secure) adds Transport Layer Security (TLS) protection to standard FTP. FTPS operates in two distinct modes:
- Explicit FTPS (Recommended): The client connects to standard Port 21 and explicitly requests encryption by issuing the
AUTH TLScommand before sending login credentials. - Implicit FTPS (Legacy): The client establishes an SSL/TLS connection immediately upon connecting to a dedicated port (traditionally TCP Port 990 for control and Port 989 for data). Implicit FTPS is considered legacy and is officially deprecated in favor of Explicit FTPS under RFC 4217.
To enforce Explicit FTPS in vsftpd (/etc/vsftpd.conf), apply the following configuration parameters:
ssl_enable=YES allow_anon_ssl=NO force_local_data_ssl=YES force_local_logins_ssl=YES ssl_tlsv1_2=YES ssl_tlsv1_3=YES ssl_sslv2=NO ssl_sslv3=NO rsa_cert_file=/etc/ssl/certs/ftps.crt rsa_private_key_file=/etc/ssl/private/ftps.key
Step 2: Restrict Access Using Firewalls and Network ACLs
Exposing file transfer ports to the entire public internet invites constant automated attacks. Access control lists (ACLs) and stateful firewalls should be implemented to restrict access to trusted IP addresses or internal networks.
Restricting Access via UFW (Uncomplicated Firewall)
On Ubuntu and Debian systems, UFW allows administrators to restrict access to the SSH/SFTP port (Port 22) or FTPS port (Port 21) exclusively to authorized static IP addresses:
# Allow SFTP access only from a trusted management IP address sudo ufw allow from 203.0.113.50 to any port 22 proto tcp # Block all other public access to Port 21 if legacy FTPS is used sudo ufw deny 21/tcp
Restricting Access via Firewalld (RHEL/CentOS/Rocky Linux)
On Enterprise Linux platforms, firewalld provides zone-based access management:
# Create a dedicated trusted zone for file management sudo firewall-cmd --permanent --zone=trusted --add-source=198.51.100.25/32 sudo firewall-cmd --permanent --zone=trusted --add-port=22/tcp sudo firewall-cmd --reload
Implementing IP Whitelisting via Network Security Groups (NSGs)
In cloud environments such as AWS, Microsoft Azure, or Google Cloud Platform, restrict port access at the hypervisor layer using Cloud Security Groups:
- Remove the default
0.0.0.0/0ingress rule on TCP Port 21 and Port 22. - Create inbound security rules allowing TCP Port 22 (SFTP) or Port 21 (FTPS) strictly from specific office CIDR blocks or corporate VPN exit IPs.
- If file access must be available globally, enforce mandatory Multi-Factor Authentication (MFA) via a zero-trust network access (ZTNA) gateway or VPN prior to granting connection rights to the file server.
Step 3: Harden FTP Server Configuration
Securing the port requires locking down the underlying FTP daemon daemon software (such as vsftpd, ProFTPD, or Microsoft IIS FTP Service).
Disable Anonymous Access
Anonymous FTP permits users to log in without authenticating, typically using the username anonymous or ftp. This creates significant risk for unauthorized data discovery and server abuse.
In vsftpd (/etc/vsftpd.conf):
anonymous_enable=NO
In ProFTPD (/etc/proftpd/proftpd.conf):
RequireValidShell off DenyAll
Isolate Users with Chroot Jails
Prevent authenticated users from navigating up the server file hierarchy into root directories. A chroot jail restricts users strictly to their designated home directory.
In vsftpd.conf:
chroot_local_user=YES allow_writeable_chroot=NO
Note: For security reasons, modern versions of vsftpd disallow users from writing directly to the root of a chrooted directory. Create a writable subdirectory (e.g., /home/username/uploads) inside the locked root folder.
Change Default Listening Ports
While port obfuscation is not a standalone security solution, changing the listening port from TCP Port 21 (or Port 22 for SSH/SFTP) to a non-standard high port (e.g., Port 2121 or Port 2222) eliminates automated background noise from basic internet scanners.
In vsftpd.conf:
listen_port=2121
In SSH /etc/ssh/sshd_config:
Port 2222
After modifying listening ports, ensure your host firewall and Cloud Security Groups match the updated TCP port assignments.
Step 4: Secure Passive Mode Port Ranges
FTP uses two separate channels: a control channel (commands) and a data channel (file listings and contents). In Passive Mode (PASV), the client connects to the control port (Port 21), and the server tells the client which high-numbered unassigned port to use for the data stream.
If dynamic passive ports are improperly configured, firewalls must remain open to huge port ranges, increasing the server's attack surface.
Define a Narrow Passive Port Range
To secure passive mode transfers in FTPS, restrict the server to a specific, narrow range of ephemeral ports, then open only that exact range in your firewall.
In vsftpd.conf:
pasv_enable=YES pasv_min_port=60000 pasv_max_port=60100 pasv_address=203.0.113.10
Open the Passive Range in Firewalls
Once defined in the FTP daemon configuration, update host firewall rules to match:
- UFW:
sudo ufw allow 60000:60100/tcp - Firewalld:
sudo firewall-cmd --permanent --add-port=60000-60100/tcp && sudo firewall-cmd --reload - iptables:
sudo iptables -A INPUT -p tcp --dport 60000:60100 -j ACCEPT
By narrowing the passive range from tens of thousands of ports to 100 ports, you minimize open pathways while supporting concurrent server connections.
Step 5: Implement Brute-Force Protection and Rate Limiting
Even when services are configured securely, exposed ports remain targets for continuous authentication attacks. Deploy dynamic blocking software to protect against automated cracking attempts.
Configuring Fail2ban for FTP and SFTP
Fail2ban monitors server log files (such as /var/log/auth.log or /var/log/vsftpd.log) for repeated authentication failures. Upon detecting brute-force activity, it automatically updates firewall rules to block the offending IP address for a pre-set duration.
To enable protection for SSH/SFTP and vsftpd, create or edit /etc/fail2ban/jail.local:
[sshd] enabled = true port = ssh logpath = %(sshd_log)s backend = %(sshd_backend)s maxretry = 3 bantime = 1d [vsftpd] enabled = true port = ftp,ftps,2121 logpath = /var/log/vsftpd.log maxretry = 3 findtime = 10m bantime = 1d
Restart Fail2ban to enforce the policy:
sudo systemctl restart fail2ban
Technical Comparison of File Transfer Protocol Security Options
Selecting the right file transfer architecture depends on network complexity, firewall constraints, compliance requirements, and client software capabilities.
| Protocol | Default Ports | Encryption Status | Firewall Complexity | Primary Advantage |
|---|---|---|---|---|
| Standard FTP | TCP 21 (Control), TCP 20 (Active Data) | None (Cleartext) | High (Requires dynamic passive port tracking) | Legacy compatibility |
| Explicit FTPS | TCP 21 (Control), Custom Range (Passive Data) | TLS 1.2 / TLS 1.3 | Medium (Requires opening defined passive ranges) | Uses standard FTP commands with strong encryption |
| Implicit FTPS | TCP 990 (Control), TCP 989 (Data) | SSL / TLS (Deprecated) | High (Deprecated protocol under RFC 4217) | Legacy system support only |
| SFTP (SSH) | TCP 22 (Control & Data) | SSH-2 (AES-256, ChaCha20) | Low (Single port required for all traffic) | Highest security, simple firewall rules, supports SSH keys |
Frequently Asked Questions
Can I secure plain FTP without changing to SFTP or FTPS?
No. Standard FTP (Port 21) sends authentication credentials and file contents across the network in cleartext without encryption capabilities. While network firewalls or VPNs can restrict who connects to Port 21, the traffic within the network remains unencrypted and vulnerable to packet sniffing unless wrapped in an encrypted protocol like SFTP or FTPS.
What is the difference between Port 21 and Port 22?
Port 21 is used by legacy File Transfer Protocol (FTP) and Explicit FTPS for control traffic. Port 22 is assigned to Secure Shell (SSH), which powers SFTP (SSH File Transfer Protocol). SFTP over Port 22 is completely distinct from standard FTP and provides fully encrypted data and credential streams over a single connection.
Why do I need to open multiple ports for FTPS but only one for SFTP?
FTPS uses separate connections for control commands (Port 21) and file data transfers (a dynamic passive port range). SFTP multiplexes both control commands and data streams over a single, secure SSH connection (Port 22), making SFTP much simpler to configure through firewalls and NAT routers.
Is changing the default FTP port from 21 to another number sufficient?
No. Changing Port 21 to a non-standard port (such as 2121) is a basic obfuscation technique that reduces log noise from simple automated scanning scripts. Port scanners like Nmap can quickly discover services running on non-standard ports. Port changes must be combined with modern protocol encryption, firewall whitelisting, and strict authentication.
Should I block Port 20 on my server firewall?
Port 20 is used exclusively for traditional active-mode FTP data connections initiated by the server. Most modern clients use passive mode (PASV), rendering active mode obsolete. You can safely block inbound TCP Port 20 on external firewalls, provided your server is configured for secure passive mode or SFTP.
Recommended Next Steps for Server Hardening
To maintain a secure file transfer environment:
- Audit Active Listening Ports: Run
sudo netstat -tulpnorsudo ss -tulpnon your server to verify which processes are listening on Port 21, 22, and associated passive ports. - Migrate Legacy Clients to SFTP: Phase out plain FTP accounts and issue SSH key pairs to users for SFTP access.
- Deploy SSL/TLS Certificates for FTPS: If FTPS must be maintained, issue valid certificates signed by a public Certificate Authority (CA) such as Let's Encrypt rather than using self-signed certificates.
- Enforce Cryptographic Standards: Disable outdated cipher suites, SSLv2, SSLv3, TLS 1.0, and TLS 1.1 in server configuration files. Enforce TLS 1.2 and TLS 1.3 for all FTPS endpoints.
- Review System Audit Logs: Periodically inspect system logs (
/var/log/secureor/var/log/auth.log) to confirm that Fail2ban is successfully blocking unauthorized access attempts.