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:
  • Raspberry Pi 3
  • Noobs updated
What you'll need:
  • 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:
sudo nano /etc/dhcpcd.conf
Add this on the last line
denyinterfaces eth0
Save and exit (Press ctrl + o to save and ctrl +x to exit)

Open interfaces by typing:
sudo nano /etc/network/interfaces

and add this code on the last:
auto 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
Save and exit

STEP 2: CONFIGURING DHCP service (dnsmasq)
We will configure the DHCP service so your Raspberry Pi will automatically assign IP for every device plugged in the Ethernet Port.
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig  
sudo nano /etc/dnsmasq.conf

Add this code
interface=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
Save and exit

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.
auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
Save and exit

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)
ctrl_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
}
Save and exit

Reboot your Raspberry pi
sudo reboot

Check if your Wifi is connected and has internet connection
sudo apt-get update
If it download and update, it means you have internet

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 #
net.ipv4.ip_forward=1
And lets activate it
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:
#!/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
Save and exit

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"
iptables-restore < /etc/iptables.ipv4.nat 
Save and exit
Let check the Firewall setting:
route
Here is the Routing table in my Raspberry
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:
  1. The Router IP address should not be 192.168.1.1
  2. The Router Dhcp service should be disabled
If Raspberry Pi and Router has the same IP address, it will not work. Also only one Dhcp service should run in a network. So ensure the router has no Dhcp running. Remember we configured the Raspberry Pi Dhcp service.



0 comments:

Copyright © 2012 My Linux Code