Wednesday, January 19, 2011

Setting up default routes and nameservers on Ubuntu 10.x

Usually I set up dns entries using the network connection gui but somehow I found myself needing to manually set it up. Also, there are certain routes that I want to be added by default on the kernel routing table. Now it's been ages since I tried to do this and it surprised me how a lot of things have changed. Learning is really a continuous and painful but fun process :)

First I needed to setup my default routes; after googling and reading man pages [1], I found out that you can add scripts that can be ran during bringing up/down of an interface. So what I did was I added a post-up command on the interface that needed static routes. Now the scripts that are run are stored at /etc/network/[event], where [event] can be if-down.d, if-post-down.d, if-up.d, if-post-up.d. I named my script routes.sh; below is a sample interface entry:

# The primary network interface
auto eth1
iface eth1 inet static
address x.x.x.x
netmask x.x.x.x
network x.x.x.x
broadcast x.x.x.x
gateway x.x.x.x
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers x.x.x.x x.x.x.x
dns-search artesyncp.com emrsn.org
# Static routes
post-up /etc/network/if-up.d/routes.sh

routes.sh:
#!/bin/bash

ip route flush dev eth1
ip route add to x.x.x.x/24 dev eth1
ip route add to x.x.x.x/16 via x.x.x.x dev eth1
ip route add to x.x.x.x/16 via x.x.x.x dev eth1

Of course x.x.x.x should be replaced by a valid IP address.

The problem was there is also an interface that retrieves its address through a DHCP server but the DNS entries that it sets are not complete. I can't touch the DHCP server and /etc/resolv.conf always gets overwritten during network reboots. Because of this, I needed to add option modifiers to my /etc/dhcp3/dhclient.conf. Below are what i added:

prepend domain-search "something.com", "another.something.com";
prepend domain-name-servers x.x.x.x, x.x.x.x;

I used prepend because I want to use my static name servers before the one that the DHCP server gives.

References

[1] man 5 interfaces
[2] man 5 dhclient.conf
[3] man 5 dhcp-options

No comments: