Linux Auto Ip Address Assignment

For temporary network configurations, you can use standard commands such as ip, ifconfig and route, which are also found on most other GNU/Linux operating systems. These commands allow you to configure settings which take effect immediately, however they are not persistent and will be lost after a reboot.

To temporarily configure an IP address, you can use the ifconfig command in the following manner. Just modify the IP address and subnet mask to match your network requirements.

sudo ifconfig eth0 10.0.0.100 netmask 255.255.255.0

To verify the IP address configuration of eth0, you can use the ifconfig command in the following manner.

ifconfig eth0eth0 Link encap:Ethernet HWaddr 00:15:c5:4a:16:5a inet addr:10.0.0.100 Bcast:10.0.0.255 Mask:255.255.255.0 inet6 addr: fe80::215:c5ff:fe4a:165a/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:466475604 errors:0 dropped:0 overruns:0 frame:0 TX packets:403172654 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2574778386 (2.5 GB) TX bytes:1618367329 (1.6 GB) Interrupt:16

To configure a default gateway, you can use the route command in the following manner. Modify the default gateway address to match your network requirements.

sudo route add default gw 10.0.0.1 eth0

To verify your default gateway configuration, you can use the route command in the following manner.

route -nKernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.0.0.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0 0.0.0.0 10.0.0.1 0.0.0.0 UG 0 0 0 eth0

If you require DNS for your temporary network configuration, you can add DNS server IP addresses in the file /etc/resolv.conf. In general, editing /etc/resolv.conf directly is not recommanded, but this is a temporary and non-persistent configuration. The example below shows how to enter two DNS servers to /etc/resolv.conf, which should be changed to servers appropriate for your network. A more lengthy description of the proper persistent way to do DNS client configuration is in a following section.

nameserver 8.8.8.8 nameserver 8.8.4.4

If you no longer need this configuration and wish to purge all IP configuration from an interface, you can use the ip command with the flush option as shown below.

Flushing the IP configuration using the ip command does not clear the contents of /etc/resolv.conf. You must remove or modify those entries manually, or re-boot which should also cause /etc/resolv.conf, which is actually now a symlink to /run/resolvconf/resolv.conf, to be re-written.

I know that the internet is full of articles teaching you how to configure your network interface with static or dynamic IP addresses, but I promise that this article is as short as it can be and very good.

I decided to make a simple and short guide on how to setup a static or dynamic IP adress on Debian based Systems, such as: Ubuntu, Debian, Knoppix, Damn Small Linux, Linux Mint, Sparky Linux, Elive, Descent OS, Solus OS, Snow Linux,Crunchbag, etc (trust me, distrowatch knows many others Debian/Ubuntu based Linux distributions)

To make network configurations, you have to open (as root or with sudo) the /etc/network/interfaces file a text editor.

Set a dynamic IP address:

This is how a network having an dynamic IP address (got from a dhcp server) is configured.

Explanations:

  • auto eth0 – enable at startup the eth0 interface
  • iface eth0 inet dhcp – consider that iface eth0 comes from interface eth0, inet tells you that the network configuration is IPv4 and dhcp that the dynamic ip is assigned by a dhcp server.

Set a static IP address

We have a little more work to do for configurationg a network interface with a static IP address. Edit your /etc/network/interfaces file again, so that it looks like this (these are my IP addresses, replace them with yours.) :

Explanation:

  • auto eth0 – enable at startup the eth0 interface
  • iface eth0 inet static– consider that iface eth0 comes from interface eth0, inet tells you that the network configuration is IPv4 and static that your network interface has static ip adresses.
  • address – the network’s IP address
  • netmask – the network’s mask address
  • network – the network’s address
  • broadcast – the broadcast address
  • gateway – the gateway address

It is not necesarry to insert the network and broadcast lines in the /etc/network/interfaces file because the system calculates them from the address and the netmask fields.

So, your network interface file could also look like this, for static IP configurations:

Set the network’s DNS server:

To set the DNS server address, you have to edit the /etc/resolv.conf file, with root priviledges:

The /etc/resolv.conf file, should look like this:

8.8.8.8 is Google’s private DNS address. You can replace 8.8.8.8 with another DNS address, but everything works perfect with the Google DNS.

Reboot the networking service:

All the configurations that are made in configuration files, are persistent. They don’t apply before you reboot the service, but they will not be discarded after reboot. This is how you reboot the network service, to apply the changes:

Share and Enjoy

Tagged with: linux, network configuration, network interfaces, set dynamic ip crunchbag, set dynamic ip debian, set dynamic ip knoppix, set dynamic ip linux mint, set dynamic ip solus os, set dynamic ip ubuntu, set static ip crunchbag, set static ip debian, set static ip knoppix, set static ip linux mint, set static ip solus os, set static ip ubuntu, unix
Posted in The Linux and Unix Articles!

0 comments

Leave a Reply

Your email address will not be published. Required fields are marked *