![]() |
Using Linux iptables or ipchains to set up an internet gateway / firewall / router for home or office
|
Methods of connecting your network to the internet:
This tutorial will cover using a linux computer as a gateway between a private network and the internet. Any internet connection whether it be a dial-up PPP, DSL, cable modem or a T1 line can be used. In the case of most dial-up PPP connections and cable modem connections, only a single IP address is issued allowing only one computer to connect to the internet at a time. Using Linux and iptables / ipchains one can configure a gateway which will allow all computers on a private network to connect to the internet via the gateway and one external IP address, using a technology called "Network Address Translation" (NAT) or masquerading and private subnets. Iptables/ipchains can also be configured so that the Linux computer acts as a firewall, providing protection to the internal network.
Related YoLinux Tutorials:
|
Note: References to ipfwadm and ipchains refer to older deprecated software.
Note: Red Hat 7.1-9.0 and the default Linux 2.4 kernel may use ipchains or iptables but not both. Iptables is the preferred firewall as it supports "state" and can recognize if a network connection has already been "ESTABLISHED" or if the connection is related to the previous connection (required for ftp which makes multiple connections on different ports). Ipchains can not. Ipchain rules take precedence over iptables rules. During system boot, the kernel attempts to activate ipchains, then attempts to activate iptables. If ipchain rules have been activated, the kernel will not start iptables. Red Hat 7.1 will not support ipchains unless that option is configured (during install or later). If during install you select "Disable Firewall - no protection" then ipchains will not be available and you must rely upon iptables for a manual firewall configuration. (iptables only. ipchains will be unavailable) GUI configuration:
The default Red Hat 7.1+ Linux 2.4 kernel is compiled to support both iptables and ipchains. Kernel support for ipchains is available during a kernel configuration and compilation. During make xconfig or make menuconfig turn on the feature: "IP: Netfilter Configuration" + "ipchains (2.2-style) support".
Check your installation by using the command: rpm -q iptables ipchains
[Potential Pitfall]: When performing an
upgrade instead of a new install, the upgrade software will not install
iptables as did not exist on the system previously. It will perform an
upgrade to a newer version of ipchains. If you wish to use iptables,
you must manually install the iptables RPM.
[Potential Pitfall]: The Linux operating system kernel may load or not load what you had expected. Use the command lsmod to see if ip_tables or ip_chains were loaded. Switching a running system from ipchains to iptables: (Red Hat 7.1-9.0 - Linux kernel 2.4 specific)
An individual on a computer on the private network may point their web browser to a site on the internet. This request is recognized to be beyond the local network so it is routed to the Linux gateway using the private network address. The request for the web page is sent to the web site using the external internet IP address of the gateway. The request is returned to the gateway which then translates the IP address to computer on the private network which made the request. This is often called IP masquerading. The software interface which enables one to configure the kernel for masquerading is iptables (Linux kernel 2.4) or ipchains (Linux kernel 2.2) The gateway computer will need two IP addresses and network connections, one to the private internal network and another to the external public internet. A note on private network IP addresses: A set of IP addresses has been reserved by IANA for private networks. They range from 192.168.0.1 to 192.168.254.254 for a typical small business or home network and are often referred to as CIDR private network addresses. Most private networks conform to this scheme.
This is detailed in RFC 1918 - Address Allocation for Private Internets. For a description of class A, B, and C networks see the YoLinux Networking Tutorial class description. The private networks may be subdivided into various subnets as desired. Examples:
CertGuide.com: Network Subnets
This example uses a Linux computer connected to the internet using a dial-up line and modem (PPP). The Linux gateway is connected to the internal network using an ethernet card. The internal network consists of Windows PC's. The Linux box must be configured for the private internal network and PPP for the dial-up connection. See the PPP tutorial to configure the dial-up connection. Use the ifconfig command to configure the private network. i.e. (as root) /sbin/ifconfig eth1 192.168.10.101 netmask 255.255.255.0 broadcast 192.168.10.255 This is often configured during install or can be configured using the Gnome tool neat (or the admin tool Linuxconf or netcfg for older Red Hat systems). System changes made with the ifconfig or route commands are NOT permanent and are lost upon system reboot. Permanent settings are held in configuration scripts executed during system boot. (i.e. /etc/sysconfig/...) See the YoLinux Networking tutorial for more information on assigning network addresses. Run one of the following scripts on the Linux gateway computer:
iptables:
ipchains:
A PPP connection as described by the YoLinux PPP tutorial will create the PPP network connection as the default route.
High speed connections to the internet result in an ethernet connection to the gateway. Thus the gateway is required to possess two ethernet Network Interface Cards (NICs), one for the connection to the private internal network and another to the public internet. The ethernet cards are named eth and are numbered uniquely from 0 upward. Use the ifconfig command to configure both network interfaces.
This is often configured during install or can be configured using the Gnome tool neat (or the admin tool Linuxconf or netcfg for older Red Hat systems). System changes made with the ifconfig or route commands are NOT permanent and are lost upon system reboot. Permanent settings are held in configuration scripts executed during system boot. (i.e. /etc/sysconfig/...) See the YoLinux Networking tutorial for more information on assigning network addresses. Run the appropriate script on the linux computer where eth0 is connected to the internet and eth1 is connected to a private LAN:
iptables:
ipchains:
Create a route for internal packets: route add -net 192.168.10.0 netmask 255.255.255.0 gw XXX.XXX.XXX.XXX dev eth1Where XXX.XXX.XXX.XXX is the internet gateway defined by your ISP. For more information on routing see the YoLinux networking tutorial Note: While this configuration requires that the Linux gateway computer have two network cards, if you only have one PCI slot available you may use a card such as the Intel Pro 100 or Pro 1000 Dual Port which has two ethernet connections which reside on a single card. (This is what I use) Yolinux Hardware tutorial: More on Network interface cards
|
General /sbin/iptables format to add rules:
Six pre-defined "chain" rules are available:
For the full info see the man page for iptables.
General /sbin/ipchains format to add rules:
ipchains options:
Four chain rule types are available:
For the full info see the man page for ipchains. To add firewall rules read the links provided below.
Windows '95 Configuration:
Linux computers:
iptables:Deny a specific host: iptables -I INPUT -s XXX.XXX.XXX.XXX -j DROP Block ports by adding the following firewall rules:
Debugging and logging:
Another approach to firewalls is to drop everything and then grant access to each port you may need.
ipchains:This script configures firewall rules for a Linux computer with two ethernet ports. One port connects the computer to the internet with an external address of XXX.XXX.XXX.XXX. The other ethernet port connects the computer to an internal network of 192.168.10.0 to 192.168.10.255. This script is more complex but preferred to the previous scripts because of the extra security that the extra firewall rules offer. The script does work with a system running portsentry. For more on portsentry see the YoLinux Internet Security: portsentry Tutorial.
Internet external network interface: eth0
Gateway script for ipchains firewall and NAT:
Notes:
Red Hat 7.1 will configure firewall rules as an option during installation. Note that the firewall rules are generated for ipchains. The configuration tool /usr/bin/gnome-lokkit was used to perform this setup.
Example of the security configuration: /etc/sysconfig/ipchains
Save/restore an tables/ipchains configuration:
The system init script looks for the file name /etc/sysconfig/ipchains instead of /etc/sysconfig/ipchains.rules. This will make the rules accessible to the init script which will invoke the rules upon system boot. See the YoLinux Init process tutorial for more information on init scripts and system boot procedures. Also see: how to turn off ICMP and look invisible to ping.
The TCP Man page - Linux Programmer's Manual and /usr/src/linux/proc.txt [link] (Kernel 2.4) cover /proc/sys/net/ipv4/* file descriptions. Also see:
Choose one of the following to allow the Linux kernel to forward IP packets:
All the above methods will result in a proc file value of "1" to
allow TCP packet forwarding. Options 2 and 3 set boot configurations in
a configuration file and will not take effect until system boot.
Note: The /proc directory is NOT on your hard drive but is present in the running kernel.
CIDR Notation:
GUI tools and scripts exist to help you with the configuration of ipchains. See:
Included with Red Hat 7.x is the Gnome GUI tool gnome-lokkit. (ipchains) Tools for iptables configuration:
|
Return to http://YoLinux.com home page
Return to YoLinux Tutorial Index Feedback Form Copyright © 2000, 2001, 2002, 2003, 2004 by Greg Ippolito |