CentOS 7 is not setting my gateway from DHCP

21,633

Solution 1

Make sure that the DHCP-server responds to ping. Otherwise, the /sbin/dhclient-script will not set the default route correctly.

This additional "check" seems to have been added to later versions of dhclient or specially added in CentOS 7. The ping-check does not exist in CentOS versions prior to 7.

Solution 2

I had this issue just now on a clean install of a CentOS 7 VMWare guest and it was due to me having setup a dhcp reservation prior for that guest in VMWare Workstation at:

C:\ProgramData\VMware\vmnetdhcp.conf

So the fix is to comment out or remove the two lines in the conf file relating to the CentOS server:

host VMnet8 {
    hardware ethernet 00:40:56:C0:00:08;
    fixed-address 192.168.80.1;
    #hardware ethernet 00:0C:29:19:C7:7A; <--Comment or remove this CentOS line
    #fixed-address 192.168.80.111;        <--Comment or remove this CentOS line
    option domain-name-servers 0.0.0.0;
    option domain-name "";
    option routers 0.0.0.0;
}

Save the file (as administrator), restart the VMWare DHCP service on the host, then do a network service restart on CentOS. You will now have a gateway to get out to the internet. However, you also need to yum update to fix the dhcp reservation no gateway issue. Otherwise, if you add the reservation back before that, it will have no gateway on the next restart. Once the updates are done, the issue should be resolved. Therefore, add the lines above back to the conf file and restart the VMWare DHCP service again so your reservation is back in place, and you're set.

This is only the case on the initial setup of CentOS. So, it's just painful that one time after a clean install. I'll be doing VMWare snapshots moving forward, despite the downside of that.

For the record, my NetworkManager is enabled. If anyone knows a permanent way to avoid this after a clean install, please share. It might get fixed in a later update of CentOS since yum update fixes it.

It's kind of specific situation, but not an uncommon setup. I mostly posted this answer because I'll probably be back here in the future trying to remember what I did. But, I killed some hours on this, so hopefully it helps someone else as well.

Solution 3

Maybe you have set a default-gateway in /etc/sysconfig/network/routes already?

If not, you can at least configure that what you route manually is done automatically in that file:

144.76.190.224 - 255.255.255.255 ens4
default 144.76.190.224 - ens4
Share:
21,633

Related videos on Youtube

Daniele Testa
Author by

Daniele Testa

Updated on September 18, 2022

Comments

  • Daniele Testa
    Daniele Testa over 1 year

    I have a Minimal Install of CentOS 7.

    I have disabled the Network Manager as I want to setup my network the "old school" way.

    systemctl stop NetworkManager 
    systemctl disable NetworkManager
    chkconfig network on
    service network restart
    

    My network config (/etc/sysconfig/network-scripts/ifcfg-ens4) looks like this:

    DEVICE="ens4"
    TYPE="Ethernet"
    NOZEROCONF="yes"
    PERSISTENT_DHCLIENT="1"
    BOOTPROTO="dhcp"
    DEFROUTE="yes"
    PEERROUTES="yes"
    IPV4_FAILURE_FATAL="no"
    NAME="ens4"
    ONBOOT="yes"
    NM_CONTROLLED="no"
    

    My DHCP lease-file looks like this:

    lease {
      interface "ens4";
      fixed-address 144.76.190.238;
      option subnet-mask 255.255.255.255;
      option routers 144.76.190.224;
      option dhcp-lease-time 86400;
      option dhcp-message-type 5;
      option domain-name-servers 8.8.8.8,8.8.4.4;
      option dhcp-server-identifier 144.76.190.224;
      option host-name "hello.example.com";
      option domain-name "example.com";
      renew 2 2014/10/21 05:44:47;
      rebind 2 2014/10/21 15:04:03;
      expire 2 2014/10/21 18:04:03;
    }
    

    Now, my problem is that the "routers" field from the DHCP seems to get ignored by CentOS 7. IP, netmask and hostname is being set correctly, but my default route is not being set (blank).

    As you can see, I am using netmask 255.255.255.255 so the gateway IP is "outside" of my network. Therefor an extra route is needed. If I run these manually:

    route add -host 144.76.190.224 dev ens4
    route add default gw 144.76.190.224
    

    Then everything works fine:

    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         144.76.190.224  0.0.0.0         UG    0      0        0 ens4
    144.76.190.224  0.0.0.0         255.255.255.255 UH    0      0        0 ens4
    

    All other linux distributions I have tested, including Centos 6, works fine and sets these 2 routes automatically from the DHCP.

    So my question is this; Why does it not getting set automatically in CentOS 7? Did something change and I have to add some additional flags to the DHCP client for it to work?

    It seems that CentOS 6 uses dhclient version 4.1.1-P1 and CentOS 7 uses 4.2.5. Maybe they changed something between those versions?

    UPDATE 1:

    I looked at the release notes of dhclient and found this for 4.0.0:

    "The dhclient-script was updated to create a host route for the default gateway if the supplied subnet mask for an IPv4 address was a /32. This allows the client to work in 'captive' network environments, where the operator does not want clients to crosstalk directly."

    So this should have been working since long time ago. Maybe CentOS 7 has removed it from the "dhclient-script"?

    UPDATE 2:

    I copied the file "/sbin/dhclient-script" from a CentOS 6 installation to the CentOS 7 server. Now everything works fine. I will investigate what changes they have done, but it seems they have introduced a bug in CentOS 7.

    UPDATE 3:

    I figured out the issue. They added a (stupid) ping check in the new version of /sbin/dhclient-script that tries to ping the gateway before adding it. As I am blocking pings in my firewall on the DHCP server, it did not answer and therefor the dhclient-script did not add any default route.

    • Michael Hampton
      Michael Hampton over 9 years
      Why are you trying to use DHCP? Hetzner wants you to set up the address statically.
    • Daniele Testa
      Daniele Testa over 9 years
      Not sure why you are mentioning Hetzner, but this is a VPS guest against the hosts dhcpd.
    • user9517
      user9517 over 9 years
      if it is a hetzner system you might find the answer to your issue in this document wiki.hetzner.de/index.php/Netzkonfiguration_CentOS/en.
    • Michael Hampton
      Michael Hampton over 9 years
      Don't block ICMP traffic unless you really know what you're doing. Things will break without obvious reason, and it will be even worse with IPv6.
    • devicenull
      devicenull over 7 years
      The CentOS dhclient-script appears to have been updated at some point, so it no longer relies on the gateway responding to pings. Most of the answers here are now obsolete.
  • Daniele Testa
    Daniele Testa over 9 years
    There is no such path. "/etc/sysconfig/network" is a file. I'm afraid that manually setting the routes is not an option for me. It needs to be set from the DHCP, just as it did in CentOS 6.x
  • Christian
    Christian over 9 years
    Sorry, it should be in /etc/sysconfig/network-scripts (I always confuse those)