You are not logged in.

#1 2015-04-08 10:22:13

anthillsocial
Member
Registered: 2013-02-18
Posts: 136

[Solved] Share internet from (laptop) wifi to (RaspberryPi) ethernet

Hi,

I've been attempting to share the internet from my laptop wifi connection to a raspberry pi (with arch) connected via an ethernet port.
For some reason, the Rpi doesn't connect. I've closely followed the instructions at:
https://wiki.archlinux.org/index.php/Internet_sharing

Yet the following script doesn't seem to work. The Rpi recognizes the the Ethernet connection but dhcpcd isn't dishing out an IP address. When I plug the Rpi into an ethernet port on my router it connects fine so as far as i'm understanding it should 'just work'.

#!/bin/bash
DEVICE="enp0s25"
ip link set $DEVICE down
ip link set $DEVICE up
ip addr add 192.168.123.1/24 dev $DEVICE 
sysctl net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -o internet0 -j MASQUERADE
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i $DEVICE -o internet0 -j ACCEPT
dhcpcd $DEVICE

Any tips for getting to the bottom of the issue?
Tom

Last edited by anthillsocial (2015-04-08 12:03:14)

Offline

#2 2015-04-08 10:56:40

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: [Solved] Share internet from (laptop) wifi to (RaspberryPi) ethernet

You're a bit confused there. First you're setting a static ip:

ip addr add 192.168.123.1/24 dev $DEVICE

then later you're running dhcpcd:

dhcpcd $DEVICE

Pick one - I'd suggest static myself, so you don't have to set up a dhcp server on your laptop.

Also, do everything manually until it's working as intended, then put it in your script.

Offline

#3 2015-04-08 11:16:31

anthillsocial
Member
Registered: 2013-02-18
Posts: 136

Re: [Solved] Share internet from (laptop) wifi to (RaspberryPi) ethernet

Thanks for the input. Ideally I would temporarily run a dhcp server on my laptop as I will be working with lots of raspberry pi's which I wont be able to set a static IP on. The script I provided above is run from my laptop so doesn't:

 dhcpcd enp0s25 

Create the dhcp server on my laptop? Or am I misunderstanding dhcpcd? And doesn't the laptop require a static IP address so it acts as a router for the Rpi?

I'm definite confused as I've used a similar script in order to connect to a beagle bone black & it all seemed to work. So any help in untangling my brain would be much appreciated!

Offline

#4 2015-04-08 11:19:07

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [Solved] Share internet from (laptop) wifi to (RaspberryPi) ethernet

You want dhcpd then, not dhcpcd.

Offline

#5 2015-04-08 12:02:44

anthillsocial
Member
Registered: 2013-02-18
Posts: 136

Re: [Solved] Share internet from (laptop) wifi to (RaspberryPi) ethernet

Success!! I'd mistakenly thought that dhcpcd acted as both client and server so that's where I went wrong. The following script now works using dhcpd. As a reference for others, here's my setup:

#!/bin/bash
DEVICE="enp0s25"
ROUTERIP=139.96.30.1 # 192.168.123.1
ip link set $DEVICE down
ip link set $DEVICE up
ip addr add $ROUTERIP/24 dev $DEVICE # arbitrary address
sysctl net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -o internet0 -j MASQUERADE
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i $DEVICE -o internet0 -j ACCEPT
#Config file: /etc/dhcpd.conf
#Database file: /var/lib/dhcp/dhcpd.leases # cat this to see if a device has been asigned a lease
#PID file: /var/run/dhcpd.pid
dhcpd $DEVICE 

And /etc/dhcpd.conf contains:

option domain-name-servers 8.8.8.8, 8.8.4.4;
option subnet-mask 255.255.255.0;
option routers 139.96.30.100;
subnet 139.96.30.0 netmask 255.255.255.0 {
      range 139.96.30.150 139.96.30.250;
}

Then to find out the IP address assigned to the Rpi, simply

cat /var/lib/dhcp/dhcpd.leases

Thanks for the help smile

Offline

Board footer

Powered by FluxBB