# error setting static route [solved]

## snIP3r

hi all!

i have an error setting a static route on my gentoo testmachine. first interface enp1s0 is connected to the "normal" network 192.168.0.0/24 and the second to my "testnetwork" 192.168.1.1/24 with a dnsmasq dhcp.

my config looks like this:

```

config_enp1s0="192.168.0.25 netmask 255.255.255.0"

routes_enp1s0="default via 192.168.0.5"

dns_servers_enp1s0="192.168.0.5"

config_enp2s0="192.168.1.1 netmask 255.255.255.0"

routes_enp2s0="default via 192.168.0.25"

```

but on boot/reboot, the second interface does not start. if restarting it manually i get this:

```

/etc/init.d/net.enp2s0 restart

 * Bringing up interface enp2s0

 *   192.168.1.1 ...                                                                                  [ ok ]

 *   Adding routes

 *     default via 192.168.0.25 ...

Error: Nexthop has invalid gateway.                                                                   [ !! ]

```

but then the interface is up and running:

```

 ifconfig

enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 192.168.0.25  netmask 255.255.255.0  broadcast 192.168.0.255

        inet6 fe80::4770:d116:30e5:bf2  prefixlen 64  scopeid 0x20<link>

        ether 00:25:22:44:47:b5  txqueuelen 1000  (Ethernet)

        RX packets 403  bytes 37234 (36.3 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 486  bytes 117148 (114.4 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp2s0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500

        inet 192.168.1.1  netmask 255.255.255.0  broadcast 192.168.1.255

        ether 00:08:54:56:60:83  txqueuelen 1000  (Ethernet)

        RX packets 0  bytes 0 (0.0 B)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 0  bytes 0 (0.0 B)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

```

i have no idea what else to configure. packet forwarding is on 

```

net.ipv4.ip_forward = 1

```

any ideas?

thx in advance

snIP3r

----------

## mike155

You cannot set two default routes. Please note that IPv4 standard routing rules (the routes you manage with 'route') are NOT attached to specific interfaces. There is a global routing table, and you can define only one default route.

----------

## NeddySeagoon

snIP3r,

```
$ route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

default         router          0.0.0.0         UG    2      0        0 eth0

loopback        0.0.0.0         255.0.0.0       U     0      0        0 lo

loopback        localhost       255.0.0.0       UG    0      0        0 lo

192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
```

the kernel matches outgoing packets against the routing table entries from the bottom, working its way up.

At the bottom, it says to reach  192.168.100.0/24 no gateway is required, just send the packet out of eth0.

Lets ignore loopback.

Any packets that reach the default are sent to a gateway called router that is reached via eth0.

Were there more routes, further up the routing table, the could not be reached. Default matches everything. 

```
config_enp2s0="192.168.1.1 netmask 255.255.255.0"

routes_enp2s0="default via 192.168.0.25" 
```

enp2s0 is in the 192.168.1.0/24 subnet. It cannot reach 192.168.0.25 so its not useful to try to define a static route there out of enp2s0.

If the route had been created and used, you wouldn't like the result.

----------

## snIP3r

thx for the explanation!

i totally forgot to do some iptables magic to do the job:

```

iptables -A POSTROUTING -s 192.168.1.0/24 -o enp1s0 -j MASQUERADE

```

and everything is ok now...

----------

