Tuesday, November 29, 2011

Static IP's in linux

If you're like me, you like centralized control of your configurations in Linux.  I'm not a big fan of letting gui tools manage things like my network settings and stuff. Especially on a server. So a friend of mine recently asked how to set a static IP for a box. So here's how I do it in the core config files for linux networking.

(Note that if you want to try static DHCP, you can do that with Netgear SOHO routers. I'm not sure about other brands (Belkin, Linksys, Dlink,etc. static DHCP is where the client still asks the DHCP server/router for an address but the DHCP server/router is configured to always give that client MAC address the same IP rather than whatever is first available out of the pool. It's a simple way to manage static IP's and lets your DHCP server/router still handle controling the other information including DNS servers. If you change DNS servers, for example, then you only need to change the info in the DHCP server/router.)

First you need to understand that if you are using DHCP to get info and change to static, you'll need more than just an IP configured. You'll also need other info that the DHCP server gives out when you lease an IP address: netmask, gateway, and DNS servers. I'm only going to discuss IP4 here since most SOHO applications will still be using that for a while. I use Ubuntu/Debian so some of the locations here may be specific to that distribution however, there are similar files in RedHat/CentOS that are configured the same.

The files I want to modify are located in /etc and all need to be modified as root. I recommend doing this from terminal since if you restart the box and it doesn't come up, you'll

First lets setup our IP address, gateway, and netmask.

Use vi, nano, emacs or whatever to edit the file /etc/networks/interfaces. You should at least have an eth0 interface. So lets set that up.

auto eth0
iface eth0 inet static
   address  10.0.0.5
   netmask  255.255.255.0
   gateway  10.0.0.1

So address will be whatever you want to give your box. IMPORTANT: Do not give it an address that is in your DCHP pool. This will cause big problems. Netmask is typically that unless you're doing something unique. Gateway is typically set to your router.

One additional thing we need to setup is the DNS info since we won't be getting it handed to us from the DHCP server. Look at your server and find out what the DNS server IPs should be. This could be a local DNS server if you have one on your network. It could be your internet provider's DNS servers. Or you can use openDNS servers. I prefer openDNS over internet providers. I actually run my own DNS caching server locally but it forwards out to openDNS servers. There are two basic IPs for openDNS server pools: 208.67.220.220 and 208.67.222.222. So I'll use those here to configure in my example.

We now want to edit /etc/resolv.conf to say the following:

nameserver 208.67.220.220
nameserver 208.67.222.222

It's important to at least list two nameservers in case one isn't reachable. You can use any number of servers and it will try them in the order listed. Therefore, you may combine your internet providers DNS servers with the open DNS servers and list them all if you like. If you run a local DNS list it first, then list an external DNS. While the local DNS should always respond and should forward out to another DNS on the internet for lookups it doesn't know, it's safe to have that backup one listed on your server too. That way if local DNS is down, your server still has a backup to ask.

With that, a restart of the network service or rebooting, and you should be back up with your static IP. I recommend rebooting the box if you can and testing. Never hurts to make sure that it'll work if it does end up rebooting. You don't want to have to troubleshoot that when you're in a crunch trying to bring a box back up after a power failure or something.

One additional note, if you have multiple nics, you may try bonding them. This works pretty easily with the current linux kernel, there are a bunch of resources on how to do this and I may right this up in the future for what I did. You can bond them in a round robbin way that will provide redundancy and some network boost. I have not been successful at bonding wireless nics. (whether bonding wireless to wired or wireless to wireless) I've read that due to the way WPA/WPA2 encryption works that it's likely impossible. If you decided to try to bond wireless, be prepared for a long path with many hours of trial and error and lots of internet research. But if you succeed, please post a tutorial somewhere for those of us who weren't successful.