3

I used to add static routes up until debian 9 this way:

up route add -net 1.2.3.4.5/23 gw 2.3.4.5.6
up route add -host 2.3.4.5 gw 3.4.5.6

What changed for debian10 and what's the new syntax for static routes in the /etc/network/interfaces file?

3 Answers 3

8

The up ... lines are not stand-alone, but they are extensions of an iface ... line before them. Before Debian 9, the actual network interface used to pretty much always be the last entry in /etc/network/interfaces, so just adding up route add ... lines at the end might have actually worked pretty often.

If you chose to install a desktop environment, the installation is likely to include NetworkManager, and in that case, there may be no iface line for your network interface at all, allowing the interface to be controlled by NetworkManager instead. In that case, you could use one-time nmcli commands to persistently add new routes:

 nmcli c modify eno1 +ipv4.routes "1.2.3.4/23 2.3.4.5" # network route
 nmcli c modify eno1 +ipv4.routes "2.3.4.5 3.4.5.6" # host route

And if you don't use NetworkManager... the net-tools package that includes the old ifconfig and route commands has been deprecated since Debian 9, and is no longer installed by default. So unless you have explicitly chosen to install net-tools, you should use the newer ip route commands instead:

iface eno1 ...
    up /bin/ip route add 1.2.3.4/23 via 2.3.4.5   # network route
    up /bin/ip route add 2.3.4.5/32 via 3.4.5.6   # single host route
4
  • Thank you for the answer, any idea why the routes don't apply after a reboot?
    – thpetrus
    Feb 5, 2020 at 12:00
  • Are you sure you have not made any typos, e.g. specified a gateway IP that is not reachable directly with the IP/netmask configuration of that interface? Are you using NetworkManager or not, and if not, have you made sure NetworkManager is definitely disabled? In /etc/network/interfaces, you may need to use an absolute path: up /bin/ip ... instead of just up ip ....
    – telcoM
    Feb 5, 2020 at 12:09
  • using nmcli you might want to use nmcli connection show to get your value of eno1 above.
    – Archemar
    Nov 12, 2020 at 15:03
  • A bit late to the party, but did you find a solution? I found that when using dhcp the up command is not executed. Feb 10, 2022 at 14:07
3

This works for debian 9 (debian stretch), debian 10 (debian buster) and debian 11 (debian bullseye):

Edit /etc/network/interfaces file and add your permanent routes.

Example:

auto ens192
allow-hotplug ens192
iface ens192 inet static
    address 192.168.221.54/24
    gateway 192.168.221.1
    dns-nameservers 82.99.137.41 212.158.133.41
    dns-search secar.cz
    up ip route del 192.168.0.0/24 via 192.168.221.1 dev ens192 
    up ip route add 192.168.0.0/24 via 192.168.221.1 dev ens192
    up ip route del 192.168.1.0/24 via 192.168.221.1 dev ens192
    up ip route add 192.168.1.0/24 via 192.168.221.1 dev ens192

Here is completed guide for permanent debian static routes

3
  • I could not find the route command on my Debian 11, I wonder, has it been moved to be a function of ip now?
    – Sam Sirry
    Feb 13, 2022 at 5:05
  • I don't use the route command anywhere in my tutorial... Feb 15, 2022 at 9:45
  • In the example above there's ip route add. This used to be done with the route command by itself. Now I see it has been integrated as a function of ip
    – Sam Sirry
    Feb 16, 2022 at 1:24
0

I solve the problem in /etc/systemd/network/, here i put all network configuration!

Replace or comment if exists: dhcp.network file for static.network

[Match]
Name=e*

[Network]
Address=aaa.aaa.aaa.aaa/aa  (MY IP)
Gateway=ccc.ccc.ccc.ccc         (Default Gateway)
DNS= 8.8.8.8
DNS= 8.8.4.4


[Route]
Destination=yyy.yyy.yyy.yyy/yy
GatewayOnlink=true
Gateway=zzz.zzz.zzz.zzz
Metric=1


[Route]
Destination=xxx.xxx.xxx.xxx/xx
GatewayOnlink=true
Gateway=xxx.xxx.xxx.xxx
Metric=1

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.