Linux Basics - Static IP and Network Configuration in Debian Linux

This guide explains how to configure static IP address, dns configuration and hostname on debian based Linux distributions on the shell. It will be same in server & desktop.

 

1 Preliminary Note

Suppose you are working in a data center or company and your boss puts a dumb debian server setup and you need to configure it in the running environment. Yes it is little painstaking, but not very tough task. In my case I have a dumb debian server which was installed by someone in his networking environment and I want to make it functional in my static IP environment. Suppose I have a vacant IP 192.168.0.100 and I will implement it in my environment. My IP details are as follows:

IPv4

IP 192.168.0.100
subnet 255.255.255.0
gateway 192.168.0.1

IPv6

address 2001:db8::c0ca:1eaf
netmask 64
gateway 2001:db8::1ead:ed:beef

DNS

8.8.8.8
8.8.4.4

All the above values will be fitted as per your environment. It will differ in your case. The DNS Servers 8.8.8.8 and 8.8.4.4 are free public DNS servers from Google, you may use them on your server for free. I will use the editor nano in the examples Use your preferred text editor to edit the configuration files (e.g. nano, vi, joe etc.). If you use “nano” editor, type Ctrl+x to save changes.

2 Implementation

I will do manual configuration with root credentials of the network config file, which is the responsible for the IP information in my debian system. The file name is /etc/network/interfaces I will first make backup of my original file as /etc/network/interfaces.bak and then proceed for the changes /etc/network/interfaces

mv /etc/network/interfaces /etc/network/interfaces.bak

nano /etc/network/interfaces

I will change  the value like this

For IPv-6 You just need to add the entires below the segment as

nano /etc/network/interfaces

3 DNS configuration

DNS can be added in the file /etc/resolv.conf

nano /etc/resolv.conf

Note : DNS entries with the debian system will only works if resolvconf is not installed. If resolvconf is installed then you need to append the DNS entries in the file /etc/network/interfaces only as follows:

You can check whether resolvconf is installed or not by

dpkg -l | grep resolvconf

Note: DNS entries can be either enterd in /etc/network/interfaces or /etc/resolv.conf. There shouldn’t be double entries.

4 Hostname

In my case the hostname is server1.example.com to add the hostname use:

echo server1.example.com > /etc/hostname

Again add it here in

nano /etc/hosts

/etc/init.d/hostname.sh start

Check your hostname by using below code Now the value must be same for both cases

hostname
hostname -f

5 Advanced networking

I am using Debian Linux and I would like to  create alias for eth0 so that I can have multiple IP address. I will implemented by appending it as follows:

nano /etc/network/interfaces

Note: There will be no extra column for the Gateway.

Here I have done the IP aliasing for the IP 192.168.0.108, it could vary as per your requirement.

6 Services

After any change in the networking files you need to restart the network services as follows:

/etc/init.d/networking restart

After the service restart you can check the changes as:

ifconfig

The output will confirm the changes done statically. It will be almost similar like this:

root@server1:~# ifconfig
eth0      Link encap:Ethernet  HWaddr 20:89:84:c8:12:8a
inet addr:192.168.0.100  Bcast:192.168.0.255  Mask:255.255.255.0
inet6 addr: fe80::2289:84ff:fec8:128a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:200197 errors:0 dropped:67 overruns:0 frame:0
TX packets:69689 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:64103748 (64.1 MB)  TX bytes:14106191 (14.1 MB)
Interrupt:16

eth0:0    Link encap:Ethernet  HWaddr 20:89:84:c8:12:8a
inet addr:192.168.0.108  Bcast:192.168.0.255  Mask:255.255.255.0
inet6 addr: fe80::2289:84ff:fec8:128a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

Note Above values will differ in your case.