how do I use nmcli to add an IP-address to an interface?

6,485

Solution 1

Your conn modify looks mostly right, but need to put a netmask on the address otherwise it'll get the default (probably /8, which may or may not be what you want). For instance, if you needed a class-c network (254 hosts, 10.1.1.1 through 254 in your case), you'd:

nmcli con modify 'Wired connection 1' ipv4.addresses "10.1.1.2/24"

You also need to apply the config to the running interface. I usually:

nmcli conn down "Wired Connection 1"
nmcli conn up !$

You may be able to just use conn up with out taking the interface down first.

Other note: you can run "nmcli conn show "Wired Connection 1" to get all the settings for that device. At the end of that, it'll also show the current settings in all caps.

connection.id:                          virbr0
connection.stable-id:                   --
connection.type:                        bridge
connection.interface-name:              virbr0

[SNIP]

IP4.ADDRESS[1]:                         192.168.122.1/24
IP4.GATEWAY:                            --
IP4.ROUTE[1]:                           dst = 192.168.122.0/24, nh = 0.0.0.0, mt = 0
IP6.GATEWAY:                            --

Solution 2

nmcli con modify "Wired connection 1" ifname ens7 type ethernet ip4 10.1.1.2/24 
Share:
6,485

Related videos on Youtube

RabT
Author by

RabT

Updated on September 18, 2022

Comments

  • RabT
    RabT over 1 year

    On a CentOS 7 virtual machine, I have an interface called ens7, as shown here:

    [root@localhost ~]# nmcli con show
    NAME                UUID                                  TYPE            DEVICE 
    Wired connection 1  448101d7-1f8f-4b78-ad90-7efd5be23b08  802-3-ethernet  ens7   
    eth0                d976f7ca-ab7f-4fd0-ab2b-6213815bd1a1  802-3-ethernet  eth0   
    

    How do I get nmcli to assign the 10.1.1.1 ip address to the ens7 interface?

    I tried typing nmcli con modify 'Wired connection 1' ipv4.addresses "10.1.1.2" from within the virtual machine, but it did not take, as seen in the results of `` as follows:

    [root@localhost ~]# nmcli con modify 'Wired connection 1' ipv4.addresses "10.1.1.2"
    [root@localhost ~]# nmcli device show
    GENERAL.DEVICE:                         eth0
    ...lots of stuff related to eth0
    
    GENERAL.DEVICE:                         ens7
    GENERAL.TYPE:                           ethernet
    GENERAL.HWADDR:                         52:54:00:8F:3B:14
    GENERAL.MTU:                            1500
    GENERAL.STATE:                          70 (connecting (getting IP configuration))
    GENERAL.CONNECTION:                     Wired connection 1
    GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/64
    WIRED-PROPERTIES.CARRIER:               on
    
    GENERAL.DEVICE:                         lo
    ...lots of stuff related to lo
    [root@localhost ~]#
    

    What syntax do I use to get the IP-address to show up in the nmcli device show results? So that I will be able to ping the new IP-address from an authorized outsider?

  • Torsten Bronger
    Torsten Bronger over 7 years
    If you downvote, please leave a comment why. Then, the reader can evaluate more easily whether the answer is helpful for them or not. Thanks!
  • RabT
    RabT over 5 years
    If someone else wants to validate this answer independently and leave comments on results, then I will mark this or the other answer as accepted. But I cannot validate myself as I am on to other things.