Share WiFi With Ethernet Port on a Raspberry Pi

108,245

172

59

Introduction: Share WiFi With Ethernet Port on a Raspberry Pi

About: Nothing interesting

Do you have an old laser printer or scanner that still works great but isn't wifi compatible? Or maybe you want to connect an external hard drive as a backup device on your network and you've run out of ethernet ports on your home router. This instructable will help you create a bridge from the wifi connection to the ethernet port on a Raspberry Pi.

I needed a way to connect an older Xerox copier/printer that has a built in network adapter and network software but wasn't wifi compatible. This printer was in an older building and the printer was in a location that wasn't close to an ethernet punch down and couldn't be moved. With a few parts that I already had around my house I was able to put together a solution that solved my needs.

This easy DIY solution will give you the capability of adding a wifi connection to your older devices without breaking the bank by purchasing wireless print adapter.

Step 1: Items You'll Need

  1. Raspberry Pi (any model will do, but you'll see faster results with a model 3).
  2. Power adapter for your Pi.
  3. SD Card to install the operating system on (you could use an 8GB card up to whatever size you want. I usually go with a 32GB card in case I want to add more options to the RPi).
  4. Wifi adapter
  5. Ethernet cable
  6. HDML cable (I have an old DVI monitor so I use this HDMI to DVI cable).
  7. Keyboard and mouse
  8. Card reader or computer with built in card reader.
  9. A case for the Pi (optional)

Step 2: Install Raspbian Operating System

Download the latest version of Raspbian (Stretch as of this writing) operating system from here. Insert the SD card in the adapter that ships with it and put the adapter with the SD card into your card reader. Copy the Raspian operating system image onto the SD card by using these instructions:

Step 3: Assemble the Remaining Components

This will take some time to copy the image to your SD card. Assemble the rest of the RPi while you are waiting.

Insert the wifi adapter in one of the USB ports. Insert the keyboard and mouse dongle into one of the other USB ports. Connect a monitor to the Raspberry Pi with the HDMI cable.

When the Raspbian image has completed installing on the SD card, remove the SD card from the adapter and insert it into the SD card slot on the underside of the Raspberry Pi. Then insert the power adapter into the micro USB port and power up the Raspberry Pi.

Step 4: Set Up WiFi Connection

Once the Raspberry Pi has finished booting up set up your wifi connection on the Raspberry PI by opening a terminal window and edit the wpa_supplicant.conf file by using the following command:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Change the country to your two letter country code.

Add your Wifi access point SSID and password at the bottom of the file:

network={
ssid="Your Wifi SSID"
psk="yourWifiPassword"
}

One thing to note: The default keyboard setting is to use a GB configuration. For those of us in the US it places some of the special characters in different places, mainly the @ and the " symbols are switched.

Save the file and exit nano.

Bring the WIfi connection up by typing:

ifup

or by rebooting the Raspberry PI with:

sudo reboot

Step 5: Configure Other Options With Raspi-config

When your Raspberry Pi has successfully connected to your Wifi you should see the Wifi symbol in the menu bar in the upper right corner of the screen for the Raspberry Pi.

You can now configure other options for your Pi. From a termnial window type:

sudo raspi-config

This will bring up the raspi-config interface an allow you to configure other options your Raspberry PI. You don't have to do this but there are a couple of things that you should do:

  1. Change the default password. Don't leave devices on your network vulnerable by leaving the default passwords on your RPi for the pi and root users.
  2. Set your location setting. This will give you the right keyboard settings, time settings and location for installing other software from the nearest repos. If you get the annoying PERL warnings about the locale cannot be set you can resolve it by using these instructions.
  3. Expand the file system to use the entire SD card. This will give you access to the entire storage space on the HD card.

Feel free to look at the other options that are available to you through this interface. You can do other things like overclocking your CPU, set up ssh and ftp connections, and change your boot settings to boot to a command line or the desktop.

Step 6: Configure the Network Bridge From Wifi to Ethernet

To do this we're going to use dnsmasq to set up the RPi to be a DHCP server and set up some custom DNS settings. This will allow the device connected to the RPi through ethernet to get an IP address from the RPi and also to for the RPi to pass DNS queries.

We'll also configure some iptables settings to make a NAT between the ethernet adapter and the Wifi connection.

First, install dnsmasq:

sudo apt-get install dnsmasq

Set your ethernet adapter to a static IP address

This will serve as a gateway for the device that you want to connect to the RPi ethernet port. Most Wifi routers use what's called a Private Network and set the IP range to something similar to:

192.168.1.1

For the ethernet adapter on your RPI you'll want to set that to an address that won't interfere with the routers ability to assign addresses, so we'll increment the subnet of the PRi to be:

192.168.2.1

Along with that you'll need to set up the the netmask to:

255.255.255.0

As well as the DCHP settings to broadcast what IP address are available:

network 192.168.2.0
broadcast 192.168.2.255


Use iptables to configure a NAT setting to share the Wifi connection with the ethernet port
NAT stands for Network Address Translation. This allows a single IP address to server as a router on a network. So in this case the ethernet adapter on the RPi will serve as the router for whatever device you attach to it. The NAT settings will route the ethernet requests through the Wifi connection.

There are several commands to run here:

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

Configure the dnsmasq settings

The first thing to do is to turn on IP forwarding. This is done by putting a single number 1 in the /proc/sys/net/ipv4/ip_forward file:

sudo nano /proc/sys/net/ipv4/ip_forward

Put a 1 on the first line and then exit and save. Hint: you also may have to edit /etc/sysctl.conf and uncomment this line:

net.ipv4.ip_forward=1

Next set up ip routing:

sudo ip route del 0/0 dev eth0 &> /dev/null
a=`route | awk "/${wlan}/"'{print $5+1;exit}'`
sudo route add -net default gw 192.168.2.1 netmask 0.0.0.0 dev eth0 metric $a

The last thing to do is edit your /etc/dnsmasq.conf file and include these settings;

interface=eth0
bind-interfaces
server=8.8.8.8
domain-needed
bogus-priv
dhcp-range=192.168.2.2,192.168.2.100,12h

Then run this command to start your dnsmasq services:

sudo systemctl start dnsmasq

Now plug a CAT5 network cable into the device you want to include on the network and put the other end of the cable into the ethernet port on the RPi and you should be good to go! When we set up the ethernet interface we made it hot pluggable, so you should see the ethernet interface come up when you plug the device into the RPi.

Step 7: Automate All Those Commands With a Script

This was a lot of work to get the network bridge up and running. You'll probably want this to run automatically every time your RPi boots up, so to do that we're going to need a script to run all of these commands for us. Luckily Arpit Agarwal has already created a script and is available for download here.

Don't worry about typing all those commands above and run this command from your home directory to download the script file:

https://raw.githubusercontent.com/arpitjindal97/raspbian-recipes/master/wifi-to-eth-route.sh

To get this file to run every time you boot your RPi you'll need to add a directive to your session autostart file:

nano /home/pi/.config/lxsession/LXDE-pi/autostart

and add this to the bottom of the file:

sudo bash /home/pi/wifi-to-eth-route.sh

Then just reboot the RPi and the script does all the work for you. You can also run this set up any time you want by running this command from a terminal:

sudo bash /home/pi/wifi-to-eth-route.sh

First Time Author Contest 2018

Participated in the
First Time Author Contest 2018

Raspberry Pi Contest 2017

Participated in the
Raspberry Pi Contest 2017

3 People Made This Project!

Recommendations

  • STEM Contest

    STEM Contest
  • Role Playing Game Challenge

    Role Playing Game Challenge
  • CNC Contest

    CNC Contest

59 Comments

0
vtoib
vtoib

15 hours ago

I was able to get this to work easily with easytether and an android phone for anyone that is interested, once easytether was setup I just changed one line in the script:

from
wlan="wlan0"

to
wlan="tun-easytether"

plugged the Pi into my router's WAN port and it worked perfectly

0
lmmclean08
lmmclean08

12 days ago

hey! great guide! Just one question: will this prevent me from remoting (ssh) into my pi, as it redirects all packets to the ethernet connection? will it only do this after the autostart has been executed?

0
lindsay.arthur.fowler
lindsay.arthur.fowler

3 months ago

Hi, I'm having a bit of trouble with these instructions:
"Set your ethernet adapter to a static IP address
This will serve as a gateway for the device that you want to connect to the RPi ethernet port. Most Wifi routers use what's called a Private Network and set the IP range to something similar to:
192.168.1.1
For the ethernet adapter on your RPI you'll want to set that to an address that won't interfere with the routers ability to assign addresses, so we'll increment the subnet of the PRi to be:
192.168.2.1
Along with that you'll need to set up the the netmask to:
255.255.255.0
As well as the DCHP settings to broadcast what IP address are available:
network 192.168.2.0
broadcast 192.168.2.255"


In Raspian, there is nothing in the GUI to accomplish this, so I presume you mean for us to edit etc/dhcpcd.cfg manually?
I have used this and set my ethernet0 with the following two lines:
interface eth0
static ip_address=192.168.2.1/24

-is that correct?

And what do you mean by:
"As well as the DCHP settings to broadcast what IP address are available:network 192.168.2.0 broadcast 192.168.2.255"

How do we do that?
Thanks very much?


0
MorphyDK
MorphyDK

Question 6 months ago

Hi an thanks or this great guide.

When I get to :

sudo route add -net default gw 192.168.2.1 netmask 0.0.0.0 dev eth0 metric $a

I get this error:
pi@rasp4:~ $ sudo route add -net default gw 192.168.2.1 netmask 0.0.0.0 dev eth0 metric $a

Usage: inet_route [-vF] del {-host|-net} Target[/prefix] [gw Gw] [metric M] [[dev] If]

inet_route [-vF] add {-host|-net} Target[/prefix] [gw Gw] [metric M]

[netmask N] [mss Mss] [window W] [irtt I]

[mod] [dyn] [reinstate] [[dev] If]

inet_route [-vF] add {-host|-net} Target[/prefix] [metric M] reject

inet_route [-FC] flush NOT supported

What am I doing wrong?

Thanks :)

0
harold.hefley1
harold.hefley1

8 months ago

Hi, a noobs Raspberry pi 4. I tried to start at the after wifi connection. Copy and paste each command. sudo iptables -F (iptables/1.8.2 Failed to initialize nft: Protocol not supported. I really need to use iPhone data through the pi for Ethernet to a router. I can connect using hotspot to each device, but would like the router do the heavy lifting.
I've been doing trial and error with the terminal. I can open terminal with crtl, alt. t combination
Help needed please!!!

0
eyemmateo
eyemmateo

Question 10 months ago on Introduction

do I need to use a USB wifi adapter or can I use the wifi built in my pi?

0
rhospid
rhospid

Answer 8 months ago

It works fine with just the onboard wifi

0
bad_brad
bad_brad

Question 1 year ago

i configured this but it isn't working. Please help.

pi@PI1:~ $ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.1.1 0.0.0.0 UG 1 0 0 eth0
default 10.10.10.254 0.0.0.0 UG 303 0 0 wlan0
10.10.10.128 0.0.0.0 255.255.255.128 U 303 0 0 wlan0
192.168.1.0 0.0.0.0 255.255.255.0 U 202 0 0 eth0
pi@PI1:~ $ sudo iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
pi@PI1:~ $

0
FelixK28
FelixK28

Question 1 year ago

I want to use the Raspberrypi as an Ethernet to Usb Adapter. It should just bridge the Ethernet connection with the USB Port on the back. How do I do this?

0
cwills2000
cwills2000

1 year ago

I'm trying to setup my Pi by using the wifi-to-eth-route.sh script. My Pi connects to wifi and I'm able to get to the web in the Chromium browser and by pinging a website using the terminal. I'm testing the ethernet connection by connecting it to my Macbook Pro with it's wifi turned off. The network settings show that I have an ethernet connection with the following settings: ip address 192.168.2.94, subnet mask 255.255.255.0, router 192.168.2.1

The problem I have is the Mac can't connect to the internet. I can't connect through the browser and pinging sites through terminal returns a cannot resolve, unknown host error.

My router gateway is 192.168.1.1

Any suggestions on what I can try to get this working?

0
medicoh
medicoh

1 year ago

This works a treat. I used the bash script method. The only thing I would like to change is that the Wireless & Wired Network Settings" panel shows "eth0 configured 169.254.91.66/16" , instead of what is actually the case show by ifconfig 192.168.2.1?
I see you are in the same situation in one of your screen shots.
Thanks for this guide :-)

2020-01-31-231117_1920x1080_scrot.png
1
BjørnM2
BjørnM2

1 year ago

hi, thank you for making the time to set this up for everybody. I am unable to get this working. I believe where I am getting lost is the directions for specifying ip address and netmask and such. You see, when I go to specify these options the network preferences dialog for interface eth0 gives me options for ipv4 address, router, and dns server and dns search. There isn't any option available for netmask. Where should I enter this information?

0
Darren Smith Photography

I have this working and I can connect another rPi device to it, I am a little confused how to access my printer through it.

if my main network is 192.168.1.XXX
my printer has been given the ip address 192.168.2.10

how can my pc at 192.168.1.100 connecting to it?

thanks for your suggestions





1
an0ndev
an0ndev

2 years ago

Hey i get this error SIOADDRT: Network is Unreachable when i type this:
sudo route add -net default gw 192.168.2.1 netmask 0.0.0.0 dev eth0 metric $a
and after all i dont get a connection my eth0 is down. i musing RPi 3 B+

2
2MylesAway
2MylesAway

Reply 1 year ago

sudo route add -net default gw 192.168.2.1 netmask 0.0.0.0 dev eth0 metric $a
### If an error occurs when attempting try this:
# sudo ip route add 192.168.2.0/23 dev eth0
### And then repeat the first command

0
fossy fosser
fossy fosser

Reply 1 year ago

I think you meant '/24', though apparently /0 works too

0
pctopgs1
pctopgs1

Reply 1 year ago

I get the same issue

0
2MylesAway
2MylesAway

Reply 1 year ago

sudo route add -net default gw 192.168.2.1 netmask 0.0.0.0 dev eth0 metric $a
### If an error occurs when attempting try this:
# sudo ip route add 192.168.2.0/23 dev eth0
### And then repeat the first command

0
fossy fosser
fossy fosser

1 year ago

Thank you for the guide. You speak my language, I don't see that too often.

Pain points I ran into:
- I don't know how to set static IPs in Raspbian... I ended up using the GUI and remembering to set a subnet mask on the IP field with `/24`.

- `sudo route add -net default gw 192.168.2.1 netmask 0.0.0.0 dev eth0 metric $a`
Im scratching my head at the purpose of this line... Why is the netmask disabled? Why do we need to specify a gateway... unless the only reason we are making this network is, to set a constraint for the metric

0
David LG
David LG

1 year ago

I've made it and it all works fine,thank you so much, until the point i want to add nordVPN to it, by following these instructions : https://pimylifeup.com/raspberry-pi-wireless-acces...

Whenever i try to connect the VPN, connection dies on the ethernet port, any clue ? I am a total IPtable noob, please help, nord VPN gives me this :

Mon Dec 2 21:17:01 2019 SENT CONTROL [fr222.nordvpn.com]: 'PUSH_REQUEST' (statu s=1)
Mon Dec 2 21:17:01 2019 PUSH: Received control message: 'PUSH_REPLY,redirect-ga teway def1,dhcp-option DNS 103.86.96.100,dhcp-option DNS 103.86.99.100,sndbuf 52 4288,rcvbuf 524288,explicit-exit-notify,comp-lzo no,route-gateway 10.7.7.1,topol ogy subnet,ping 60,ping-restart 180,ifconfig 10.7.7.17 255.255.255.0,peer-id 0,c ipher AES-256-GCM'

Mon Dec 2 21:17:01 2019 OPTIONS IMPORT: timers and/or timeouts modified

Mon Dec 2 21:17:01 2019 OPTIONS IMPORT: --explicit-exit-notify can only be used with --proto udp

Mon Dec 2 21:17:01 2019 OPTIONS IMPORT: compression parms modified

Mon Dec 2 21:17:01 2019 OPTIONS IMPORT: --sndbuf/--rcvbuf options modified

Mon Dec 2 21:17:01 2019 Socket Buffers: R=[341760->327680] S=[44800->327680]

Mon Dec 2 21:17:01 2019 OPTIONS IMPORT: --ifconfig/up options modified

Mon Dec 2 21:17:01 2019 OPTIONS IMPORT: route options modified

Mon Dec 2 21:17:01 2019 OPTIONS IMPORT: route-related options modified

Mon Dec 2 21:17:01 2019 OPTIONS IMPORT: --ip-win32 and/or --dhcp-option options modified

Mon Dec 2 21:17:01 2019 OPTIONS IMPORT: peer-id set

Mon Dec 2 21:17:01 2019 OPTIONS IMPORT: adjusting link_mtu to 1659

Mon Dec 2 21:17:01 2019 OPTIONS IMPORT: data channel crypto options modified

Mon Dec 2 21:17:01 2019 Data Channel: using negotiated cipher 'AES-256-GCM'

Mon Dec 2 21:17:01 2019 Outgoing Data Channel: Cipher 'AES-256-GCM' initialized with 256 bit key

Mon Dec 2 21:17:01 2019 Incoming Data Channel: Cipher 'AES-256-GCM' initialized with 256 bit key

Mon Dec 2 21:17:01 2019 ROUTE_GATEWAY 192.168.1.1/255.255.255.0 IFACE=wlan0 HWA DDR=b8:27:eb:bb:a7:f2

Mon Dec 2 21:17:01 2019 TUN/TAP device tun0 opened

Mon Dec 2 21:17:01 2019 TUN/TAP TX queue length set to 100

Mon Dec 2 21:17:01 2019 /sbin/ip link set dev tun0 up mtu 1500

Mon Dec 2 21:17:01 2019 /sbin/ip addr add dev tun0 10.7.7.17/24 broadcast 10.7.


It seems the GATEWAY should be 192.168.2.1 instead of 1.1 ?