3

Hi I'm trying to configure my Raspberry PI to work as a dhcp server. Idea is that through eth0 interface I will connect switch, to which I can connect other devices. But I still want to keep ssh and internet access through the wlan0.

To achieve I have installed isc-dhcp-server and then configured it by adding configuration for the static ip address to the /etc/network/interfaces.d/eth0

allow-hotplug eth0
iface eth0 inet static
  address 10.0.0.1
  netmask 255.255.255.0
  broadcast 10.0.0.255
  gateway 10.0.0.1

Next I have added configuration for dhcp server to file /etc/dhcp/dhcpd.conf:

authorative
subnet 10.0.0.0 netmask 255.255.255.0 {
  range 10.0.0.1 10.0.0.10;
  option subnet-mask 255.255.255.0;
  option broadcast-address 10.0.0.255;
  option routers 10.0.0.1;
  option domain-name "local-network";
  option domain-name-servers 8.8.8.8, 8.8.4.4;
}

After that I have restarted Raspberry PI and everything seems fine I'm able to connect from my machine to it through the WiFi and other machines that are connected to switch are getting IP addresses from 10.0.0.2 to 10.0.0.10 (and I'm able to ssh to them through the configured Raspberry PI).

Biggest problem is that I'm not able to access internet from that Raspberry PI. When I type something like

ping 10.0.0.3

it works, but

ping 8.8.8.8

fails. After debugging the problem I managed to discover problem with command ip route:

default via 10.0.0.1 dev eth0 proto dhcp src 10.0.0.2 metric 202 
default via 192.168.0.1 dev wlan0 proto dhcp src 192.168.0.46 metric 303 
10.0.0.0/24 dev eth0 proto dhcp scope link src 10.0.0.2 metric 202 
192.168.0.0/24 dev wlan0 proto dhcp scope link src 192.168.0.46 metric 303

I have tried to remove default route via 10.0.0.1 and it helps temporary, but after sometime some process (I suspect dhcp server) recovers it. Of course I can write a daemon that will remove it all the time, but I'm looking for some better solution.

Update #1

Without dhcp server ip route shows:

default via 192.168.0.1 dev wlan0 proto dhcp src 192.168.0.46 metric 303 
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.1 
169.254.0.0/16 dev eth0 scope link src 169.254.44.117 metric 202 
192.168.0.0/24 dev wlan0 proto dhcp scope link src 192.168.0.46 metric 303

Thanks in advance

  • It's unusual that eth0 is being configured with dhcp when it has a static setup. Are there any entries in /etc/network/interfaces or /etc/network/interfaces.d/* containing iface eth0 inet dhcp? – Torin Jan 3 at 23:44
  • /etc/network/interfaces has only one line "source-directory /etc/network/interfaces.d", /etc/network/interfaces.d has only file that I have created above. – kobra Jan 3 at 23:55
  • I am having similar issues and problems trying to setup a k8s cluster on my pis. However, I am seeing my master server getting 10.0.0.1 on eth0 and 10.0.0.2 seems to be pointed to the same mac address as 10.0.0.1? – Shawn Jan 4 at 0:06
  • If you are using stretch like I am it appears things have changed with networking. I am still trying to work out how to set this up. raspberrypi.stackexchange.com/questions/37920/… – Shawn Jan 4 at 2:52
1

The problem is indeed the route table but I have successfully removed the route record without it returning.

As mentioned above, the networking in Stretch (back to Jessie I think) changed quite a bit. This post led me to instructions for setting up static IPs via modification of /etc/dhcpcd.conf instead of /etc/network/interfaces*. However, I discovered it was best in my situation to comment out the static router=x.x.x.x variable for my eth0 adapter static ip, as I did not want that to serve as my default gateway. I then simply removed the route from the table with ip route del default via <address> dev eth0 and it stayed deleted.

You won't be done here though, for internet access to work properly across your other devices connected to the dhcp server you will need to do the following to setup NAT:

1) Edit /etc/sysctl.conf to enable IP forwarding with the following toggle net.ipv4.ip_forward=1
2) Edit /etc/rc.local to add iptable rules for forwarding from eth0 to wlan0 and back. Add the following:

iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
iptables -A FORWARD -i wlan0 -o eth0 -m state \
--state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT

3) Reboot for changes to take effect

Your other devices should now work as intended with wifi access and IPs assigned from your dhcp server.

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.