3

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?

  • 1
    NetworkManager will never write an ifcfg-rh file with NM_CONTROLLED=no. – thaller Mar 7 '17 at 8:56
  • @thaller Why not? – CodeMed Mar 7 '17 at 9:05
  • 1
    "NM_CONTROLLED=no" marks a device as unmanaged by NetworkManager and causes NM to ignore the ifcfg file. There is no API in NetworkManager to configure this flag, it's only interpreted if the admin writes it by hand. On the one hand it's a missing feature. But it also doesnt really make sense to have NM API for that (because usually you configure connections -- but this setting hides a connection -- or you configure devices, but this setting lives inside the ifcfg file, which is a connection to NM). If you want NM to ignore the file, write it by hand. – thaller Mar 7 '17 at 22:20
  • @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. – CodeMed Mar 7 '17 at 22:39
4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.