Share WiFi With Ethernet Port on a Raspberry Pi

18,190

140

31

About: Nothing interesting

Intro: Share WiFi With Ethernet Port on a Raspberry Pi

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

Share

    Recommendations

    • Furniture Contest 2018

      Furniture Contest 2018
    • Fix It! Contest

      Fix It! Contest
    • Tiny Home Contest

      Tiny Home Contest

    31 Discussions

    0
    None
    Svein AreK

    2 months ago on Step 4

    I just make the wpa_supplicant.conf on my laptop, an I put that, and an empty file named ssh, in the SD card BOOT partition... This will let it auto-connect to wifi, and enable ssh without using a display and a keyboard...

    Then, i just ssh into the PI and do the rest :)

    0
    None
    arpitjindal97

    4 months ago

    I just come to this page. Thanks @WaltW for this post and helping people with their queries.

    0
    None
    evilgeniusha

    Question 5 months ago

    Would I be able to connect multiple devices to the pi through a network switch?

    1 more answer
    0
    None
    WaltWevilgeniusha

    Answer 5 months ago

    Yes, you should be able to do that. You'll want to increase the number of IP addresses that dnsmasq manages for the amount of ports that you will be using on your switch.

    0
    None
    Nakker

    Question 6 months ago

    What kind of speed do you get out of this? Is there a noticable data speed drop by passing thourgh the pi?

    1 more answer
    0
    None
    WaltWNakker

    Answer 6 months ago

    I didn't do benchmarking or anything scientific. But I didn't notice any difference from my casual browsing. I have set up the same model RPi as a pi-hole and I haven't noticed any speed difference when using that as the dns server.

    0
    None
    WaltWstevemac00

    Answer 6 months ago

    @stevemac00, I apologize that I didn't see this earlier. Not sure why I missed it.

    I am not the Arpit Jindal in that github account.

    Just to clarify, you want to use the wlan0 and bridge that to the eth0 and then route that through pfSense. But what do you want to connect to the eth0? Are you wanting the RPi to serve as a hardware firewall between the wifi and whatever you're connecting to it? I don't have any experience with pfSense but the principle would be that same. My best guess would be that instead of using dnsmasq and iptables configuration you'd be using pfSense.

    0
    None
    dranem50260

    Question 7 months ago

    Hi, I am trying to connect an old Windows PC to my router through the Pi. I have performed all the steps and verified all commands and conf 3 times. My PC cannot access the web through the Pi. The static address on the Pi is set at 192.168.2.1 and my PC receives address 192.168.2.2 from dhcp.

    2 more answers
    0
    None
    WaltWdranem50260

    Answer 7 months ago

    What is the gateway up on your wireless router?

    0
    None
    dranem50260WaltW

    Answer 7 months ago

    I will check my xyzel router and post an answer to your question in the coming days. I also noted that the iptables and routing commands work ok when entered manually but fail when executed from the .sh script. I receive the following message: "iptables: No chain/target/match for this name", even when "sudo iptables -F" is the only command not commented out. Thanks for your support.

    0
    None
    WylM

    7 months ago

    I'm a wannabe geek so I have a few let's call them "will I get fired" questions. I work at a large college that takes their network security very seriously. No, I mean SERIOUSLY.

    So first question, could I use this method to plug in a wi-fi router and create my own network in my office (mainly to bypass the firewalls and safeguards that prevent me from doing things like streaming to a ChromeCast or controlling Keynote from my phone)?

    Second, let's pretend I did this (And used a solid WPA with no broadcast SSID). Am I opening my entire school's network up to a security breach because of the presence of this network? I assume not because the network failsafes would lock out my RPi before anything was able to do any damage.

    Finally (and here's the important question), would my system administrator easily figure out that something like this is going on, get angry, and make sure I never worked another day in this town?

    2 replies
    0
    None
    WaltWWylM

    Reply 7 months ago

    Umm, this is a complicated question, but overall it's just a really bad idea.

    1- Could you do this? Probably, but you'll be under all the same firewall rules for the gateway that you're currently using. This won't enable you to bypass any of those network wide settings.

    2- Yes, you're potentially opening up your network to a security breach. Any time you're adding more devices to a network you add the potential for a vulnerability, especially if those devices have poor security mechanisms in place.

    3- Yes, most likely they will be able to figure it out. It may take some time and you could get away with it for a while, but a good network admin would be able to track devices on the network.

    0
    None
    WylMWaltW

    Reply 7 months ago

    Thanks for the response. I'll stick to playing with stuff in my basement where I can't cause too much damage.

    0
    None
    merniebraukSoMuchBS

    Answer 7 months ago

    just did this project with an xfinity internet login, so yes! and THANK YOU to OG poster you've solved my internet pi woes :)

    0
    None
    WaltWSoMuchBS

    Answer 7 months ago

    Yes, I tried this on my router and was able to get it to work. However, I had to change the ip_address, dhcp_range_start and dhcp_range_end in the wifi-to eth-route.sh file to match the settings on the Xfinity router. My router uses 10.0.0.1 as the gateway this is how I set up the bash variables:

    ip_address="10.0.1.1"
    netmask="255.255.255.0"
    dhcp_range_start="10.0.1.2"
    dhcp_range_end="10.0.1.2"

    Then I reran the bash script from the command line and as able to connect through the RPi.

    0
    None
    Frickinghell

    7 months ago

    Can I use this to create my own wifi group if I connect to a wifi router? I have a boat and sometimes we can be quite a distance from a hotspot. To remedy this I have a high gain antennae and amplifier which plugs into a USB on my Macbook. Works a well, but unfortunately I cannot then share the wifi from my computer to other devices like phones and the wife's iPad! I have RasPi B (I think it is) Can I use this setup plugged into my Pi and the take the wifi signal out via Ethernet into an old router to create a hotspot on the boat?

    2 replies
    0
    None
    WaltWFrickinghell

    Reply 7 months ago

    @Frickinghell, yes this should be possible. I tried it on my home network and was able o connect the RPi to my ISP router with wifi and then connect a TP-LINK router to the RPi with the ethernet cable and the TP-LINK router handled all the wifi connections in my home.

    20180129_190558.jpg
    0
    None
    FrickinghellWaltW

    Reply 7 months ago

    Thank you that's great news. Now to dig out my RPi :)