Linux Basics - Static IP and Network Configuration on 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 on server & desktop.

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.

Network configuration

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

auto lo
iface lo inet loopback


#My IP description
# IPv4 address
iface eth0 inet static
	address	192.168.0.100
	netmask	255.255.255.0	
	network	192.168.0.0	
	broadcast 192.168.0.255
	gateway	192.168.0.1

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

nano /etc/network/interfaces
[...]
#IPv6 address
iface eth0 inet6 static
        address 2001:db8::c0ca:1eaf
        netmask 64
        gateway 2001:db8::1ead:ed:beef

DNS configuration

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

nano /etc/resolv.conf
nameserver	8.8.8.8 
nameserver	8.8.4.4

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:

[....]

#DNS configurations
dns-nameservers	8.8.8.8
dns-nameservers 8.8.4.4

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.

Set or change the 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
[...]
127.0.0.1     localhost
192.168.0.1   server1.example.com server1
[...]
/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

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
[....]

#IP Aliasing
auto eth0:0
iface eth0:0 inet static
	name Ethernet alias LAN card
	address 192.168.0.108
	netmask 255.255.255.0
	broadcast 192.168.0.255
	network 192.168.0.0

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.

Restart Networking Service

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

service networking restart

On Debian 7, use the following command instead:

/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.

Share this page:

8 Comment(s)