Tuesday, 26 September 2017

Sharing Wireless internet connection on a PC running Ubuntu with the Raspberry pi connected to the PC via Ethernet cable

Sharing Wireless internet connection on a PC running Ubuntu with the Raspberry pi connected to the PC via Ethernet cable.


Laptop running Ubuntu.

Laptop connected to Internet via Wireless LAN.

Raspberry pi and Laptop connected using a ethernet cable. (Cable used to connect a PC to wired LAN. Not a special cable)

Raspberry pi running - RASPBIAN STRETCH LITE (Minimal image based on Debian Stretch)

Steps:

Raspberry pi

1. Enable ssh on raspberry pi. Google for the instructions depending on if you boot headless or have connected the pi with any input (keyboard) / output (TV / Monitor) device.
2. Confirm that DHCP client is running on Raspberry pi. By default the RASPBIAN STRETCH LITE has DHCP client running / listening on ethernet port.

Laptop

3. Install DHCP server.
$ sudo apt-get install isc-dhcp-server

4. Update the DHCP configuration with the IP range of your choice for your pi and laptop network.  Change the file mentioned below. Will need root access on Laptop.
$ cat /etc/dhcp/dhcpd.conf
subnet 17.1.10.0 netmask 255.255.255.224 { *****Network subnet *****
  range 17.1.10.3 17.1.10.30; *****Range of IP address that will be allocated by DHCP*****
  option domain-name-servers 208.67.222.222; *****Open DNS server*****
  option subnet-mask 255.255.255.224;
  option routers 17.1.10.1; *****IP address for the Laptop*****
  option broadcast-address 17.1.10.31;
  default-lease-time 600;
  max-lease-time 7200;
}

5. Set DHCP server to serve requests on the ethernet interface (device). Change the file mentioned below. Will need root access on Laptop.
$ cat /etc/default/isc-dhcp-server
INTERFACES="eth0"

UPDATE-
ON UBUNTU the if command does not work anymore. In order to assign a static IP address use the following--

Helpful names of the kernal interfaces like eth* have changed!!!

Following shows the entwork hardware and new names.
$sudo lshw -class network

$sudo ip addr add 17.1.10.1/24 dev enp0s25

If a static could not be assigned to laptop, the DHCP server deamon fails with error 

"No subnet declaration for <ethernet device> (no IPv4 addresses)."

The ethernet device is mentioned in the /etc/default/isc-dhcp-server. This file should have the appropriate name as per Ubuntu!

6. Set static IP.
$ cat /etc/network/interfaces
auto lo eth0
iface lo inet loopback
iface eth0 inet static 
address 17.1.10.1
netmask 255.255.255.0

UPDATE-
ON UBUNTU the if command does not work anymore. In order to assign a static IP address use the following--

Helpful names of the kernal interfaces like eth* have changed!!!

Following shows the entwork hardware and new names.
$sudo lshw -class network

$sudo ip addr add 17.1.10.1/24 dev enp0s25

If a static could not be assigned to laptop, the DHCP server deamon fails with error 

"No subnet declaration for <ethernet device> (no IPv4 addresses)."

The ethernet device is mentioned in the /etc/default/isc-dhcp-server. This file should have the appropriate name as per Ubuntu!

7. Set IP address forward.
$sudo sysctl net.ipv4.ip_forward=1

8. Set IP forwarding in IPTables.
$sudo /sbin/iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
$sudo /sbin/iptables -A FORWARD -i wlan0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
$sudo /sbin/iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT

Set up wise thats it.

Commands to - 
Check IP address assigned to pi.
laptop$ nmap -sP 17.1.10.1/29

SSH to pi.
laptop$ ssh pi@<Ip address for the pi>

Check the route on pi or on laptop
laptop$ ip route

Check if domain resolution works fine.
pi$ ping www.google.com

Check the route to internet
pi$ traceroute www.google.com

No comments: