Linux Static IP Configuration

Network configurations are either DHCP or statically assigned. Along with IP addresses, a network configuration requires DNS resolvers and routing information. This guide details how to set up a static IP configuration, and assumes you are configuring eth0.

Error:If you are modifying an existing network configuration, please ensure you have out-of-band access (i.e. via IPMI for a dedicated server or via the control panel for a VPS) in case you lose access to the system.

DNS Resolver Settings

Nameservers are defined in /etc/resolv.conf.

nameserver 4.2.2.1
nameserver 4.2.2.2
nameserver 8.8.8.8
nameserver 8.8.4.4

Static IP Configuration

CentOS 5 & 6

Network interfaces are configured in the files located in /etc/sysconfig/network-scripts/. For each network interface (eth0, eth1, etc), there should be a corresponding configuration file (e.g. ifcfg-eth0) in that directory.

DEVICE=eth0
BOOTPROTO=static
HWADDR=00:33:2D:T5:V1:22
IPADDR=1.2.3.4
NETMASK=255.255.255.0
GATEWAY=1.2.3.1
ONBOOT=yes
TYPE=Ethernet

Additional IPs (aliases)

Aliases should be configured within a file named /etc/sysconfig/network-scripts/ifcfg-eth0:<X>, for example ifcfg-eth0:1.

 You do not need to specify a gateway, netmask or hardware (MAC) address for aliases.

DEVICE=eth0:1
BOOTPROTO=static
IPADDR=1.2.3.4
ONBOOT=yes
TYPE=Ethernet

CentOS 7 & Fedora 22+

The primary IP and aliases are specified in the same file.

GATEWAY=1.2.3.4

# Your primary public IP address.
# The netmask is derived from the PREFIX.
IPADDR0=1.2.3.4
PREFIX0=&amp;quot;24&amp;quot;

# To add an IP alias:
IPADDR1=2.3.4.5
PREFIX1=&amp;quot;24&amp;quot;

Debian & Ubuntu

The primary IP and aliases are specified in the same file.

# Your primary public IP address.
iface eth0 inet static
address 1.2.3.4/24
gateway 1.2.3.4

# To add a second public IP address:
iface eth0:0 inet static
address 2.3.4.5/24

Gentoo

Networking in Gentoo uses the netifrc utility. Addresses are specified in /etc/conf.d/net file, on the config_eth0 line, separated by spaces.

config_eth0=&amp;quot;198.51.100.5/24 198.51.100.10/24 192.0.2.6/17&amp;quot;
routes_eth0=&amp;quot;default gw 198.51.100.1&amp;quot;

OpenSUSE

1. Edit the interface’s configuration file:


# Your primary public IP address.
IPADDR='1.2.3.4'
NETMASK='255.255.255.0'
GATEWAY=&amp;quot;1.2.3.1&amp;quot;

# Add a second public IP address:
IPADDR1='2.3.4.5'
NETMASK1='255.255.255.0'
LABEL1='1'

2. Add your gateway to the network routes file at /etc/sysconfig/network/routes:

# Destination Gateway Netmask Device
default 1.2.3.1 255.255.255.0 eth0

Read More