Adsense Leaderboard Ad

10.24.2012

How to Configure a Static IP Address in Red Hat, CentOS and Fedora

Question sent in by Demarco from New York:


Q: I have come across a few how-to article on setting a static IP address but I always seem to get stuck.  Can you tell me simply how to configure CentOS or Red Hat for a static IP address.  I am currently using CentOS 5. 

A: Sure Demarco, this is a simple task once you understand the files and changes involved.  Let's start...

You will need the following information in order to complete this configuration:

    IP Address
    Gateway Address
    Broadcast Address
    Netmask
    Network Address
    DNS Server Addresses
    System Hostname    

The first thing I like to do is to turn off NetworkManager.  This service is responsible for requesting DHCP addresses and configuring the network interfaces. Since we are setting them statically we do not need it.

First let's stop NetworkManager:
service NetworkManager stop
or /etc/init.d/NetworkManager stop

Now let's make sure it does not start at boot:
chkconfig NetworkManager off

Let's also make sure the network service is set to start at boot:
chkconfig network on

Now that NM (NetworkManager) is out of the way we need to edit three files. 

/etc/sysconfig/network

/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/resolv.conf  

First let's edit /etc/sysconfig/network.  Here w e will need to tell the system to turn on networking, the hostname of the machine and the gateway.  Open the file in your favorite text editor and add or change the following lines. Of course you will need to make sure the configuration matches your system, this is just an example.
NETWORKING=yes
HOSTNAME=server.domain.com
GATEWAY=192.168.1.1

Now save and close that file.  Let's move on to editing the /etc/sysconfig/network-scripts/ifcfg-eth0 file.  Open the file in your favorite text editor and add or change the following lines.
DEVICE=eth0
BOOTPROTO=static
BROADCAST=192.168.1.255
IPADDR=192.168.1.10
NETMASK=255.255.255.0
NETWORK=192.168.1.0
ONBOOT=yes 

Now that the network interface is taken care of we need the final important piece of the puzzle, DNS.  Without DNS you will not be able to go to a website by name, on connect to anything else on the network unless you know the IP address.  To tell the system what DNS servers to use we edit the /etc/resolv.conf file.  Open the file in your favorite editor and add or change the following lines (search line is optional).
search domain.com
nameserver 192.168.1.2
nameserver 192.168.1.3

Now that we have given the system the necessary information all we need to do is restart the network service.
service network restart

That's it.  I hope this answers your question and helps you easily configure your system for a static IP address.  If you have any additional questions feel free to post them in the comments. 

12 comments:

  1. On my system it says /etc/resolv.conf is generated. So to configure /etc/resolv.conf, one must edit ifcfg-[device]. On my system device was em1 to em4 (see ifconfig output). So in one of the ifcfg-[device] files add the following:

    DNS1=192.168.1.2
    DNS2=192.168.1.3
    DOMAIN="domain1.com domain2.com"

    ReplyDelete
  2. Hi I have folllowed your instructions to the letter (with the exception of changing the interface name to match mine) and the interface seems very promising.

    [root@juniper devices]# ifconfig
    em1: flags=4163 mtu 1500
    inet 192.168.0.101 netmask 255.255.255.0 broadcast 192.168.0.255
    inet6 fe80::217:a4ff:fe99:f6da prefixlen 64 scopeid 0x20
    ether 00:17:a4:99:f6:da txqueuelen 1000 (Ethernet)
    RX packets 10241 bytes 9420657 (8.9 MiB)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 10546 bytes 1626849 (1.5 MiB)
    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    device interrupt 16

    As can be seen the interface has all the right properties.

    Now if I try and ping my router at 192.168.0.1 I get no response.

    I know the router exists because if I set the interface up to aquire a network address via DHCP the ping is replied to.

    [root@juniper devices]# ping 192.168.0.1
    PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
    64 bytes from 192.168.0.1: icmp_req=1 ttl=255 time=0.393 ms
    64 bytes from 192.168.0.1: icmp_req=2 ttl=255 time=0.394 ms
    64 bytes from 192.168.0.1: icmp_req=3 ttl=255 time=0.408 ms
    64 bytes from 192.168.0.1: icmp_req=4 ttl=255 time=0.406 ms

    I've Googled and found nothing to help me set a static IP address. Please help before I tear what's left of my hair out.

    ReplyDelete
  3. @Anyonymous - In order to help you I need to know the output of the command "route -n" while you have the interface statically configured. That will show you what your default gateway is (it should be your router 192.168.0.1).

    ReplyDelete
  4. alternatively u can disable the gateway settings on /etc/sysconfig/network file and configure them individually inside ifcfg-ethx file as
    GATEWAY=10.102.107.1 ( example )

    Also, set defroute parameter in it to 'no'

    This might help when u have multiple cards in the unix box !

    ReplyDelete
  5. For some reason nobody else seems to get accurate working steps on how to do this for Fedora 20, thanks!

    ReplyDelete
  6. how to know ip static or dynamic is there any unix command

    ReplyDelete
    Replies
    1. What flavor of Linux are you using?

      Delete