0

I have a raspberry connected to a pc via ethernet. The pc's ip is 172.16.0.2 that can't be changed, so i had to give the rpi's eth0 172.16.0.1 so they can communicate.

Meanwhile the rpi's wlan0 is directly linked to the router with a 192.168.1.2 ip.

Now i want the pc to be able the access the internet. i tried editing the /etc/dhcpcd.conf file like this:

interface eth0
static ip_address=172.16.0.1/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1 8.8.8.8 fd51:42f8:caae:d92e::1

but it didn't work.

Hbib
  • 43
  • 2
  • 7

1 Answers1

3

Your Raspberry Pi has two interfaces: eth0 (172.16.0.1) and wlan0 (192.168.1.2) each with an ip address from another subnet. This is a very good condition to make your raspi a router. First you have to enable ip forwarding. There are several ways to do it. You can enable it direct to the kernel with:

rpi ~$ echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward

Or you can uncomment it in /etc/sysctl.conf and reboot:

rpi ~$ grep -B 1 'ipv4.ip_forward' /etc/sysctl.conf
# Uncomment the next line to enable packet forwarding for IPv4
#net.ipv4.ip_forward=1

Or with systemd-networkd you can add IPForward=yes to the [Network] section in your /etc/systemd/network/eth0.network file.

Then you have to set a static route in your internet router so it can find the route over the raspi to your pc. On most internet router you can set a static route but how to do that varies from model to model. It's up to you to find it out. On a Raspberry Pi it would look like this (don't set it on your Raspi router!)

rpi ~$ sudo ip route add 172.16.0.0/24 via 192.168.1.2 dev wlanX

That means for the internet router: "send all packets belonging to subnet 172.16.0.0/24 (destination network) to the next router on my subnet, your raspi-router 192.168.1.2 (gateway). It knows where to go on."

For troubleshooting and reference you can look at Using the Raspberry Pi as a Router.

Ingo
  • 42,961
  • 20
  • 87
  • 207