Configure Ubuntu system to connect network with static IP

February 13, 2021

Comments

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:
$ 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 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:

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.

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
Satish Kumar

Satish Kumar

I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.

Related Posts

How to add swap space on Ubuntu 21.04 Operating System

How to add swap space on Ubuntu 21.04 Operating System

The swap space is a unique space on the disk that is used by the system when Physical RAM is full. When a Linux machine runout the RAM it use swap space to move inactive pages from RAM. Swap space can be created into Linux system in two ways, one we can create a...

How to Install Nginx on Ubuntu 21.04 Server

How to Install Nginx on Ubuntu 21.04 Server

Nginx is the most potent, open-source, and a high-performance Web server. It can work as a reverse proxy server also, nowadays, is used by most of the most significant websites on the internet. People pronounced “engine x” for Nginx; it is the hot choice for every...

0 Comments

0 Comments

Submit a Comment

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

seventeen − eight =

News & Updates

Join Our Newsletter