Page 1 of 1

Changing IP config without rebooting

Posted: Wed Dec 13, 2017 11:37 am
by hollymcr
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?

Re: Changing IP config without rebooting

Posted: Thu Dec 14, 2017 6:59 pm
by DougieLawson
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.

Re: Changing IP config without rebooting

Posted: Sun Dec 17, 2017 10:30 am
by hollymcr
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!)

Re: Changing IP config without rebooting

Posted: Sun Dec 17, 2017 10:34 am
by DougieLawson
It's this boot only, you need to change /etc/dhcpcd.conf if you want to make the changes permanent.

Re: Changing IP config without rebooting

Posted: Sun Dec 17, 2017 10:51 am
by hollymcr
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.

Re: Changing IP config without rebooting

Posted: Sun Dec 17, 2017 12:21 pm
by DougieLawson
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.

Re: Changing IP config without rebooting

Posted: Sun Dec 17, 2017 1:34 pm
by hollymcr
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?

Re: Changing IP config without rebooting

Posted: Thu Jan 18, 2018 3:45 pm
by pboone
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'])

Re: Changing IP config without rebooting

Posted: Mon Jan 22, 2018 2:38 pm
by hollymcr
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!

Re: Changing IP config without rebooting

Posted: Thu Apr 05, 2018 11:56 pm
by PlanB
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

Re: Changing IP config without rebooting

Posted: Tue May 01, 2018 8:50 am
by PlanB
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'])

Re: Changing IP config without rebooting

Posted: Wed Jul 18, 2018 12:20 pm
by codingecho
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.

Re: Changing IP config without rebooting

Posted: Sat Feb 23, 2019 1:33 pm
by farmerPaco
Hi codingecho,

I assume this solution still works. I keep loosing my wlan0 connectivity with a static address in stretch and am looking for a stable solution because I access my Pi remotely and need to fixed IP.

Was this solution for stretch or Jessie.
Can I do the same for wlan0 instead of eth0?

Right now I have to every so often:

Code: Select all

sude route add default gw 10.0.1.1 wlan0 
to get Internet.
Looking for a permanent fix.
Using a Raspi 2 headless accessed over ssh. Accessing at home is no problem but without stable internet I can’t be sure to access when on the road.

Will gladly accept guidance and/or feedback from anyone else for a static ip which doesn’t lose its gateway.

Re: Changing IP config without rebooting

Posted: Sat Feb 23, 2019 7:50 pm
by bls
@farmerPaco, you don't provide much detail about the wlan0 losing connectivity, but you may want to check out https://www.raspberrypi.org/forums/view ... 6&t=234058 to see if the wlan0 bounching problem description and hackaround are applicable to you.