Configure Ubuntu system to connect network with static IP

When you install Ubuntu server, its network setting defaults to dynamic IP addressing, that is, the network management daemon in Ubuntu searches for a DHCP server on the connected network and configures the network with the IP address assigned by DHCP. Even when you start an instance in the cloud, the network is configured with dynamic addressing using the DHCP server setup by the cloud service provider. In this chapter, you will learn how to configure the network interface with static IP assignment.

Prerequisites

You will need an Ubuntu server with access to the root account or an account with sudo privileges. If network configuration is a new thing for you, then it is recommended to try this on a local or virtual machine.

Configure Static IP

Follow these steps to connect to the network with a static IP:

  • Get a list of available Ethernet interfaces using the following command:
<code>$ ifconfig -a | grep eth</code>
  • Open /etc/network/interfaces and find the following lines:
<code>auto eth0 iface eth0 inet dhcp</code>
  • Change the preceding lines to add an IP address, net mask, and default gateway (replace samples with the respective values):
<code>auto eth0 iface eth0 inet static address 192.168.1.100 netmask 255.255.255.0 gateway 192.168.1.1 dns-nameservers 192.168.1.45 192.168.1.46</code>
  • Restart the network service for the changes to take effect:
<code>$ sudo /etc/init.d/networking restart</code>
  • Try to ping a remote host to test the network connection:
<code>$ ping www.google.com</code>

How it works:

In this article, we have modified the network configuration from dynamic IP assignment to static assignment.

First, we got a list of all the available network interfaces with ifconfig -a. The -a option of ifconfig returns all the available network interfaces, even if they are disabled. With the help of the pipe (|) symbol, we have directed the output of ifconfig to the grep command. For now, we are interested with Ethernet ports only. The grep command will filter the received data and return only the lines that contain the eth character sequence:

<code>ubuntu@ubuntu:~$ ifconfig -a | grep eth eth0 Link encap:Ethernet HWaddr 08:00:27:bb:a6:03</code>

Here, eth0 means first Ethernet interface available on the server. After getting the name of the interface to configure, we will change the network settings for eth0 in interfaces file at /etc/network/interfaces. By default, eth0 is configured to query the DHCP server for an IP assignment. The eth0 line auto is used to automatically configure the eth0 interface at server startup. Without this line, you will need to enable the network interface after each reboot. You can enable the eth0 interface with the following command:

<code>$ sudo ifup eth0</code>

Similarly, to disable a network interface, use the following command:

<code>$ sudo ifdown eth0</code>

The second iface eth0 inet static line sets the network configuration to static assignment. After this line, we will add network settings, such as IP address, netmask, default gateway, and DNS servers.

After saving the changes, we need to restart the networking service for the changes to take effect. Alternatively, you can simply disable the network interface and enable it with ifdown and ifup commands.

The steps in this article are used to configure the network changes permanently. If you need to change your network parameters temporarily, you can use the ifconfig and route commands as follows:

  • Change the IP address and netmask, as follows:
$ sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
  • Set the default gateway:
$ sudo route add default gw 192.168.1.1 eth0
  • Edit /etc/resolv.conf to add temporary name servers (DNS):
nameserver 192.168.1.45 
nameserver 192.168.1.46
  • To verify the changes, use the following command:
$ ifconfig eth0 
$ route -n
  • When you no longer need this configuration, you can easily reset it with the following command:
$ ip addr flush eth0
  • Alternatively, you can reboot your server to reset the temporary configuration.

IPv6 configuration

You may need to configure your Ubuntu server for IPv6 IP address. Version six IP addresses use a 128-bit address space and include hexadecimal characters. They are different from simple version four IP addresses that use a 32-bit addressing space. Ubuntu supports IPv6 addressing and can be easily configured with either DHCP or a static address. The following is an example of static configuration for IPv6:

iface eth0 inet6 static 
address 2001:db8::xxxx:yyyy 
gateway your_ipv6_gateway

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

12 − four =

Related Articles