Monday 3 December 2012

Static ip address for a ubuntu machine behind a router with dynamic ip assignment

Q. Why do we need it?
A. Speaking of a home business/non-critical dev environment here. When one keeps several servers behind a router and has the router do the port forwarding depending on the ports/port ranges, there's a need to have those servers keep the same ip addresses. Otherwise, upon a reboot of the router/cable modem, all the forwarding rules might be off and will need manual adjustments.

There are several ways to achieve this goal. One way that is OS independent would be to fiddle with the settings of your router and see if it can assign a static ip when it sees the specified mac address (an individual number than computers/iphones etc have when connecting to a network).

I would like to describe a Linux-specific way. I'm using Ubuntu (Debian), but most linux flavors will have the same/very similar way of doing it.

Before you make these changes, I'd like to warn you that in case of problems you'll lose connection to your server. So, ideally you have a physical access to the server so you can log into it other than via ssh. Also, make a copy of your current /etc/network/interfaces file before you proceed.

In the following, the $ sign connotes the linux prompt.

- Look up your current ip
$ ifconfig
 In the output produced, find lines:
inet addr:192.168.101.35 Bcast:192.168.101.255  Mask:255.255.255.0
The first ip address you see above is what's currently assigned to you by the router and will likely be different in your case.
If your network configuration is more complicated (in case you have some virtual networking such as vmware installed, 'inet addr' might be called something else).
The 192.168.101.255 above is the broadcast ip. Yours will likely be different. Mark it down.

- Let's find some ips specific to your network
$ route -n
 gives:
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.101.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
0.0.0.0         192.168.101.1   0.0.0.0         UG    100    0        0 eth0

The 192.168.101.0 above is the network ip. Yours will likely be different.
255.255.255.0 is netmask. Yours will likely be the same.
192.168.101.1 is the gateway. Yours will likely be different.
Mark all of them down.

Now, I would like my address to always be: 192.168.101.105 (it's the last portion of the ip address that we have control over). I chose 105 because it's high enough not to conflict with the dynamic ips being handed out by your router. I believe you can go as high as 200.
As a 'sudo' edit your /etc/network/interfaces with an editor of choice (I use 'vim'):

$ sudo vim /etc/network/interfaces

and put the following lines (replacing the values in red with your specific values):

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
 address 192.168.101.105
 netmask 255.255.255.0
 network 192.168.101.0
 broadcast 192.168.101.255
 gateway 192.168.101.1

save the file and restart networking:
$ sudo /etc/init.d/networking restart



At this point you'll have to relogin via ssh using the new ip you've specified. And if all worked well, you'll be able to do it.
$ ssh yourlogin@192.168.101.105 (use the ip you specified)

Or, if a terminal access is available to you
$ ifconfig
and see that your ip is what you've provided.

If no luck, using the terminal access, copy the original networking file over the changed one and restart your networking again.
$ sudo /etc/init.d/networking restart

Google your way to success.