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
    The short answer is yes, you can do this via `ip route`. I don't use normal network configs though, so you'll have to wait for someone to come along with that knowledge. In the meantime I'd recommend you explain the context of *why* you want to do this, since it will help clarify things. Some people won't risk wasting their time providing answers to ambiguous questions only to find out "Oh, that's not what I meant...", etc. And they may not bother trying to get the information out of you if you cannot be bothered to provide it in the first place ;) – goldilocks May 05 '18 at 13:19
  • ok thank you, i've changed the description, now should be more understandable – Hbib May 05 '18 at 13:46

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
  • 40,606
  • 15
  • 76
  • 189