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:

  1. Flush existing rules:
sudo iptables -F
  1. Set the default policy to drop incoming and forward traffic:
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
  1. Allow incoming traffic on the loopback interface:
sudo iptables -A INPUT -i lo -j ACCEPT
  1. Allow incoming SSH traffic:
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Replace “22” with the actual port number used by the SSH daemon.

  1. Allow incoming HTTP traffic:
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

Replace “80” with the actual port number used by the web server.

  1. Allow incoming HTTPS traffic:
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

Replace “443” with the actual port number used by the web server for HTTPS traffic.

  1. Save the firewall rules:
sudo service iptables save

These 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.

Leave a Comment