Linux IP Masquerading and Firewalling

This paper includes my notes on setting up my Linux box as a firewall and to provide IP forwarding, or masquerading, from private networks. All I know I learned from the excellent articles in the next section.

Background Reading


In order to get the kernel talking to the Ethernet card that PacBell delivered, I added the following to /etc/conf.modules:

alias eth1 tulip

I had to obtain and install the latest version of DHCP (1.0.2) to handle both Ethernet cards.

I reconfigured the kernel, which should be 2.2.X, as follows:


Note that I didn't turn on CONFIG_EXPERIMENTAL as per the HOWTOs and it worked just the same.

See Section 3 of RFC 1918 for the IP addresses you should use on your private network. You can also simply use the network that I've used in the initialization file below [].

ipchains is used for packet filtering and replaces ipfwadm in newer kernels. It currently handles masquerading, but the masquerading commands are expected to be pulled out into their own command in the future.

I've put these commands after my first Ethernet commands in /etc/init.d/network.local and slimelinked it from /etc/rcS.d/S40network.local so they get run at boot time.


  1. Values marked in red must be changed to match your own IP address and your Internet gateway.
  2. Values marked in orange may be changed to if you don't want to use the IP address I use on my private network.
  3. Text marked in blue is used to perform IP masquerading between the private network and the outside world.
  4. Text marked in green is used to configure the firewall. The place to put holes in your dike begins with the comment, "Open various ports." You'll probably remove a couple of those lines if you're not running a Web server, and you'll probably need to add a whole lot more lines to let you do your thing. The last line logs unauthorized network traffic to /var/log/syslog. You'll want to keep on eye on this to see which ports you may wish to open, or to watch losers get snuffed by your firewall.
#! /bin/sh
# Local network configuration.


# Configure first Ethernet card connected to Internet as default gateway.
ifconfig eth0 $IP				> /dev/null 2>&1
route add default gw

# Set up IP forwarding.
ipchains -F forward
ipchains -P forward DENY
echo "1" > /proc/sys/net/ipv4/ip_forward

# Configure second Ethernet on private network. Forward and masquerade
# packets going to Internet.
ifconfig eth1			> /dev/null 2>&1
ipchains -A forward -j MASQ -s -d

# Firewalling.
ipchains -F input
ipchains -P input REJECT

# ICMP and local packets cool.
ipchains -A input -p icmp -j ACCEPT
ipchains -A input -i lo -j ACCEPT

# As are local networks.
ipchains -A input -i ppp0 -j ACCEPT
ipchains -A input -i eth1 -j ACCEPT

# Open various ports.
ipchains -A input -p tcp -d $IP www -j ACCEPT
ipchains -A input -p udp -d $IP www -j ACCEPT
ipchains -A input -p tcp -s www -d $IP 1024: -j ACCEPT
ipchains -A input -p tcp -s https -d $IP 1024: -j ACCEPT

# RealAudio input stream (see RealNetworks RealSystem Firewall Support).
ipchains -A input -p udp -s 1024: -d $IP 6970:7170 -j ACCEPT

# Log anything that fell through.
ipchains -A input -d $IP -l

by-nc-nd Best Viewed With YOUR Browser Valid HTML 4.01! Valid CSS! Powered by Debian GNU/Linux
Free DNS