Add a static IP to a Redhat/Fedora/CentOS box

  • by Tim White
  • posted 2010.08.12
  • 1 Comments
  • 1609 views
iPhone on T-Mobile

ifcfg-eth0

Need to permanently assign a static IP address to a Linux box? This tutorial describes how to add any number of IP addresses to a network interface from the console.

Login as root and change directory to /etc/sysconfig/network-scripts and list the configurations of your network interface. In this example, we're assuming the network interface is eth0.

# cd /etc/sysconfig/network-scripts
# ls -l ifcfg-eth0*
-rw-r--r-- 1 root root 38 Dec  8  2009 ifcfg-eth0

Take note of the files listed. In the above example, eth0 only has one config file, but there could be many.

# cd /etc/sysconfig/network-scripts
# ls -l ifcfg-eth0*
-rw-r--r-- 5 root root 235 Sep 29  2009 ifcfg-eth0
-rw-r--r-- 1 root root 237 Sep 29  2009 ifcfg-eth0:1
-rw-r--r-- 1 root root 237 Sep 29  2009 ifcfg-eth0:2
-rw-r--r-- 1 root root 237 Sep 29  2009 ifcfg-eth0:3

Make a copy of the ifcfg-eth0 file and name the new file ifcfg-eth0:X where X is the next increment of the file names. For example, if there was only a single ifcfg-eth0 file, the new file should be named ifcfg-eth0:1. If the last config file was ifcfg-eth0:3, name the new file ifcfg-eth0:4.

cp ifcfg-eth0 ifcfg-eth0:1

Edit the new file and change/add the settings below. The DEVICE, NETMASK, IPADDR, GATEWAY settings must be set to your specific values. For example:

# DEVICE should be the network interface name and increment
DEVICE=eth0:1
IPADDR=192.168.0.102
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
ONBOOT=yes
BOOTPROTO=none

Repeat these steps for each IP address you need to add. For each static IP address, create a new network interface config file named with the ":X" increment, and similarly set the DEVICE directive.

If you're not using DHCP at all, you may need to add/edit your DNS servers. Open up /etc/resolv.conf and make sure the IP's of your DNS servers are in there. For example:

nameserver 192.168.0.1
nameserver 192.168.0.2

Finally, restart the network interfaces.

/etc/init.d/network restart

If you're SSH'd into the box, don't log out yet. Log into the box with a new SSH session to verify the network interface came back up. Once verified, try pinging the new IP from a different box.

# ping 192.168.0.102
PING 192.168.0.102 (192.168.0.102) 56(84) bytes of data.
64 bytes from 192.168.0.102: icmp_seq=1 ttl=64 time=0.051 ms
64 bytes from 192.168.0.102: icmp_seq=2 ttl=64 time=0.061 ms
64 bytes from 192.168.0.102: icmp_seq=3 ttl=64 time=0.068 ms

Comments

  1. Bugsy
    2010.12.05

    Great, simple writeup!

Leave a Comment