Overview:
Static routes are manually configured paths that instruct a device on how to reach specific networks. They are particularly useful in small networks, offering enhanced control and security, or as a backup to dynamic routes.
Static routes can be used for various setups, such as connecting different subnets, routing VPN traffic, or ensuring traffic takes a specific path.
Below you'll find guides for setting up a static route on your server depending on the type of operating system you are using.
The following examples will use the IP addresses 192.51.100.10
and 192.51.100.42
, please make sure to change the examples with your own IP addresses.
Table of contents:
You will need the following information to setup a static route:
- The IP addresses.
- The subnets.
- The gateways.
If you're not sure how to find this info, you can follow our guide here.
How do I setup a static route on CentOS?
- Open the network configuration file. If you use eth0, the file is called route-eth0 and located under /etc/sysconfig/network-scripts/.
- Add the below entry to setup a static server from 192.51.100.10 to 192.51.100.42:
#/etc/sysconfig/network-scripts/route-eth0
...
192.51.100.42/32 via 192.51.100.1 dev eth0
- Add the below entry to setup a route from 192.51.100.42 to 192.51.100.10:
#/etc/sysconfig/network-scripts/route-eth0
...
192.51.100.10/32 via 192.51.100.1 dev eth0
- Alternatively, both servers can have a single route added to the whole /24 subnet by adding the following:
#/etc/sysconfig/network-scripts/route-eth0
...
192.51.100.0/24 via 192.51.100.1 dev eth0
How do I setup a static route on Debian/Ubuntu(Until version 17.04)?
- Open the network configuration file /etc/network/interfaces. Entries are added to the end of the file or under the iface sections of the respective interface. Assuming the interface to be eth0, the entry on server 192.51.100.10 would look like this:
#/etc/network/interfaces
...
up ip route add 192.51.100.42/32 via 192.51.100.1 dev eth0
down ip route del 192.51.100.42/32 via 192.51.100.1 dev eth0
- On server 192.51.100.42, this would be the static route to 192.51.100.10:
#/etc/network/interfaces
...
up ip route add 192.51.100.10/32 via 192.51.100.1 dev eth0
down ip route del 192.51.100.10/32 via 192.51.100.1 dev eth0
- It is also possible to add a static route to the whole /24 on both servers instead with the following configuration:
#/etc/network/interfaces
...
up ip route add 192.51.100.0/24 via 192.51.100.1 dev eth0
down ip route del 192.51.100.0/24 via 192.51.100.1 dev eth0
How do I setup a static route on Ubuntu (Version 17.10 and above)?
- Open the network configuration file /etc/netplan/01-netcfg.yaml.
- On the server with the IP 192.51.100.10, the entry has to be added in the correct indentation to the network interface, which in our example is eth0. You would to add the two routes shown below for each server IP.
#/etc/netplan/01-netcfg.yaml
...
eth0:
routes:
- to: 192.51.100.0/25
via: 192.51.100.1
- to: 192.51.100.128/25
via: 192.51.100.1
- Finally, reset the routing table and to apply the changes. Please use the whole command at once to prevent a network connection loss:
ip route flush table main; ip route flush cache; netplan apply
How do I setup a static route on openSUSE?
- Open the network configuration file /etc/sysconfig/network/routes.
- Add the following to setup a static route from 192.51.100.10 to 192.51.100.42:
#/etc/sysconfig/network/routes
...
192.51.100.42/32 192.51.100.1 - eth0
- Add the following to setup a static route from 192.51.100.42 to 192.51.100.10:
#/etc/sysconfig/network/routes
...
192.51.100.10/32 192.51.100.1 - eth0
- As an alternative, both servers can have a static route to the whole /24 subnet with the following configuration:
#/etc/sysconfig/network/routes
...
192.51.100.0/24 192.51.100.1 - eth0
How do I setup a static route on Windows?
- Open the “Command Prompt” with administrative privileges. Simply right-click on the Command Prompt icon in your Start panel and then click on “Run as administrator”:
- On server 192.51.100.10 enter the following command:
route -p add 192.51.100.42 mask 255.255.255.255 192.51.100.1
- Setup the corresponding entry on server 192.51.100.42 with this command:
route -p add 192.51.100.10 mask 255.255.255.255 192.51.100.1
- Please note setting the -p option makes the route persistent across reboots. If the route is meant to be temporary, you may omit -p.
- Since Windows automatically adds a default route to the whole subnet with a higher metric, it is necessary to also deactivate this default route. Otherwise, the new static route will not have any effect. Use the following command to delete the default route:
route delete 192.51.100.0