Disable DHCP

Would anyone know how to use nmcli to disable dhcp on RHEL7?
Is that even possible? If not, how can switch a network interface using dhcp to have a static IP adress?

  • post-author-pic
    Bob S
    12-22-2018

    To set a static IP address use:

     'nmcli connection add type ethernet con-name connection-name ifname interface-name ip4 address gw4 address

    EX: nmcli con add type ethernet con-name test-lab ifname ens9 ip4 10.10.10.10/24 gw4 10.10.10.254

    To set the DNS address use:

    EX: nmcli con mod test-lab ipv4.dns "8.8.8.8 8.8.4.4"

  • post-author-pic
    Tariq A
    12-23-2018


    Could you use the nmcli con-mod eth0 ipv4.manual disable or manual to setup the static and disable dhcp command?

    Redhat says only via the interactive editor.idk

  • post-author-pic
    Jake W
    12-24-2018

    In addition to Bob's answer, you can also directly edit the interface script at /etc/sysconfig/network-scripts/ -- Just make sure you know what you're doing if you modify it this way. You'll need to know, for instance, that IPADDR=[address] is the way you assign the IP within the file itself, in addition to other necessary values like the default gateway that correspond to those in the nmcli command Bob wrote out. You would set the BOOTPROTO= line to 'static'

  • post-author-pic
    umla-engineering
    12-24-2018

    I would create a new connection:

    # nmcli con add con-name eth0-static  ifname eth0 type ethernet autoconnect yes ip4 192.16.10.12/24 gw4 192.168.10.1

    Modify to add DNS... and use the ipv4.method manual to ensure static connection:

    # nmcli con mod eth0-static  ipv4.method manual ipv4.dns 192.168.10.254

    # nmcli con up eth0-static up

    You could also try to modify the existing dhcp connection to use the manual method, but I have not verified this:

    # nmcli con mod eth0-orig connection.autoconnect yes ipv4.method manual ipv4.addresses 192.168.10.12/24 ipv4.dns 192.168.10.254

    # nmcli con up eth0-orig

    However, I found that deleting the original dhcp connection and creating a new one works best.

    # nmcli con del eth0-orig

    Verify your work by checking the following files:
    /etc/resolv.conf 
             nameserver 192.168.10.254
    /etc/sysconfig/network-scripts/ifcfg-eth0-static
            BOOTPROTO=none
            TYPE=ethernet
             IPADDR=192.168.10.12
             PREFIX=24
             DNS1=192.168.10.254
             ONBOOT=yes

    The connection names and IP addresses used are just examples. Substitute what is applicable in your case. Hope this helps...

Looking For Team Training?

Learn More