This tutorial contains How to configure Linux static IP address on Ubuntu by editing /etc/network/interfaces configuration files. This tutorial contains configuration for IPv4 only !. This tutorial also works on Debian and other distribution that based on Debian distribution.
How to configure Linux static IP ?
By default Linux acquire their IP address, routing and DNS Information by using DHCP, but DHCP will only assign one IP to your interface so if you have multiple IPs you will need to set Linux static IP configuration manually or if your network doesn’t have DHCP server, then you will need to assign Linux static IP manually. So how to configure Linux static IP ? You need to update and edit the network configuration files in /etc/network/interfaces. Configure /etc/network/interfaces will not change your configuration if you restart your computer, so let’s get started.
In this tutorial I will use the following Internet Protocol Version 4 (TCP/IPv4) Properties, including the IP address, default gateway, DNS Servers, and hostname.
- IP address: 192.168.1.2
- Netmask: 255.255.255.0
- Hostname: hos1.opnux.com
- Domain name: opnux.com
- Gateway : 192.168.1.1
- DNS Server 1: 192.168.1.1
- DNS Server 2: 188.8.131.52
- DNS Server 3: 184.108.40.206
Setting Hostname and FQDN
Before we settings our Linux static IP, you will net to set your system’s hostname and FQDN. Please note your hostname must be unique. To change the hostname, use the following command and change “host1” to the hostname of your choice.
echo "host1" > /etc/hostname hostname -F /etc/hostname
Other method is you can change your hostname by editing hostname file directly, you can use any text editor of your choice.
$ sudo vim /etc/hostname
then change the default hostname to the hostname of your choice and save the file,
Update hosts file
Next step before setting your own Ubuntu/Debian linux static IP is edit editing your hosts file in /etc/hosts, just like before you can use any text editor to edit /etc/hosts
$ sudo vim /etc/hosts
Add a new line for your own ip address, domain, and hostname, change host1.opnux.com to your own FQDN, host1 for your hostname, and 192.168.1.2 for IP address.
127.0.0.1 localhost.localdomain localhost 192.168.1.2 host1.opnux.com host1
Linux static IP configuration
In this step we will edit and change some lines in /etc/network/interfaces, follow the example below to configure your Ubuntu/Debian linux static IP.
$ sudo vim /etc/network/interfaces
If you found like this :
iface eth0 inet dhcp
Your interface (in this case is eth0) is configured as dhcp client which acquired network configuration from the dhcp server.
To change it into a static, you will need to change those lines and add some lines and if you not found those lines just add this lines :
iface eth0 inet static address <IP Address> network <Network Address of Subnet> netmask <Subnet mask> broadcast <Broadcast Network of Subnet> gateway <IP Address to connect to other network>
As an example here is the screenshot.
If you noticed there is auto eth0, with this line the interface (eth0) will be brought up during bootup.
After configure your Linux static IP, now restart your network service to make changes by enter this following command :
$ sudo service network restart
Configure DNS Resolver
DNS is use to resolve DNS to Domain name, or reverse. To change DNS Settings you need to edit your /etc/resolv.conf file so it can resolve DNS from a DNS server. The search and domain lines are optional. In the example below we will enter Google DNS and a Local DNS from your gateway.
$ sudo vim /etc/resolv.conf
Add this line to your /etc/resolv.conf
domain google.com search google.com nameserver 192.168.1.1 #Local DNS nameserver 220.127.116.11 #Google DNS 1 nameserver 18.104.22.168 #Google DNS 2
Now save /etc/resolv.conf file.
Test your Linux Static IP
Everything is setup, this last step is to test your connection, before checking it works or not, check your interface ip address. To check your static ip configuration enter this command :
$ ifconfig eth0
If ip address is setup, now test ping to your gateway.
$ ping 192.168.1.2
If everything works, Congratulations you are successful configure your own Linux Static IP.
That’s all for my tutorial How to configure Linux static IP address on Debian / Ubuntu, I hope this tutorial helps you for configuring your Ubuntu/Debian Linux static IP Address, Thank You !