set route on linux

some linux route operations

linux route

add static route

  • add default static route
1
2
3
4
5
6
7
8
9
# Permanent
echo "any net 0.0.0.0/0 gw 110.188.40.1" >> /etc/sysconfig/static-routes
# Temporary
ip route add default dev vlan7
ip route add default via 110.188.40.1
ip route add default via 110.188.40.1 dev vlan7
ip route add 0.0.0.0/0 dev vlan7
ip route add 0.0.0.0/0 via 110.188.40.1
ip route add 0.0.0.0/0 via 110.188.40.1 dev vlan7
  • add specific net static route
1
echo "any net 110.188.40.0/24 gw 110.188.40.1" >> /etc/sysconfig/static-routes

delete route

1
2
3
4
5
# delete default route
route del default gw 110.188.40.1
ip route del default via 110.188.18.1 dev vlan16
# delete a non-default route
ip route del 110.188.18.0/24 via 110.188.18.1 dev vlan16

replace route

1
2
3
4
5
# work well after every network restart
# replace if exists, or add
echo "ip route replace default via 110.188.40.1 dev vlan7" >> /sbin/ifup-local
chmod +x /sbin/ifup-local
systemctl restart network

change route

1
2
# change some params of existing route
ip route change 192.192.13.1/24 dev ens32
Licensed under CC BY-NC-SA 4.0
Last updated on Mar 25, 2020 20:23 UTC