Raspberry Pi Ethernet to WiFi

This tutorial is about using your Raspberry Pi (or any Linux box) as WiFi router.

Setup
I am using Raspberry Pi 2 with Raspbian Strech. Edimax WiFi adapter. Internet enabled Ethernet connection.

Check Ethernet
Before continuing make sure the Ethernet cable is connected in and you can ping out from the Pi

try ping

ping 8.8.8.8

Install Software
You’ll need DHCP server. We’ll use isc-dhcp-server in this tutorial but you can use dnsmasq as well.

sudo apt-get install isc-dhcp-server

Backup original configuration file and create new one.

sudo mv /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.orig
sudo nano /etc/dhcp/dhcpd.conf

Copy below options in config file.

ddns-update-style none;
authoritative;

subnet 10.1.1.0 netmask 255.255.255.0 {
	range 10.1.1.10 10.1.1.100;
	option subnet-mask 255.255.255.0;
	option broadcast-address 10.1.1.255;
	option routers 10.1.1.1;
	default-lease-time 600;
	max-lease-time 7200;
	option domain-name "local";
	option domain-name-servers 8.8.8.8, 8.8.4.4;
}

Open file

sudo nano /etc/default/isc-dhcp-server

search INTERFACESv4= and copy your network adapter name in here like:

INTERFACESv4="wlan0"

Need to assign manual IP to wlan0

sudo nano /etc/network/interfaces
allow-hotplug wlan0
iface wlan0 inet manual

Add static IP

sudo nano /etc/dhcpcd.conf
interface wlan0
static ip_address=10.1.1.1/24
static routers=10.1.1.1
static domain_name_servers=8.8.8.8

Install hostapd

sudo apt-get install hostapd

Configure hostapd

sudo nano /etc/hostapd/hostapd.conf
interface=wlan0
ssid=RPIAP
country_code=US
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=raspberry
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
wpa_group_rekey=86400
ieee80211n=1
wme_enabled=1
sudo nano /etc/default/hostapd

Search DAEMON_CONF= and give it configuration file name

DAEMON_CONF=/etc/hostapd/hostapd.conf

Test configuration by running

sudo hostapd /etc/hostapd/hostapd.conf

Remove WPA Supplicant so it won’t connect to any WiFi network

sudo mv /usr/share/dbus-1/system-services/fi.epitest.hostap.WPASupplicant.service ~/

IP related configuration

sudo nano /etc/sysctl.conf

Enable IP forwarding by uncommenting below line

net.ipv4.ip_forward=1

Disable IPv6 by adding following lines to the end

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1

Run the following commands to create the network translation between the ethernet port eth0 and the wifi port wlan0

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

Make routes persistent by saving in a file and loading up at boot time.

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

Open rc.local

sudo nano /etc/rc.local

Add below line just above exit 0

iptables-restore < /etc/iptables.ipv4.nat

Reboot device

sudo reboot

Now it should broadcast SSID RPIAP, connect and check Internet.

If there is some issues check DHCP Server status

sudo service isc-dhcp-server status

If there are errors then try to add a delay in DHCP service start

sudo nano /etc/init.d/isc-dhcp-server

Search for start) and add Sleep 15 after this line.

Leave a Reply

Your email address will not be published.