Wednesday, May 7, 2008

Configuring the gateway under Linux

I recently ran into a situation where I had to change my gateway. First, I wanted to check what my gateway was set to. The normal command 'ifconfig' only showed the following:

eth0 Link encap:Ethernet HWaddr 00:13:72:DF:1B:99
inet addr:192.9.200.37 Bcast:192.9.200.255 Mask:255.255.255.0
inet6 addr: fe80::213:72ff:fedf:1b99/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:10323194 errors:0 dropped:0 overruns:0 frame:0
TX packets:5229128 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1479472650 (1.3 GiB) TX bytes:2978700320 (2.7 GiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:12477530 errors:0 dropped:0 overruns:0 frame:0
TX packets:12477530 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3343381852 (3.1 GiB) TX bytes:3343381852 (3.1 GiB)


but not the gateway. Some interweb postings suggested using tracert, but I think you should use the right tool for the right job. The route command is that tool.

Running route alone will report the current gateway on the "default" line:

$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.9.200.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1000 0 0 eth0
default 192.9.200.38 0.0.0.0 UG 0 0 0 eth0


The 192.9.200.38 is the configured default gateway. To change it, first add the new default gateway with the command

$ route add default gw 192.9.200.40

Next, remove the old gateway

$ route del default gw 192.9.200.38

Verify your results:

$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.9.200.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1000 0 0 eth0
default 192.9.200.40 0.0.0.0 UG 0 0 0 eth0



I'll need to do some testing to determine the relationship of the route command to the /etc/network/interfaces file, but these settings seem to take priority.

But in case you need to tweak the named file...


sudo vi /etc/network/interfaces
sudo /etc/init.d/networking restart

No comments: