6

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?

lightbord
  • 169
  • 1
  • 3

3 Answers3

3

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'])
Aurora0001
  • 6,357
  • 3
  • 25
  • 39
PhillipB
  • 31
  • 1
2

I ran into this issue recently, my old Raspberry Pi became my FreeRadius server and so I set it up with a static IP.

The relevant information from PhillipB's Python script turned out to be running ip addr flush on the interface in question, eth0 in my case. Just restarting dhcpcd.service didn't help.

So here's the simplified solution in my case:

sudo systemctl stop dhcpcd.service
sudo ip addr flush dev eth0
sudo systemctl start dhcpcd.service
anssi
  • 21
  • 2
2

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.

Ray Foss
  • 161
  • 8