Purpose: Most of the users, whether at home or at office, have some sort of DHCP server running which assigns IP addresses to the computer automatically. But sometime there are instances where you need to have your own static IP address. In this post we will how to assign a static IP address to your Debian system through command line.

Scenario: I am going to assume the following scenario for this post. We have two boxes running Debian Linux and we would like to connect them both using a crossover CAT 5/CAT 5e Ethernet cable such that both the boxes are isolated.

Step 1: Connect the two boxes

First step is to connect both the boxes with a crossover Ethernet cable. We will not go into details as to why do we need a crossover cable. You can search for it on the Internet. There are plenty of resources available.

Step 2: Assign static IP address

There are many ways by which you can assign a static IP address (through GNOME.KDE GUI, command line), but the following method (command line) works for sure every time. Edit the following file:

# nano /etc/network/interfaces

and you will have a line similar to this:

#The primary network interface
allow-hotplug eth0
iface eth0 inet dhcp

Now replace the above lines with the following:

#The primary network interface
allow-hotplug eth0
iface eth0 inet static
address 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1

Save and quit the file.

Note that different Linux distribution family will have different files to edit for assign static IP address. This method will work for all Debian and Ubuntu based distributions.

Step 3: Assign static IP address to other box


Similarly assign a static IP address to the other box also:

#The primary network interface
allow-hotplug eth0
iface eth0 inet static
address 192.168.0.2
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1

Step 4: Restart computers/services

Now you can either restart your networking services by the following command:

# /etc/init.d/networking restart

or to be 100% sure simply restart your computers:

# shutdown -r now

Step 5: Verify

Finally let’s verify if the computers got the newly assigned static IP address or not by simply giving the command:

# ifconfig eth0

Output:
eth0      Link encap:Ethernet  HWaddr 00:1f:d0:35:74:b5
inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::21f:d0ff:fe35:74b5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:370736239 errors:0 dropped:0 overruns:0 frame:0
TX packets:371567656 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:35563938219 (33.1 GiB)  TX bytes:135869251961 (126.5 GiB)
Interrupt:254 Base address:0xe000

That’s it! You can now ping to each other and do whatever you intend to do on your newly isolated networked computer.

Happy Static’IP’ing!

Be Sociable, Share!