5

I have a Pi 3, with the wifi interface connected to one network and the Ethernet interface connected to another network. How can I change the eth0 static IP I have set from /etc/dhcpcd.conf and apply those changes without rebooting the Pi? It used to be as simple as changing /etc/networking/interfaces, and restarting the networking service, but now that it's switched to using dhcpcd to configure it, my best attempts, including various combinations of: restarting dhcpcd, networking, and bringing the eth0 interface up and down several times, at best will result in me now having both the old and the new static ip, with ifconfig showing the old one.

After changing dhcpcd.conf and rebooting, it appears normal. But how can I achieve a clean static ip change without rebooting?

  • have you tried changing it with: sudo ifconfig eth0 X.X.X.X netmask 255.255.255.0 – Alan McDonley Dec 20 '17 at 19:05
  • That will change it until the next restart. I'm looking to change it permanently. So it has to be changed in dhcpcd.conf then dhcpcd must be restarted, and my problem is that restarting it is not applying my new static ip. – lightbord Dec 20 '17 at 19:12
  • what's the harm in rebooting? – tedder42 Dec 23 '17 at 0:44
  • 1
    @tedder42 It will ruin one's uptime record. – PNDA Dec 28 '17 at 16:46
  • @tedder42 the Pi is logging input from various sensors connected via GPIO. Rebooting would cause it to not log for that period of time. – lightbord Dec 28 '17 at 16:57
2

I attached a Python script to apply changes to dhcpcd.conf without rebooting. The key is to flush the IP addresses for each interface. It may take a few seconds to apply after the command is executed, but has worked for me.

Python script (note TAB required before "subprocess.call after for loop"):

#!/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'])
0

The following worked for me over a remote ssh connection:

sudo ifconfig eth0 10.0.1.71 netmask 255.255.255.0
sudo route add default gw 10.0.1.1

Where 10.0.1.71 is the Pi's IP address, /24 is the netmask and 10.0.1.1 is the default gateway. You'll still need to update /etc/dhcpcd.conf to have the changes survive a reboot.

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.