-1

I have a computer (RPi CM4) with two NICs. I want one (eth0)set up on network 1 and the other (eth1) on network 2. I need to be able to see computer on net2 from net1. I have eth0 set for 192.168.1.x/23, Gateway 192.168.1.1 I have eth1 set for 192.168.0.x With just eth0 connected, I can ssh and ping 192.168.1.x, but when I connect eth1, I cannot.

ip r gives:

 default via 192.168.1.1 dev eth0 src 192.168.1.224 metric 202 default via 192.168.1.1 dev eth1 src 192.168.0.200 metric 203 192.168.0.0/24 dev eth1 proto dhcp scope link src 192.168.0.200 metric 203 192.168.0.0/23 dev eth0 proto dhcp scope link src 192.168.1.224 metric 202 

route gives:

 Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default 192.168.1.1 0.0.0.0 UG 202 0 0 eth0 default 192.168.1.1 0.0.0.0 UG 203 0 0 eth1 192.168.0.0 0.0.0.0 255.255.255.0 U 203 0 0 eth1 192.168.0.0 0.0.0.0 255.255.254.0 U 202 0 0 eth0 

ip addr

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether e4:5f:01:dc:d3:ca brd ff:ff:ff:ff:ff:ff inet 192.168.1.224/23 brd 192.168.1.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::8b50:c7e0:6f8d:a2aa/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 84:8b:cd:4a:a1:ee brd ff:ff:ff:ff:ff:ff inet 192.168.0.200/24 brd 192.168.0.255 scope global noprefixroute eth1 valid_lft forever preferred_lft forever inet6 fe80::7e0e:d55b:12a1:f029/64 scope link valid_lft forever preferred_lft forever 

I am a total networking dunce. Trying to read similar questions, but I am still confused.

ELIA5, what do I have to do to route 192.168.0.x so it can be seen by 192.168.1.x?

main network/inet (192.168.1.x) <-> CM4 eth1(192.168.1.200) eth0(192.168.0.60) <-> private LAN(192.168.0.50) 
5
  • 1
    Why do you have 192.168.0.0/23? This makes 192.168.0.x and 192.168.1.x belong to the same network. For routing you need different networks. Draw a diagram of your network including the IP addresses of the systems. Show the output of ip addr. Please edit your question to provide requested information od clarification. Commented Jun 7, 2023 at 17:11
  • @Bodo I set the IP of eth0 to 192.168.1.200/23. I set eth1 to 192.168.0.60, didn't set a mask. No idea what 192.168.0.0/23 is. My question here is why is it 192.168.0.0 at all? What I really need at this point is just to be able to connect to both, don't even care if they can talk to each other. I just need to be able to ssh into it on eth0 and have it see the device on eth1. Commented Jun 7, 2023 at 18:35
  • It would help if you describe your problem more thoroughly. Basically you want to setup a router. Without further information (network layout etc.) nobody really can help you. Commented Jun 8, 2023 at 5:26
  • Here is what I am attempting: I have a PLC (192.168.0.61) that collects production data. I pull that data from the PLC on the CM4 eth1(192.168.0.200) and write it to an .xlsx file. Every night at midnight, I transfer the file to a directory on the plant network through eth0(192.168.1.224, netmask 255.255.254.0, gw 192.168.1.1) If I have just the connection from CM4/eth1 to the PLC, I can pull the data and save to the .xlsx file. If I have just the connection from CM4/eth0, I can move the file to the target directory. If both connections are made, I can't do either. Commented Jun 8, 2023 at 20:09
  • Please edit your question and add all requested information or clarification to the question instead of using comments for this purpose. If you don't have an idea what /23 is, then try to configure your network cards and routing without it. Show/describe what settings or configuration files exactly you used to configure your network. Commented Jun 11, 2023 at 16:16

1 Answer 1

0

Background

A netmask (or just "mask" for short) defines how to split an IP address into a network part and a host part. The IP protocol assumes by default that a network interface will be able to directly contact a remote host if the remote host's IP address and the interface's IP address both have a matching network part. If the network part is different, then the system needs to check its routing table to figure out what to do.

An IPv4 netmask is fully expressed as a 32-bit value which has all 1-bits starting from the most significant bit until some bit position, then all 0-bits. The 1-bits indicate the corresponding bit of the IP address is part of the network part of the address; 0-bits indicate the host part, respectively. As a result, it is convenient to describe a netmask by just the number of 1-bits it has; this can be properly known as netmask length, but often is referred just as "mask" for brevity. So, a mask of /24 is fully written out as 255.255.255.0 in the normal base-10 IP address format, or 11111111.11111111.11111111.00000000 in binary.

If the netmask length is divisible by 8, it makes the separation of the network part and host part align with the base-10 representation of the IP address, which makes it easy for humans: for example, 192.168.0.251/24 means an IP address where the network part is 192.168.0 and the host part is .251.

Netmasks that are not divisible by 8 require a tiny bit of binary math to understand. But in a nutshell, 192.168.0.0/23 indicates a network whose first address is 192.168.0.0 and the last address is 192.168.1.255, so one of your networks would be a subnetwork of the other. On the other hand, your ip addr indicates both eth0 and eth1 have a mask of /24 configured. The routing table entries with no gateway (in route output) or via x.x.x.x (in ip r) should match the configuration of the network interfaces, so there must be a typo or some other error somewhere. The ip addr output you copied is not consistent with the ip r/route outputs.


A routing table is processed starting from most specific entries (= those with the largest netmask) towards the more generic ones (smaller netmask values). A default gateway entry is the most generic one, matching every address that has not been matched by any other entries. Technically it can be expressed as an all-zeroes IP address with a mask length of /0.

In human terms, a default gateway entry in the routing table is "if you can't directly reach some address and there isn't any more specific instructions, send it to this directly-reachable system; it will know what to do with it."

When you configure an IP address and netmask to a network interface, the Linux kernel will automatically generate a routing table entry that matches the hosts directly reachable by that interface, according to the netmask. So if your system has two network interfaces plugged to different network segments, and no Internet connection, then that system needs no manual route entries at all, and no default gateway either. But the other hosts in those two networks will need a routing table entry to know that the "other" network can be reached by routing through the 2-interface host.

If there's just the locally-reachable network and the other network, a default gateway entry pointing at the 2-interface host would suffice; but if there is also an Internet connection involved, the default gateway needs to point at your Internet router (because all the other network segments of the world are reachable through it and you most definitely don't want to enumerate them one by one) and if the resulting default gateway entry does not point to your 2-interface host, you'll also need a specific route entry that tells the system "my other network segment has this network address and this netmask, and can be reached through the 2-interface host".


What you'll need to do

Assuming that your ip addr output is the correct one and you have two distinct network segments, one 192.168.0.x/24 and another 192.168.1.x/24, you need to do four things to enable routing between them:

1.) Enable IPv4 routing master switch on the CM4.

echo "net.ipv4.ip_forward=1" >>/etc/sysctl.conf sysctl -p 

2.) Ensure that the firewall rules on the CM4 allow forwarding, i.e. iptables -L FORWARD -vn either has policy ACCEPT and no rules, or suitable ACCEPT rules in place if the policy is DROP. Some distributions set the forwarding policy to DROP by default. If that's the case, you'll need two rules:

iptables -I FORWARD 1 -s 192.168.0.0/24 -d 192.168.1.0/24 -j ACCEPT iptables -I FORWARD 2 -s 192.168.1.0/24 -d 192.168.0.0/24 -j ACCEPT 

In human terms: packets incoming from 192.168.0.x with a destination in 192.168.1.x will be accepted for forwarding (routing), and vice versa.

To make these rules persistent, you could e.g. run iptables-save and redirect the output to a file appropriate for your distribution. If your system is using ufw or some other firewall configuration tool, use that instead to configure equivalent persistent rules.

3.) On the hosts in the 192.168.0.x private LAN: set their default gateway to 192.168.0.60. Since there is no Internet gateway in that segment, that's the only route entry they'll need. (If you later want to enable outgoing Internet access from the private LAN, you will be able to do it by just changing the iptables FORWARD rules on the CM4.)

4.) On the hosts in the 192.168.1.x main network: these should already have a default gateway setting of 192.168.1.1, and if you change it, these hosts will lose their Internet connectivity. So here, you'll need to define a specific route that says "the network 192.168.0.x with mask /24 will be reachable by routing through 192.168.1.200".

With Linux configuration commands, this is either:

ip route add 192.168.0.0/24 via 192.168.1.200 

or

route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.1.200 

Other operating systems may need a slightly different syntax.

1
  • Sorry for being a dunce. I had the wrong info. Didn't realize I had to have both connections made. And had the wrong ip addr info from a prior configuration. Commented Jun 8, 2023 at 20:30

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.