Linux Iptables Setup Firewall For a Web Server
To set up a firewall for a web server using iptables in Linux, you can use the following steps:
- Flush existing rules:
sudo iptables -F- Set the default policy to drop incoming and forward traffic:
sudo iptables -P INPUT DROPrnsudo iptables -P FORWARD DROP- Allow incoming traffic on the loopback interface:
sudo iptables -A INPUT -i lo -j ACCEPT- Allow incoming SSH traffic:
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPTReplace "22" with the actual port number used by the SSH daemon.
- Allow incoming HTTP traffic:
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPTReplace "80" with the actual port number used by the web server.
- Allow incoming HTTPS traffic:
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPTReplace "443" with the actual port number used by the web server for HTTPS traffic.
- Save the firewall rules:
sudo service iptables saveThese iptables rules allow incoming traffic on the loopback interface, incoming SSH traffic, incoming HTTP traffic, and incoming HTTPS traffic, while blocking all other incoming and forward traffic. This provides a basic firewall protection for a web server, which can be customized further based on specific requirements.
