7

I want to configure my Linux so that it will be used as a network router (gateway). Can anybody give me some hints on this? (links are welcome!)

1

2 Answers 2

6

For a simple router, there are really only two steps that need to be done.

Enable routing

The first step is to enable routing in the kernel. By default, the kernel drops packets that it doesn't recognize; once you enable routing, it'll forward them. You need to issue either of these two commands when the computer boots:

sysctl -w net.ipv4.ip_forward=1
echo 1 >/proc/sys/net/ipv4/ip_forward

Many distributions have a file called /etc/sysctl.conf, where you can put the line net.ipv4.ip_forward=1 to execute that command when the computer boots. If there's a directory /etc/sysctl.d, you can add a file in that directory instead of editing /etc/sysctl.conf; call the file something.conf.

For IPv6, the corresponding setting is net.ipv6.conf.all.forwarding or /proc/sys/net/ipv6/conf/all/forwarding. You can also use net.ipv4.conf.all.forwarding or /proc/sys/net/ipv4/conf/all/forwarding for IPv4.

Set routing tables

The second step is to set routing tables. This can be simple or complicated depending on how much you need to do. For simple uses, configure each of your network interfaces' address and netmask, and add any needed extra route with the route command.

Going beyond simple routing

If you need to rewrite packets, the basic command is iptables (ip6tables for IPv6). (“Netfilter” is the name of the kernel packet handling facility, and “iptables” if the name of the program that controls it.) This is where to look for filtering, NAT and more.

For complex setups, look at the ip command from the iproute2 package.

2
  • Hi Gilles! I followed your thoughts. Something like this: eth0 - has the WAN. eth1 has 192.168.30.1 IP. After this, I done: iptables --table nat --append POSTROUTING --out-interface eth0 -j MASQUERADE and iptables --append FORWARD --in-interface eth1 -j ACCEPT. I set my client machine's IP to 192.168.30.10. My client machine is connected to the linux server via eth1. Ok, when I ping 192.168.30.1, it's ok, but I cannot connect to internet. (cannot ping google.com or any other web address). I also wrote my valid DNS in the /etc/resolv.conf. Can you help me on this? Thanks.
    – artaxerxe
    Jan 16, 2012 at 9:02
  • @artaxerxe I can't tell what's wrong from your comment. You should ask a new question. Can you ping 8.8.8.8? If yes, you have a DNS problem. If not, you have an IP connectivity problem; copy-paste the output of ifconfig, route -n, iptables -nvL and iptables -t nat -nvL. Jan 16, 2012 at 18:31
3

First plug in the IPs of the network for which you want to act as router. Either get multiple network interface cards or configure a virtual interface.
You need to enable packet forwarding from /etc/sysctl.conf and then configure iptables for NAT. Here is a brief tutorial for the same.

1
  • Hello Aditya. I followed your link, but I have issues with it. See the comment that I wrote to Gilles
    – artaxerxe
    Jan 16, 2012 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.