24/7/365 Support

Connecting to a network with static IP in Ubuntu server

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 article, you will learn how to configure the network interface with static IP assignment.

Getting ready

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.

How to do it…

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

Get a list of available Ethernet interfaces using the following command:

$ ifconfig -a | grep eth

Open /etc/network/interfaces and find the following lines:

auto eth0

iface eth0 inet dhcp

Change the preceding lines to add an IP address, net mask, and default gateway (replace samples with the respective values):

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

Restart the network service for the changes to take effect:

$ sudo /etc/init.d/networking restart

Try to ping a remote host to test the network connection:

$ ping www.google.com

How it works…

In this recipe, 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:

ubuntu@ubuntu:~$ ifconfig -a | grep eth

eth0 Link encap:Ethernet HWaddr 08:00:27:bb:a6:03

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:

$ sudo ifup eth0

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

$ sudo ifdown eth0

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.

There's more…

The steps in this recipe 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

Help Category:

What Our Clients Say