brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ip route add sends one subnet through another gateway until the next reboot

Linux
~/tips/tip-linux-ip-route-add-temporary

ip route add 198.51.100.0/24 via 192.0.2.1

Route a single network via a different router without touching your config files.

Sometimes one network has to go through a different router than everything else, for example a lab subnet behind a second box. You can test that route live before you write it into netplan or NetworkManager.

ip route add 198.51.100.0/24 via 192.0.2.1
ip route show

Traffic for 198.51.100.0/24 now goes to 192.0.2.1, and the rest still uses your default gateway. The gateway has to be reachable on one of your directly connected networks, or ip refuses with Nexthop has invalid gateway.

When you want to point the same subnet somewhere else, replace saves you a delete first:

ip route replace 198.51.100.0/24 via 192.0.2.2
ip route del 198.51.100.0/24

replace also creates the route when it does not exist yet, which makes it handy in scripts that may run twice. To check which way a single address really goes, ip route get 198.51.100.7 answers with the route the kernel picks.

I like this order: add the route by hand, test it, and only then make it permanent. A route added with ip disappears at the next reboot, and NetworkManager or systemd-networkd may also drop it the next time they reconfigure the interface. Once it works, put it in the config of whatever manages your network, so the two never disagree.

Further reading: ip-route(8) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link