OpenBSD: Static Routing Configuration

I know how to setup static routing under Linux and FreeBSD operating systems. But, how do you setup static routing under OpenBSD operating systems?

OpenBSD uses various text files in /etc for setting up hostname, ip address, default route and static routing.

route Command

The route command is used used to manually view and manipulate the network routing tables.

View Current Routing Table

Type the following command
# route show
# route -n show

To display ipv4 routes only, enter:
# route -n show -inet
To display ipv6 routes only, enter:
# route -n show -inet6
Sample outputs (ipv4 routes only):

Routing tables

Destination        Gateway            Flags   Refs      Use   Mtu  Prio Iface
default            7x.8y.2z.6y        UGS        0     7524     -     8 em1
10/8              UGS        1     3250     -     8 em0
10.8.18/24         link#1             UC         1        0     -     4 em0         00:1a:30:a9:a8:00  UHLc       1        0     -     4 em0
7x.8y.2z.6y/28     link#2             UC         1        0     -     4 em1
7x.8y.2z.6y        00:1a:30:ab:2a:00  UHLc       1        0     -     4 em1
127/8              UGRS       0        0 33160     8 lo0          UH         1      355 33160     4 lo0
224/4              URS        0        0 33160     8 lo0

netstat command

You can also use the netstat command to display routing table, enter:
# netstat -rn

How Do I Setup Static Routing Using route Command?

You can use route command as follows:

route add -net network/mask routerIp

In this example, route network using router, enter:

# route add -net
# route -n show
# ping

OpenBSD Persistence Static Routing

The drawback of route command is that, when OpenBSD reboots it will forget your static routes. So store them in configuration file. Static routing describes a system that does not implement adaptive routing. In these systems routes through a data network are described by fixed paths (statically). These routes are usually entered into the router by the system administrator. You need to edit your interface configuration file. If your interface name is em0, than config file name is /etc/hostname.em0. Edit /etc/hostname.em0, enter:
# vi /etc/hostname.em0
Append static routes as follows:

!route add -net

Save and close the file. You can additional routes also (make sure each route is added on a new line as follows):

!route add -net
!route add -net

A Note About Default Route (Default Gateway)

The default gateway is defined in the file called /etc/mygate. This will allow for your gateway to be set upon boot. This file consists of one line, with just the address of this machine’s gateway address:

cat /etc/mygate

Sample outputs:

Static Routing With Two Network Interface Configuration

Consider the following two interfaces:

 | em0 |---> (gateway

 | em1 |---> (gateway 

em0 Configuration

# cat /etc/hostname.em0
Sample outputs:

!route add -net

em1 Configuration

# cat /etc/hostname.em1
Sample outputs:


Default Gateway

# cat /etc/mygate
Sample outputs:

DNS configuration

# cat /etc/resolv.conf
Sample outputs:


How Do I Restart Network Service?

Simply type the following command:
# sh /etc/netstart
However, I recommend rebooting the server to make sure it works as described. Now, you should able to connect to both the Internet and LAN systems:
# ping
# ping
# ping
# dig


  • OpenBSD man page route, and netstat

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.