Add static IP Addresses Linux Arch CentOS Ubuntu Fedora OpenSUSE

Arch

Add the following addressing to the interface’s configuration.

/etc/systemd/network/05-eth0.network
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Match]
Name=eth0

[Network]
Gateway=198.51.100.1

# Your primary public IP address
Address=198.51.100.2/24

# To add a second public IP address:
Address=198.51.100.3/24

#To add a private IP address:
Address=192.168.133.234/17

Static IP addresses can be configured multiple ways in Arch. See the static Ip address section of Arch’s Network Configuration Wiki page for other options such as using Netctl. Additionally, you can configure dns several other ways without modifying resolv.conf.

 

CentOS 7 / Fedora 22+

The default ethernet interface file is located at /etc/sysconfig/network-scripts/ifcfg-eth0. You can configure a static IP address by adding or editing the following lines, substituting your own Linode’s IP addresses, gateways, and DNS resolvers:

/etc/sysconfig/network-scripts/ifcfg-eth0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
DEVICE="eth0"
BOOTPROTO="static"
ONBOOT="yes"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
NM_CONTROLLED="no"
PEERDNS="no"

GATEWAY=198.51.100.1
    
# Your primary public IP address
# The netmask is taken from the PREFIX (where 24 is a public IP, 17 is a private IP)
IPADDR0=198.51.100.5
PREFIX0="24"

# To add a second public IP address:
IPADDR1=198.51.100.10
PREFIX1="24"
    
# To add a private IP address:
IPADDR2=192.0.2.6
PREFIX2="17"

DOMAIN=members.linode.com
    
DNS1=203.0.113.1
DNS2=203.0.113.2
DNS3=203.0.113.3

To load your changes, restart the network service:

1
sudo systemctl restart network

CentOS 7 and recent versions of Fedora also include NetworkManager, which uses tools such as nmtui and nmcli to modify and create network configuration files. The above method to make the necessary modifications, however, is more straightforward.

 

CentOS 6

Like in CentOS 7, simply edit the ethernet interface file to configure a static IP address:

/etc/sysconfig/network-scripts/ifcfg-eth0
1
2
3
4
5
6
7
8
9
10
11
BOOTPROTO=none
PEERDNS=no
    
# Your primary static public IP address.
IPADDR0=198.51.100.5
PREFIX0=24
GATEWAY=198.51.100.1
DOMAIN=members.linode.com
DNS1=203.0.113.1
DNS2=203.0.113.2
DNS3=203.0.113.3

To add the resolv.conf option to rotate DNS providers, create a dhclient script:

/etc/dhcp/dhclient.d/rotate.sh
1
2
3
4
5
6
7
rotate_config() {
    echo "options rotate" >> /etc/resolv.conf
}

rotate_restore() {
    :
}

For multiple static IP addresses, additional IPs are assigned to an alias you create for eth0. To use this alias, an additional file must be created. For example, an eth0:1 file must be created for the eth0:1 interface alias, eth0:2 for eth0:2, etc.

/etc/sysconfig/network-scripts/ifcfg-eth0:1
1
2
3
# Add a second static public IP address.
DEVICE=eth0:1
IPADDR1=198.51.100.10

To put any changes into effect, restart your networking service:

1
sudo service network restart

For more information on the options available to interface files, see man ifcfg and Fedora’s documentation and the file /usr/share/doc/initscripts-*/sysconfig.txt on CentOS and Fedora installations.

 

Debian / Ubuntu

Add the following to the interface’s configuration file:

/etc/network/interfaces
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
. . .

# Your primary public IP address.
auto eth0
iface eth0 inet static
    address 198.51.100.5/24
    gateway 198.51.100.1
    dns-nameservers 203.0.113.1 203.0.113.2 203.0.113.3
    dns-search members.linode.com
    dns-options rotate

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

# To add a private IP address:
iface eth0 inet static
    address 192.0.2.6/17

Note that static and dynamic addressing cannot be combined in Debian and Ubuntu systems. In order to statically configure additional IP addresses, you must also statically configure your default IP address.

Gentoo

Networking in Gentoo utilizes the netifrc utility. Addresses are specified in the config_eth0 line and separated by spaces. The gateway is defined in the routes_eth0 line.

/etc/conf.d/net
1
2
3
config_eth0="198.51.100.5/24 198.51.100.10/24 192.0.2.6/17"
routes_eth0="default via 198.51.100.1"
. . .

OpenSUSE

  1. Edit the interface’s config file with:
    /etc/sysconfig/network/ifcfg-eth0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    BOOTPROTO=static
          
    . . .
    
    # Your primary public IP address.
    IPADDR=198.51.100.5/24
    GATEWAY=198.51.100.1
    
    # Add a second public IP address:
    IPADDR1=198.51.100.10/24
    LABEL1=1
    
    # Add a private IP address:
    IPADDR2=192.0.2.6/17
    LABEL2=2
    
  2. You will also need to add your gateway to the network routes file:
    /etc/sysconfig/network/routes
    1
    2
    # Destination   Gateway                 Netmask                 Device
    default         198.51.100.1            -                       eth0
    
  3. Edit each line to add DNS and domain information for netconfig. Netconfig uses this info to modify resolv.conf:
    /etc/sysconfig/network/config
    1
    2
    3
    4
    5
    6
    . . .
    NETCONFIG_DNS_STATIC_SERVERS="203.0.113.1 203.0.113.2 203.0.113.3"
    . . .
    NETCONFIG_DNS_STATIC_SEARCHLIST="members.linode.com"
    . . .
    NETCONFIG_DNS_RESOLVER_OPTIONS="rotate"