Matteo Filippa
Matteo Filippa

Reputation: 155

Switch from static IP to DHCP using nmcli

I need to change my connection, from staic assigned to DHCP using nmcli (invoked from a Python script). I've tried like this:

nmcli con mod "CONNECTION NAME" ipv4.method auto
nmcli con down "CONNECTION NAME"
nmcli con up "CONNECTION NAME"

But after the connection has gone up again, my old IP address is still there and if I show info for the connection I see something like that

....
ipv4.addresses:                         192.168.1.10/24  <-THIS IS THE BAD ONE 
....
IP4.ADDRESS[1]:                         192.168.1.10/24  <-THIS IS THE BAD ONE
IP4.ADDRESS[2]:                         10.0.2.15/24
....

How can I solve this?

Ubuntu version 16.04

Python version 3.5.2

Upvotes: 9

Views: 17273

Answers (4)

User051209
User051209

Reputation: 2513

My answer try to explain how to configure and use two NetworkManager connection profiles in the Ubuntu distribution. Both the connection profiles refer to the same ethernet interface.
To switch to DHCP you have to disable the IP Static connection profile and enable the DHCP connection profile.
This switching is executed by the use of the nmcli command.

A connection profile is saved in a file called <connection-name>.nmconnection in the folder /etc/NetworkManager/system-connections of the Ubuntu distribution.

Creation of 2 NetworkManager connection profiles

In the rest of the answer I'll use the term connection to mean the term connection profile.

A way to pass from Static Ip to DHCP (and viceversa) by nmcli command is to create 2 NetworkManager connections one for Static Ip and one for DHCP. Both the first connection and the second manage the same ethernet interface.
For example I suppose that the name of 2 connections are:

  • ethernet_ipstatic
  • ethernet_dhcp

This means that, in the system in the path /etc/NetworkManager/system-connections, there will be present 2 files called:

  • ethernet_ipstatic.nmconnection
  • ethernet_dhcp.nmconnection

The nmcli command for the creation of the 2 connections are:

# for ethernet_ipstatic
nmcli c add ifname enp2s0 type ethernet con-name ethernet_ipstatic

# for ethernet_dhcp
nmcli c add ifname enp2s0 type ethernet con-name ethernet_dhcp

In the nmcli example commands the ethernet interface name is enp2s0.

Set properties of the 2 connections

Now it is necessary to set properties of the 2 connections.
For the ethernet_ipstatic the properties are:

nmcli con mod ethernet_ipstatic ipv4.method manual ipv4.addresses 192.168.1.1/24 ipv4.gateway 192.168.1.100 ipv4.may-fail no ipv6.method disabled connection.autoconnect no connection.autoconnect-priority -1

Previous command set the following properties for the connection:

  • ipv4.method = manual (this set IP Static and not DHCP)
  • ip address 192.168.1.1, netmask 255.255.255.0, gateway 192.168.1.100
  • IPV6 disabled, autoconnection no, priority -1

For the ethernet_dhcp the properties are:

nmcli con mod ethernet_dhcp ipv4.method auto ipv4.addresses '' ipv4.gateway '' ipv4.may-fail no ipv4.dhcp-timeout 20 ipv6.method disabled connection.autoconnect no connection.autoconnect-priority -1 connection.autoconnect-retries 3

Previous command set the following properties for the connection:

  • ipv4.method = auto (this set DHCP and not Static IP Address)
  • ip address '', gateway ''
  • IPV6 disabled, autoconnection no, priority -1

Set DHCP

To set DHCP we can use the following sequence of nmcli commands:

# disable IP STATIC connection
nmcli con mod ethernet_ipstatic connection.autoconnect no connection.autoconnect-priority -1

# enable DHCP connection
nmcli con mod ethernet_dhcp connection.autoconnect yes connection.autoconnect-priority 10

# down IP STATIC connection
nmcli con down ethernet_ipstatic

# up DHCP connection
nmcli con up ethernet_dhcp

Set Ip Static

To set Ip Static we can use the following sequence of nmcli commands:

# disable DHCP connection
nmcli con mod ethernet_dhcp connection.autoconnect no connection.autoconnect-priority -1

# enable IP STATIC connection
nmcli con mod ethernet_ipstatic connection.autoconnect yes connection.autoconnect-priority 10

# down DHCP connection
nmcli con down ethernet_dhcp

# up IP STATIC connection
nmcli con up ethernet_ipstatic

The trick is the priority property

The key for the correct operation of this method is to change the priority property of connection profiles when it is necessary to pass from IP Static to DHCP or viceversa.

Upvotes: 3

Matteo Filippa
Matteo Filippa

Reputation: 155

After updating method (from manual to auto) i need to set gateway and ipaddrerr to "", like that

nmcli con mod "CONNECTION NAME" ipv4.method auto
nmcli con mod "CONNECTION NAME" ipv4.gateway ""
nmcli con mod "CONNECTION NAME" ipv4.address ""
nmcli con down "CONNECTION NAME"
nmcli con up "CONNECTION NAME"

Thanks to Nicolò Rebughini for the solution

Upvotes: 6

Dichado
Dichado

Reputation: 37

Sorry i have no Linux macchine right here but if i'm not wrong remebering, you have to create a profile before you edit connection

nmcli connection add type ethernet con-name "connection-name" ifname interface-name <-- this Is the interface name

After that you can edit profile configuration, so ADD a profile with whatever name bound to interface name.

Upvotes: 0

nirebu
nirebu

Reputation: 76

Not having a linux system in front of me, but if I remeber correctly, you can try to reset the IP addresses before the down/up:

nmcli con mod "CONNECTION NAME" ipv4.address ""
nmcli con mod "CONNECTION NAME" ipv4.method auto
nmcli con down "CONNECTION NAME"
nmcli con up "CONNECTION NAME"

Upvotes: 6

Related Questions