nmcli commands for static IP networking in CentOS 7

8,796

Try the following::

nmcli con mod eno1 ipv4.addresses 12.345.67.8xx/29
nmcli con mod eno1 ipv4.gateway 12.34.567.8yy
nmcli con mod eno1 ipv4.dns "aa.aa.aa.aa bb.bb.bb.bb"
nmcli con mod eno1 ipv4.method manual
nmcli con mod eno1 connection.autoconnect yes

Reboot and it should work.

If you don't know the name of the network connection, you could run:

CON="$(nmcli -f NAME -m multiline show con | awk '{ print $2; }')"

the use "$CON" instead of eno1 in the commands above. For example:

nmcli con mod "$CON" ipv4.addresses 12.345.67.8xx/29

This only works if you have a single connection. If your VM has more than one network connection then you'll need to add some logic to the above to work out which one to use.

Share:
8,796

Related videos on Youtube

RabT
Author by

RabT

Updated on September 18, 2022

Comments

  • RabT
    RabT over 1 year

    What specific sequence of nmcli commands can be used to effectively configure static IP networking in CentOS 7?

    The four step process shown below DOES effectively configure static IP networking.

    But the process below relies on config files, which might not easily lend themselves to an automated scripted approach later. I am imagining lots of overly complicated scripting if we stay with the config file approach shown below. For example, the UUID line would require special handling in a script that simply copied in the config file and rebooted the machine.

    Instead, I would like a series of specific nmcli commands that can be entered into the terminal manually, and which also can make it easier to design a bash script later.

    Here is the current manual process, which works:

    1.) The device eno1 was created during an automated installation process.

    2.) Assign the IP address with the given netmask to eno1.

    ip addr 12.34.567.8xx/29 dev eno1 
    

    3.) Edit the config file:

    $ vi /etc/sysconfig/network-scripts/ifcfg-eno1
    
            TYPE="Ethernet"
            BOOTPROTO="static"
            DEFROUTE="yes"
            IPV4_FAILURE_FATAL="no"
            IPV6INIT="yes"
            IPV6_AUTOCONF="yes"
            IPV6_DEFROUTE="yes"
            IPV6_FAILURE_FATAL="no"
            NAME="eno1"
            UUID="some-very-long-complex-string"
            DEVICE="eno1"
            ONBOOT="yes"
            DNS1=aa.aa.aa.aa
            DNS2=aa.aa.bb.bb
            IPADDR=12.34.567.8xx
            NETMASK=255.255.255.248
            PREFIX=29
            GATEWAY=12.34.567.8yy
            PEERDNS=yes
            PEERROUTES=yes
            IPV6_PEERDNS=yes
            IPV6_PEERROUTES=yes
            NM_CONTROLLED="no"
    

    4.) Then save file and reboot the computer.

    Note that 12.34.567.8xx and 12.34.567.8yy are public IP addresses, and that 12.34.567.8__ indicates that the first 8 characters of the IP and gateway are identical.

    Also note that BOOTPROTO=static and NM_CONTROLLED=no need to end up in whatever results from the nmcli commands.

    What specific sequence of nmcli commands can effectively replace the above 4 step process?

    • thaller
      thaller about 7 years
      NetworkManager will never write an ifcfg-rh file with NM_CONTROLLED=no.
    • RabT
      RabT about 7 years
      @thaller Thank you and +1. This is why I read the documentation in addition to asking questions. Red Hat has many thousands of pages of documentation, so comments like yours make it easier to absorb the documentation while a person is still reading it.