hollymcr
Posts: 8
Joined: Thu Feb 11, 2016 7:15 pm

Changing IP config without rebooting

Wed Dec 13, 2017 11:37 am

I'm using stretch and trying to change my IP settings (cabled and/or wifi) without rebooting.

If I make changes to /etc/dhcpcd.conf and/or /etc/wpa_supplicant/wpa_supplicant.conf and then:

Code: Select all

sudo systemctl daemon-reload
sudo systemctl restart dhcpcd
then changes are applied (eg if I switch from DHCP to static IP in dhcpcd then I get that static IP, and if I add wireless connection details to wpa_supplicant.conf then I get connected to wifi.)

However any old configuration seems to persist. This is most obvious if I remove the wireless settings then repeat the dhcpcd restart; iwconfig will confirm that the wireless is no longer connected but ifconfig will show that wlan0 still has an IP address, and nothing I do short of rebooting will remove it.

The same seems to be the case with the wired ethernet (eg if I add a static IP, then change to a different static IP, then both seem to be retained until reboot). This description is a bit wooly and it doesn't seem consistent, so if anyone is trying to replicate I suggest playing with the wireless settings as that is easy to reproduce: enter settings, restart dhcpcd, remove settings, restart dhcpcd, look at ifconfig,

What am I missing?

User avatar
DougieLawson
Posts: 33314
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Changing IP config without rebooting

Thu Dec 14, 2017 6:59 pm

Try using

sudo ip addr change dev eth0 192.168.3.14/24
sudo ip route add default via 192.168.3.254


Obviously using your address block for the interface address and your gateway router for the route add.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

hollymcr
Posts: 8
Joined: Thu Feb 11, 2016 7:15 pm

Re: Changing IP config without rebooting

Sun Dec 17, 2017 10:30 am

DougieLawson wrote:
Thu Dec 14, 2017 6:59 pm
Try using

sudo ip addr change dev eth0 192.168.3.14/24
sudo ip route add default via 192.168.3.254
Thanks, I'll try this, but I'm assuming this won't (on its own) be persistent?

(Also doesn't help with the similar WiFi issue but at this point I'll take any suggestions going!)

User avatar
DougieLawson
Posts: 33314
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Changing IP config without rebooting

Sun Dec 17, 2017 10:34 am

It's this boot only, you need to change /etc/dhcpcd.conf if you want to make the changes permanent.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

hollymcr
Posts: 8
Joined: Thu Feb 11, 2016 7:15 pm

Re: Changing IP config without rebooting

Sun Dec 17, 2017 10:51 am

DougieLawson wrote:
Sun Dec 17, 2017 10:34 am
It's this boot only, you need to change /etc/dhcpcd.conf if you want to make the changes permanent.
There has to be a way to tell the Pi to reread dhcpcd.conf for the current session (and wpa_supplicant.conf) instead of requiring a reboot.

The reason I haven't tested your initial suggestions yet is that I'm rebuilding my Pi using the latest Raspbian release in case there's a fix or I've broken something. In the absence of any suggestions to the contrary I think I'm doing everything right (eg restarting dhcpcd) and it's the Pi that's not.

User avatar
DougieLawson
Posts: 33314
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Changing IP config without rebooting

Sun Dec 17, 2017 12:21 pm

Don't bother trying, it only generates the same internal commands as sudo ip addr change ..., so in my view it's a million times easier to just use that ip addr change command rather than mucking about wasting time trying to hack dhcpcd into submission.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

hollymcr
Posts: 8
Joined: Thu Feb 11, 2016 7:15 pm

Re: Changing IP config without rebooting

Sun Dec 17, 2017 1:34 pm

DougieLawson wrote:
Sun Dec 17, 2017 12:21 pm
Don't bother trying, it only generates the same internal commands as sudo ip addr change ..., so in my view it's a million times easier to just use that ip addr change command rather than mucking about wasting time trying to hack dhcpcd into submission.
Actually, that gives me an idea. The dhcpcd restart and wpa_cli reconfigure work, except for the bit about removing old addresses. So I should be able to remove existing configurations with "ip addr" then use the dhcpcd/wpa_cli commands to finish the job?

pboone
Posts: 1
Joined: Thu Jan 18, 2018 3:39 pm

Re: Changing IP config without rebooting

Thu Jan 18, 2018 3:45 pm

I had the same problem and wanted a general way to (1) Make modifications to /etc/dhcpcd.conf and (2) restart all network interfaces without a reboot. The missing piece is that each network interface needs the IP to be flushed so that on restart the system configures the device.

I created a Python script that will do this. You just need to create a file, add the contents below, and then make the script executable.

Create a file "if_restart.py" with the following contents:

#!/usr/bin/env python
import os
import subprocess

NET_DIR='/sys/class/net'

subprocess.call(['sudo','systemctl','daemon-reload'])
subprocess.call(['sudo','systemctl','stop','dhcpcd.service'])

for net_dev in os.listdir(NET_DIR):
subprocess.call(['sudo','ip','addr','flush','dev',net_dev])

subprocess.call(['sudo','systemctl','start','dhcpcd.service'])
subprocess.call(['sudo','systemctl','restart','networking.service'])

hollymcr
Posts: 8
Joined: Thu Feb 11, 2016 7:15 pm

Re: Changing IP config without rebooting

Mon Jan 22, 2018 2:38 pm

pboone wrote:
Thu Jan 18, 2018 3:45 pm
I had the same problem and wanted a general way to (1) Make modifications to /etc/dhcpcd.conf and (2) restart all network interfaces without a reboot. The missing piece is that each network interface needs the IP to be flushed so that on restart the system configures the device.
Perfect, thanks - I'll give this a go!

PlanB
Posts: 92
Joined: Tue Oct 28, 2014 6:44 am

Re: Changing IP config without rebooting

Thu Apr 05, 2018 11:56 pm

You don't have a Jessie version of this script by any chance? Jessie does not seem to have anything in /sys/class/net/wlan0

PlanB
Posts: 92
Joined: Tue Oct 28, 2014 6:44 am

Re: Changing IP config without rebooting

Tue May 01, 2018 8:50 am

After a lot of trial & error (the great fallback for those of us bedazzled by networking) I found this works in Jessie.

Code: Select all

import os
import subprocess

NET_DIR='/sys/class/net'

print; print 'reload daemon'
subprocess.call(['sudo','systemctl','daemon-reload'])

print 'stop dhcpcd'
subprocess.call(['sudo','systemctl','stop','dhcpcd.service'])

print

for net_dev in os.listdir(NET_DIR):

  print net_dev

  print 'flush old ip' 
  subprocess.call(['sudo','ip','addr','flush','dev',net_dev])

  print 'restart dhcpcd'
  subprocess.call(['sudo','systemctl','restart','dhcpcd.service'])

codingecho
Posts: 1
Joined: Wed Jul 18, 2018 11:05 am

Re: Changing IP config without rebooting

Wed Jul 18, 2018 12:20 pm

When I change my ip adress this is what I do:
open dhcpcd.conf in editor nano
sudo nano /etc/dhcpcd.conf
then I add or change my static ip:
interface eth0
static ip_address=10.0.0.99/24
static routers=10.0.0.138
static domain_name_servers=10.0.0.138

CTRL+O to write then CTRL+X to exit.

Then I flush old ip settings on eth0:
sudo ip addr flush dev eth0
then I restart dhcpcd services so that the new ip gets added again:
sudo systemctl restart dhcpcd.services
If im connected whit ssh I run both commands at the same time whit nohup thats because when I flush the old ip I lose connection and will not be able to run the second:
sudo nohup sh -c 'ip addr flush dev eth0 && systemctl restart dhcpcd.service' > /dev/null &
You can alwasy use script do do the same like pboone did. But if you just want to change it one time then I prefer this option instead of making a python script just to change ip.

Return to “Advanced users”

Who is online

Users browsing this forum: No registered users and 10 guests