Linux Basics - Static IP and Network Configuration on Debian Linux

Want to support HowtoForge? Become a subscriber!
 
Submitted by howtoforge (Contact Author) (Forums) on Tue, 2014-06-03 06:11. :: Debian | Linux

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

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

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

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

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

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

[....]

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

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.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Sat, 2014-12-13 17:58.
i tried to change my ip adress but i got some problems it's not working any more and i  made a backup before i have started as you said in the beiganing "mv /etc/network/interfaces /etc/network/interfaces.bak" now i want to return it back how i can do that ??
Submitted by Steven (not registered) on Fri, 2014-10-10 04:47.

FYI, Debian 7.x is reporting the following as deprecated;

/etc/init.d/networking restart

Submitted by TCB (not registered) on Fri, 2014-07-25 20:48.
Thanks for a nice write up.

On a beaglebone black rev C (Linux beaglebone 3.8.13-bone47 #1 SMP Fri Apr 11 01:36:09

UTC 2014 armv7l GNU/Linux) 

I had to add auto eth0 to get the static ip address to work, thus:

 # IPv4 address

auto eth0

iface eth0 inet static

        address 192.168.1.90

        netmask 255.255.255.0

        network 192.168.1.0

        broadcast 192.168.0.255

        gateway 192.168.1.1


Submitted by Anonymous (not registered) on Tue, 2014-06-03 18:46.

While what you have is a good start for a simple two or three node network... If you are running a larger static IP net, it's far better to deploy a DHCP server and define all static addresses there using "host" statements instead of locally per new machine.  I use the isc-dhcp-server available in the Debian repo.  It's incredibly easy to run.  There are just two configuration files:

/etc/dhcp/dhcpd.conf

/etc/dhcp/dhclient.conf

And there is plenty of documentation about how these two work. But here's a very basic dhcp.conf that includes a dynamic pool as well as a single static host...

subnet 192.168.25.160 netmask 255.255.255.240 {
    pool {range 192.168.25.161 192.168.25.174; }
}

host workstation1 {
    hardware ethernet a4:6b:14:5e:4c:8a;
    fixed-address 192.168.2.45;
}

Of course, if you need to have pool addresses "talk" directly to static addresses, you can leave everything in one big class C --- which just changes the first line to:

 subnet 192.168.25.0 netmask 255.255.255.0 {

A few of the big advantages to deploying a dhcp server for a static network is the ability to not worry about local settings at all.  You only need to define the nameservers a single time in the dhcp server... and subnetting is much easier to keep track of...

That's on the address side - things on the DNS side are a lot more tricky... and I would stick with adding host names manually or not at all.  If you really do need local network host name lookup and the network is more than 5 or six hosts, you're probably better off installing a local only DNS.  It's important to point out that in order for manually defined host entries to work - every node needs to include definitions for every other node.  This gets very tedious and error prone after just a few hosts.