How To Setup IP Address in Linux

Overview

In this guide, you will be shown how to setup the IP address of a computer or server running Linux. Before we know which way to configure your network card, you need to know if you want to use a static or dynamic IP address.

Setup IP Address

Setup IP Address

Static IP vs DCHP IP

Whats the difference? Static is an IP address that yet manually chose and set for your computer. It won’t change until you decide to update it. DHCP is an IP address that is given to your computer by a DHCP server (usually a router or another server). The DHCP assigned IP address is given to you from a pool (or range) of IP addresses and can change depending on which ones are available.

How To Setup IP Address in Linux

Depending on your network configuration, you will need to either use a static IP address which is commonly used on servers on corporate networks. A static IP address is manually entered in and doesn’t change.
You could also use a dynamic IP address which can be assigned to your computer via a DHCP service. DHCP is more common in home networks as it can be setup automatically and doesn’t require the user to know how to change an IP address. DHCP is a service that runs on your network that will automatically setup your network IP address. Before DHCP can assign your computer a IP address, you need to setup your network interface to use DHCP

Editing the Network Interface

All your IP address information be it dynamic or static is stored under your network interface file which can be found under the path /etc/network/interface. To edit this file you may need root or sudo access. To edit the file enter the following:

sudo vi /etc/network/interfaces

Inside this file you will notice a loop back device. Ignore this part as we want to focus on the eth0 interface – or interface 0. If you have multiple network interfaces, you may see eth0, eth1, etc.

Setup DHCP for Network Interface

Find the section that has iface eth0 (or whichever interface you need – eth1, eth2, etc). and change it to match below. That’s the only configuration you. Now you need to restart your network interface to bring in the changes (shown below).

allow-hotplug eth0
iface eth0 inet dhcp

Setup Static IP for Network Interface

If you need a static ip address, then edit your interface file to be similar to below. Note that your ip, netmask and other items will need to match your own network range.

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

Note that network and broadcast are optional entries and if omitted will be automatically detected.

Restart Networking Interfaces

In order for the above changes to come into effect, you need to restart your network interface. You can do this by running the following command:

/etc/init.d/networking restart

Sometimes you will find your interface doesn’t come online when restarting so entering in `ifup` will bring it back up where eth0 is the network interface you configured.

ifup eth0

And inversely, if you wanted to bring the network interface down, you would type in:

ifdown eth0

Caution! Typing in ifdown will take the network interface offline and drop your network connection so don’t running this command remotely!