Configuring a firewall for an FTP server on Debian involves allowing the necessary ports for FTP traffic (control and data) while ensuring security—especially for passive mode connections. Below are step-by-step instructions using UFW (Uncomplicated Firewall) and iptables, the two most common firewall tools for Debian.
Before configuring the firewall, ensure:
vsftpd) is installed and running.vsftpd, edit /etc/vsftpd.conf and set:pasv_enable=YES
pasv_min_port=30000 # Adjust to your desired range
pasv_max_port=31000 # Adjust to your desired range
30000-31000 with the port range you configure in the FTP server.UFW simplifies firewall management with user-friendly commands. Follow these steps:
sudo apt update
sudo apt install ufw
sudo ufw enable
Confirm enabling with Y when prompted.
sudo ufw allow 21/tcp
sudo ufw allow 20/tcp
30000:31000 with your FTP server’s configured range.sudo ufw allow 30000:31000/tcp
Apply changes without rebooting:
sudo ufw reload
Check the status to ensure rules are applied:
sudo ufw status verbose
You should see entries for ports 21/tcp, 20/tcp, and your passive mode range.
For users needing more granular control, iptables offers low-level rule management.
sudo apt update
sudo apt install iptables
Add rules to allow FTP traffic:
sudo iptables -A INPUT -p tcp --dport 21 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 20 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 30000:31000 -j ACCEPT
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited
Debian does not save iptables rules by default. Use iptables-persistent to retain them across reboots:
sudo apt install iptables-persistent
sudo netfilter-persistent save
Confirm saving with Y.
For systems without iptables-persistent, create a startup script:
sudo nano /etc/network/if-pre-up.d/iptables
Add the following content:
#!/bin/sh
/sbin/iptables-restore < /etc/iptables/rules.v4
Make the script executable:
sudo chmod +x /etc/network/if-pre-up.d/iptables
Check applied rules:
sudo iptables -L -n -v
Ensure entries for ports 21, 20, and your passive mode range exist.
1024-65535 range to minimize exposure.sudo ufw allow from 192.168.1.0/24 to any port 21/tcp).By following these steps, you can secure your Debian FTP server while ensuring reliable connectivity for clients. Adjust port ranges and security settings based on your network environment and requirements.