0

I have two network interface in my linux pc. One is eth0, another is eth1. here is netplan yaml file :

network:
version: 2
renderer: NetworkManager
ethernets:
  eth0:
    addresses: [192.168.20.3/24]
    gateway4: 192.168.20.1
    nameservers:
      addresses: [192.168.10.1]
    routes:
    - to: 224.100.100.0/24
      via: 192.168.20.1

  eth1:
    addresses: [192.168.20.3/24,192.168.10.8/24]
    nameservers:
      addresses: [192.168.10.1]

eth1 and eth0 have same network segement 192.168.20.x

ther is a sensor which ip is 192.168.20.13, but i can not ping it ok, I can and a route command :

route add -host 192.168.20.13 dev eth1

it can ping successfully.

My question is how can I set the rule in the netplan yaml file?

4
  • os is ubuntu 18.04 – Arthur George Sep 1 '20 at 9:28
  • Although your question is about translating route commands to netplan, your network itself seems to be configured very suboptimally: using the same 192.168.20 on both network segments suggests a network misconfiguration. It's highly advisable to use different network addresses on each network segment, to avoid complicated static configurations. – slangasek Sep 1 '20 at 18:06
  • that said, see below for a configuration that should work. – slangasek Sep 1 '20 at 18:32
  • @slangasek I agree with the idea, this is the problem of the overall design of the system network segment in the early stage – Arthur George Sep 3 '20 at 2:44
0

Here is a configuration that should work. (Please note the indentation, which was incorrect in your question but presumably correct on-disk on your system.)

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    eth0:
      addresses: [192.168.20.3/24]
      nameservers:
        addresses: [192.168.10.1]
      routes:
      - to: 0.0.0.0/0
        via: 192.168.20.1
    eth1:
      addresses: [192.168.20.3/24,192.168.10.8/24]
      nameservers:
        addresses: [192.168.10.1]
      routes:
      - to: 192.168.20.13/32
        via: 192.168.20.3

Differences from your original config:

  • remove the static route to 224.100.100.0, which was redundant with your default route.
  • replace the use of gateway4 with a routes entry, which is clearer
  • add a host route for the sensor on eth1.

It is recommendable that you not use the same network address on different segments on your network. This is contrary to basic principles of routed networks.

3
  • thanks for your replay slangasek, routes: - to: 192.168.20.13/32 via: 192.168.20.3 solve my problem. But there is another problem, "replace the use of gateway4 with a routes entry, which is clearer" "routes: - to: 0.0.0.0/0 via: 192.168.20.1" can't work. When I use "gateway4: 192.168.20.1", when I type route -n , result is "Destiination:0.0.0. Gateway: 192.168.20.1 Genmask : 0.0.0.0" , But when I use "routes: - to: 0.0.0.0/0 via: 192.168.20.1" result is "Destiination:0.0.0. Gateway: 192.168.20.1 Genmask : 255.255.255.0", – Arthur George Sep 3 '20 at 2:38
  • It caused some problems, such as unable to ping the external network, which is connected to the gateway of 192.168.20.1 – Arthur George Sep 3 '20 at 2:41
  • I am unsure why this didn't work for you. You are showing a route with a /24 netmask, not the /0 netmask specified in the example. I have verified here that to: 0.0.0.0/0 works as expected using netplan in focal. – slangasek Sep 4 '20 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.