David Steven-Jennings
Linux, Coding, Webmastery

Set up Iptables on Ubuntu

May 7th, 2008 by David

Ubuntu, like any good Linux flavour, comes with Iptables, which is used to filter, route and NAT network traffic. Unfortunately, Ubuntu doesn’t comes with a way to quickly modify the Iptables rules, like you do with other flavours such as Fedora. This how-to will show you how to set up a script to allow you to edit a single file to change your routing/firewall settings. Don’t worry, it’s pretty straight-forward.

What You’ll Need

  1. A computer with Ubuntu installed, either desktop or server edition

Step 1

Paste the following code into a new file called iptables in /etc/init.d/

Step 2

Run the following commands:

sudo chown root.root /etc/init.d/iptables
sudo chmod +x /etc/init.d/iptables

The first command changes the file owner to the superuser, root. The second line makes the file executable, telling the system that the file contains scripting which can be executed.

Step 3

This script reads the Iptables instructions from the /etc/iptables.conf file, so let’s create a copy of the current rule set:

sudo -s (you'll be prompted for your password to log in as root)
iptables-save > /etc/iptables.conf
exit

Now, all you need to do is edit the /etc/iptables.conf file to add/modify the rules. You can start, stop and restart the script by using the following command:

sudo /etc/init.d/iptables start/stop/restart

Obviously, you choose one of the options and leave out the /’s. Also, you’ll need to restart Iptables whenever you change the rules, in order to make the changes come into affect.

And that’s it. Now, modifying the routing, etc on your Ubuntu box is much easier :)

Posted in How To's - Linux

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.