I’ve used Linux regularly for more than ten years. For the majority of that time, I’ve used Ubuntu, although I’ve also dabbled in Fedora (without much success) and CentOS (with marginally more).
I’m generally comfortable with Linux and when I’m not, I usually know where to look. However, if there’s one thing I always seem to have issues with on Linux, it’s networking.
When I first used Ubuntu, it was often a struggle to get any usable networking. After a while, it was just wireless (mainly WPA) which caused an issue. Latterly, the main issue I’ve encountered is networking with virtual machines (and most of those issues have been with VirtualBox – VMware doesn’t seem to encounter the same issues).
If you’re working with a server, you’ll want to set a static IP address. Once working, this makes things much easier than using a dynamic address.
If you use Ubuntu Desktop, you can edit these settings very easily from your control panel, but on Ubuntu Server you’ll need to use the command line.
In Ubuntu, your network settings are stored in /etc/network/interfaces
. To edit the file, enter nano /etc/network/interfaces
in a command line (or, if you have a different text editor, like vi, use that instead of nano). This will open the file in a text editor. A sample file might look like this (comments removed):
auto lo iface lo inet loopback auto eth0 iface eth0 inet dhcp
So, what does that mean? Well, there are two interfaces. The first is the loopback
interface. Leave that be – if you mess with it, things will break. Ubuntu uses that to communicate with itself.
The second is the eth0
interface. This is your network card. You may have more than one; if you have a second, it might be labelled eth1
. Your network card have a completely different designation – on my VMware virtual machine, it’s labelled ens33
).
The second line in each of those blocks sets those interfaces. The first lines (auto lo
and auto eth0
) tell Ubuntu to start those interfaces on boot. If eth0
wasn’t started on boot, the machine wouldn’t have any networking outwith the machine, which will cause you problems!
In the example above, and by default, Ubuntu uses DHCP, so the instance will allow your DHCP server (usually your router) to give the machine an IP address and set any other settings with it.
So, how do you set a static IP address? Well, it goes something like this:
auto eth0 iface eth0 inet static address 192.168.0.10 netmask 255.255.255.0 network 192.168.1.0 gateway 192.168.1.254 dns-nameservers 8.8.8.8 8.8.4.4
Line-by-line, that configuration does the following:
- Sets Ubuntu to configure the
eth0
interface on boot. - Sets the interface as a static interface (rather than DHCP).
- Gives the interface an address (in this case, 192.168.0.10).
- Sets a 24-bit subnet mask (so the network runs 192.168.0.1 – 192.168.0.255), and the network in the line underneath.
- Sets the gateway (router) address. The address displayed is the one used by BT routers – others may use 192.168.0.1.
- Sets the DNS nameservers to use. I’ve used Google nameservers in this example. You only need one for things to work, but it’s better to have at least two. You can mix them too – for better redundancy, you could use one Google namerserver and one OpenDNS nameserver.
A few quick notes:
- Once set, you need to reboot the server for the changes to take full effect. Stopping and starting the networking interface won’t cut it.
- If you forget to add the
auto eth0
line and networking doesn’t automatically come up on boot, runsudo ifup eth0
That will manually bring up the inferface. Remember to fix your configuration afterwards! - If you don’t set your DNS nameservers, you’ll still have network access, but you won’t be able to resolve domain names. Thus, you will be able to ping an IP address would work, but pinging an FQDN would not work. You’ll eventually notice if this doesn’t work, because things like updates will fail.
This should be everything you need to know to set up a static IP address in Ubuntu.