Share your Raspberry Pi internet from Wifi to Ethernet Adapter
Share your Raspberry Pi internet from Wifi to Ethernet Adapter. This project will make Raspberry Pi your personalized router. It translate the internet connection you get from maybe hotel's internet to your home network.
About my Raspberry Pi:
To do this, open your terminal and type:
Open interfaces by typing:
and add this code on the last:
STEP 2: CONFIGURING DHCP service (dnsmasq)
About my Raspberry Pi:
What you'll need:
- Raspberry Pi 3
- Noobs updated
- Wifi name and password
- Ethernet cable
- Putty
Step 1: Configure the Ethernet Interface
We have to assign a static IP 192.168.1.1 for Raspberry Pi Ethernet adapter.To do this, open your terminal and type:
Add this on the last linesudo nano /etc/dhcpcd.conf
Save and exit (Press ctrl + o to save and ctrl +x to exit)denyinterfaces eth0
Open interfaces by typing:
sudo nano /etc/network/interfaces
and add this code on the last:
Save and exitauto eth0 allow-hotplug eth0 iface eth0 inet static address 192.168.1.1 netmask 255.255.255.0 network 192.168.1.0 broadcast 192.168.1.255
STEP 2: CONFIGURING DHCP service (dnsmasq)
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig sudo nano /etc/dnsmasq.conf
Add this code
Save and exitinterface=eth0 listen-address=192.168.1.1 dhcp-range=192.168.1.50,192.168.1.100,12h server=8.8.8.8 bind-interfaces domain-needed bogus-priv
Reboot your device
sudo reboot
USING PUTTY TO CONFIGURE RASPBERRY
Ensure your Laptop Ethernet adapter setting is set to auto obtain IP address. Plug your Ethernet cable to your Raspberry Pi and the other end to your Laptop Ethernet port. Your Laptop should get IP address assigned by your Raspberry.
Now open Putty and type 192.168.1.1(Raspberry Pi IP Address) in Host Name, type any name you like in Save Sessions, click Save and finally click Open.
It will ask you a username and password every time you log in to Raspberry. Use the details below:
Username: pi Password: raspberry
From here we will use Putty to configure the Raspberry Pi
STEP 3: Configuring Wifi interface
In this step, we will configure the Wifi setting for the Raspberry to be able to connect to a wifi network.
sudo nano /etc/network/interfaces
Useful tip: You can just copy codes from your browser and paste it to putty by pressing right click of your mouse inside the putty window.
Paste this code at the end of the line.
Save and exitauto wlan0 allow-hotplug wlan0 iface wlan0 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
Set the Wifi password
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Paste this code and edit the ssid (wifi name) and psk (wifi password)
Save and exitctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 country=GB network={ ssid="***HERE IS YOUR WIFI ID***" psk="***HERE IS THE WIFI PASSWORD**" key_mgmt=WPA-PSK }
Reboot your Raspberry pi
sudo reboot
Check if your Wifi is connected and has internet connection
If it download and update, it means you have internetsudo apt-get update
STEP 4: SHARING THE INTERNET
We have to forward the internet connection we got from Wifi to the Ethernet port where our laptop is connected.Setup IPv4 Forwarding:
sudo nano /etc/sysctl.conf
Un-comment this line by removing #
And lets activate itnet.ipv4.ip_forward=1
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
Lets reset the IP table. Write a script in a file named tablereset.sh
nano tablereset.sh
Paste this code:
Save and exit#!/bin/sh echo "Resetting the IP Tables" ipt="/sbin/iptables" ## Failsafe - die if /sbin/iptables not found [ ! -x "$ipt" ] && { echo "$0: \"${ipt}\" command not found."; exit 1; } $ipt -P INPUT ACCEPT $ipt -P FORWARD ACCEPT $ipt -P OUTPUT ACCEPT $ipt -F $ipt -X $ipt -t nat -F $ipt -t nat -X $ipt -t mangle -F $ipt -t mangle -X $ipt -t raw -F $ipt -t raw -X
And run the script
sudo sh tablereset.sh
Add firewall rules, paste the code below one at a time:
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
Check the changes we made:
sudo iptables -L -n -v
Save the rules we made
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
Lets make this rules loaded automatically every time the system reboot.
sudo nano /etc/rc.local
Paste this code before "exit 0"
Let check the Firewall setting:Save and exitiptables-restore < /etc/iptables.ipv4.nat
Here is the Routing table in my Raspberryroute
pi@raspberrypi:~ $ route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default 192.168.2.1 0.0.0.0 UG 304 0 0 wlan1 192.168.1.0 * 255.255.255.0 U 0 0 0 eth0 192.168.2.0 * 255.255.255.0 U 304 0 0 wlan1
Let check if this works. Ensure your Raspberry pi is connected to your laptop Ethernet port and Raspberry pi is connected to a Wifi that has internet. Disconnect your laptop from any Wifi connection. Then browse any website and see if it works.
If you plan to connect your Rapsberry Pi Ethernet to a router, please consider these two important points:
- The Router IP address should not be 192.168.1.1
- The Router Dhcp service should be disabled
0 comments: