Setting Up A Basic Firewall On Redhat/CentOS
CentOS / Redhat Iptables Firewall Configuration Tutorial
Evans
Last Update 2 months ago
How do I configure a host-based firewall called Netfilter (iptables) under CentOS / Fedora / Redhat Enterprise Linux( RHEL)?
Netfilter is a host-based firewall for Linux operating systems. It is included as part of the Linux distribution and it is activated by default. This firewall is controlled by the program called iptables.
Netfilter filtering take place at the kernel level, before a program can even process the data from the network packet.
The default config files for RHEL / CentOS / Fedora Linux are:
- /etc/sysconfig/iptables – The system scripts that activate the firewall by reading this file.
Type the following command:
Sample outputs:
Type the following two commands to turn on the firewall:
There are a total of 4 chains:
- INPUT – The default chain is used for packets addressed to the system. Use this to open or close incoming ports (such as 80,25, and 110 etc) and ip addresses / subnet (such as 202.54.1.20/29).
- OUTPUT – The default chain is used when packets are generating from the system. Use this open or close outgoing ports and ip addresses / subnets.
- FORWARD – The default chains is used when packets send through another interface. Usually used when you setup Linux as router. For example, eth0 connected to ADSL/Cable modem and eth1 is connected to local LAN. Use FORWARD chain to send and receive traffic from LAN to the Internet.
- RH-Firewall-1-INPUT – This is a user-defined custom chain. It is used by the INPUT, OUTPUT and FORWARD chains.
- Each packet starts at the first rule in the chain .
- A packet proceeds until it matches a rule.
- If a match found, then control will jump to the specified target (such as REJECT, ACCEPT, DROP).
- The target ACCEPT means allow packet.
- The target REJECT means to drop the packet and send an error message to remote host.
- The target DROP means drop the packet and do not send an error message to remote host or sending host.
Edit /etc/sysconfig/iptables, enter:
You will see the default rules as follows:
Find lines:
Update as follows to change the default policy to DROP from ACCEPT for the INPUT and FORWARD built-in chains:
Append the following lines before final COMMIT line:
Find the lines:
Update it as follows:
Open Port
To open port 80 (Http server) add the following before COMMIT line:
To open port 53 (DNS Server) add the following before COMMIT line:
To open port 443 (Https server) add the following before COMMIT line:
To open port 25 or 587 (smtp server) add the following before COMMIT line:
Save and close the file. Edit /etc/sysconfig/iptables-config, enter:
Make sure ftp module is loaded with the space-separated list of modules
To restart firewall, type the following commands:
Edit /etc/sysctl.conf to defend against certain types of attacks and append / update as follows:
Alternate Configuration Option:
You can skip /etc/sysconfig/iptables file and create a shell script from scratch as follows: