How to share your iPhone's internet to your local network (LAN) using a Raspberry Pi
How to share your iPhone's internet to your local network (LAN) using a Raspberry Pi. This project will make your Raspberry Pi your personalized home router that shares your iPhone internet (or any mobile device) to your Home Network so your Smart TV, Apple TV, Laptop and other devices can connect to the internet by wireless or by Ethernet cable.
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
- Raspberry Pi with NOOBS loaded
- iPhone hotspot 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 (iPhone hotspot name) and psk (wifi password)
Save and exitctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 country=GB network={ ssid="***iPhone hotspot name***" psk="***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 172.20.10.1 0.0.0.0 UG 303 0 0 wlan0 172.20.10.0 * 255.255.255.240 U 303 0 0 wlan0 192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
Let check if this works. Ensure your Raspberry pi is connected to your laptop Ethernet port and Raspberry pi is connected to your iPhone Hotspot. Disconnect your laptop from any Wifi connection. Then browse any website and see if it works.
Step 5: Configuring your router
Your router maybe different from mine. This step don't cover much details. But two thing you must do to your router:1. Dhcp should be disabled
2. It should not have IP the same as your Raspberry Pi.
If Raspberry Pi and Router has the same IP address (192.168.1.1), it is conflict and will not work. Also only one Dhcp service should run in the network. So ensure the router has no Dhcp running. Remember we configured the Raspberry Pi Dhcp service.
Once all are configure and checked, devices connected through Ethernet cable or through wifi should work.
-Mylinuxcode
0 comments: