I have a need to use a Raspberry Pi to connect to a wifi network and provide ethernet (wired) connection(s) to other devices.
I have it working using dnsmasq and setting ip addresses via the dhcp server in dnsmasq.
What I need, however, is for the wlan0 to eth0 connection to pass all traffic without using dhcp that is in dnsmasq and have the devices connected to the eth0 get and ip address and DNS from the dhcp server that is on the network the wifi is connected to.
The reason for this is because the eth device must have an ip address that is reserved in the DHCP server on the network but the device need to be able get that IP address from the dhcp server and not from dnsmasq on the pi.
Like this:
device > eth0 >[going through pi]>wlan0>network dhcp server>Internet
device gets ip address from server, not dnsmasq
NOT like this (how it is currently working)
device>eth0>[dhcp on pi]>wlan0>internet.
I have a script that sets it all up and is modified slightly from another fella to make it work.
*****************************
#!/bin/bash
# Share Wifi with Eth device
#
#
# This script is created to work with Raspbian Stretch
# but it can be used with most of the distributions
# by making few changes.
#
# Make sure you have already installed `dnsmasq`
# Please modify the variables according to your need
# Don't forget to change the name of network interface
# Check them with `ifconfig`
ip_address_and_network_mask_in_CDIR_notation="192.168.2.1/24"
dhcp_range_start="192.168.2.2"
dhcp_range_end="192.168.2.100"
dhcp_time="12h"
eth="eth0"
wlan="wlan0"
sudo systemctl start network-online.target &> /dev/null
sudo iptables -F
sudo iptables -t nat -F
sudo iptables -t nat -A POSTROUTING -o $wlan -j MASQUERADE
sudo iptables -t nat -A POSTROUTING -o $eth -j MASQUERADE
sudo iptables -A FORWARD -i $wlan -o $eth -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i $eth -o $wlan -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo ip link set $eth down
sudo ip link set $eth up
sudo ip addr add $ip_address_and_network_mask_in_CDIR_notation dev $eth
# Remove default route created by dhcpcd
sudo ip route del 0/0 dev $eth &> /dev/null
sudo systemctl stop dnsmasq
sudo rm -rf /etc/dnsmasq.d/* &> /dev/null
echo -e "interface=$eth
bind-interfaces
domain-needed
bogus-priv
dhcp-range=$dhcp_range_start,$dhcp_range_end,$dhcp_time" >/tmp/custom-dnsmasq.conf
sudo cp /tmp/custom-dnsmasq.conf /etc/dnsmasq.d/custom-dnsmasq.conf
sudo systemctl start dnsmasq
*****************************************
What I want is everything working like this, except the device connected to eth0 must get it's ip address from the network that wlan0 is connected to, not from dnsmasq. What this is for is to connect an IP phone with only an ethernet connection in one of out outbuildings so we don't have to run a cat6 cable out there.(lightning hazard). And it will allow us to put that phone anywhere it needs to be as long as there is a wifi connection available
.
I hope you can understand this. I have searched a LOT for this and everyone's solution uses the dhcp server in dnsmasq.
I have it working using dnsmasq and setting ip addresses via the dhcp server in dnsmasq.
What I need, however, is for the wlan0 to eth0 connection to pass all traffic without using dhcp that is in dnsmasq and have the devices connected to the eth0 get and ip address and DNS from the dhcp server that is on the network the wifi is connected to.
The reason for this is because the eth device must have an ip address that is reserved in the DHCP server on the network but the device need to be able get that IP address from the dhcp server and not from dnsmasq on the pi.
Like this:
device > eth0 >[going through pi]>wlan0>network dhcp server>Internet
device gets ip address from server, not dnsmasq
NOT like this (how it is currently working)
device>eth0>[dhcp on pi]>wlan0>internet.
I have a script that sets it all up and is modified slightly from another fella to make it work.
*****************************
#!/bin/bash
# Share Wifi with Eth device
#
#
# This script is created to work with Raspbian Stretch
# but it can be used with most of the distributions
# by making few changes.
#
# Make sure you have already installed `dnsmasq`
# Please modify the variables according to your need
# Don't forget to change the name of network interface
# Check them with `ifconfig`
ip_address_and_network_mask_in_CDIR_notation="192.168.2.1/24"
dhcp_range_start="192.168.2.2"
dhcp_range_end="192.168.2.100"
dhcp_time="12h"
eth="eth0"
wlan="wlan0"
sudo systemctl start network-online.target &> /dev/null
sudo iptables -F
sudo iptables -t nat -F
sudo iptables -t nat -A POSTROUTING -o $wlan -j MASQUERADE
sudo iptables -t nat -A POSTROUTING -o $eth -j MASQUERADE
sudo iptables -A FORWARD -i $wlan -o $eth -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i $eth -o $wlan -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo ip link set $eth down
sudo ip link set $eth up
sudo ip addr add $ip_address_and_network_mask_in_CDIR_notation dev $eth
# Remove default route created by dhcpcd
sudo ip route del 0/0 dev $eth &> /dev/null
sudo systemctl stop dnsmasq
sudo rm -rf /etc/dnsmasq.d/* &> /dev/null
echo -e "interface=$eth
bind-interfaces
domain-needed
bogus-priv
dhcp-range=$dhcp_range_start,$dhcp_range_end,$dhcp_time" >/tmp/custom-dnsmasq.conf
sudo cp /tmp/custom-dnsmasq.conf /etc/dnsmasq.d/custom-dnsmasq.conf
sudo systemctl start dnsmasq
*****************************************
What I want is everything working like this, except the device connected to eth0 must get it's ip address from the network that wlan0 is connected to, not from dnsmasq. What this is for is to connect an IP phone with only an ethernet connection in one of out outbuildings so we don't have to run a cat6 cable out there.(lightning hazard). And it will allow us to put that phone anywhere it needs to be as long as there is a wifi connection available
.
I hope you can understand this. I have searched a LOT for this and everyone's solution uses the dhcp server in dnsmasq.
Statistics: Posted by veewee77 — Fri Jun 20, 2025 8:00 pm — Replies 1 — Views 50