How to: Ubuntu linux configure static IP address from command line

Recently I had to configure the network on a fresh installation of the Ubuntu Server operating system. I had never done this on the command line before so I’ve recorded the steps here:

Step 1: Check for a recognized network device

The Linux kernel assigns names to hardware devices so that we can use them. To check the alias for your computer’s wired/wireless network hardware, we run the command ip link. It returns something like this:

ip link

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

Lines 3 and 5 are the current hardware devices the operating system has recognized.

The device on Line 3 is called ‘lo’, and is a loopback device.

The device on Line 5 is called ‘eth0’, and is the ethernet adapter on the machine. Usually, ethernet adapters will have the prefix ‘eth’, and wireless adapters will have the prefix ‘wlan’. You can find more details here.

Now we know that we have a recognized network device, and it’s alias on the system (eth0).

Step 2: Collect information

In order to connect to a network through eth0 with a static ip, we must have a few pieces of information first:

  1. IP Address: The static address to use to identify your machine on the network
  2. Network Mask: The number defining the addressable range of ips on the network you are connecting to
  3. Gateway IP: The ip address of your default gateway
  4. Nameserver IP: The ip address of your dns nameserver

For this example, I will use junk values.

Step 3: Edit ‘/etc/network/interfaces’

Now we can add all the details about our new connection-to-be into the network interfaces configuration file so that it will automatically connect at boot. You will need root permissions.

In this example, we are using the command sudo vi /etc/network/interfaces and adding these lines to the file:

auto eth0
  iface eth0 inet static
  address: 10.0.0.13
  netmask: 255.255.255.0
  gateway: 10.0.0.1
  dns-nameserver 10.0.0.1
  • auto eth0 – Configures the service to create this connection at boot.
  • iface eth0 inet static – Indicates which network device to use, and the type of IP assignment (static vs DHCP)
  • address: 10.0.0.13 – Sets the static IP address
  • netmask: 255.255.255.0 – Sets the network mask address
  • gateway: 10.0.0.1 – Sets the default gateway address
  • dns-nameserver 10.0.0.1 – Sets the dns nameserver’s IP address. NOTE: More than one address can be specified (delimited with spaces)

Step 4: Reset and test the connection

Now that we’ve done all the configuration, we restart the network service with the command sudo service networking restart.

Then, do a simple ping test:

~> ping google.com
PING google.com (74.125.226.67) 56(84) bytes of data.
64 bytes from yyz06s07-in-f3.1e100.net (74.125.226.67): icmp_seq=1 ttl=53 time=2.39 ms
64 bytes from yyz06s07-in-f3.1e100.net (74.125.226.67): icmp_seq=2 ttl=53 time=2.05 ms

Victory!

Troubleshooting: What went wrong?

Here were the errors I ran into:

  1. Incorrect information – Make sure your network details are correct!
  2. Multiple networks – Mixing private and public network connections is tricky and can leave you with no internet access, even if one of the connections can reach the web.

    I’m still working on a solution to this one, and there will be a separate post for what I attempted. EDIT: Problem solved! See this post.

    Add suggestions in the comments!