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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 4 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • loris Jul 6, 2010 @ 12:49

    > How Do I Restart Network Service?
    > Simply type the following command:
    > # sh /etc/network
    /etc/network doesn’t exists …. I think it’s /etc/netstart:
    # sh /etc/netstart

    • 🐧 nixCraft Jul 6, 2010 @ 13:10

      Thanks for the heads up! The Q & A has been updated.

  • Charan Oct 12, 2010 @ 21:23

    Hi Vivek,

    can I setup static route based on hostname instead of IP address.

  • joe Aug 27, 2015 @ 0:16 is router’s adreess .

    i do
    ifconfig fxp0
    route change -inet
    sh /etc/netstart

    ping —-> fail

    perhaps because
    route -n show -inet
    change net gateway
    Routing tables

    Destination Gateway Flags Refs Use Mtu Prio Iface
    127/8 UGRS 0 0 32768 8 lo0 UHl 0 0 32768 1 lo0
    192.168.1/24 UCP 1 0 – 8 fxp0
    192.168.1/24 UCP 0 0 – 8 fxp0 00:0d:9d:83:1b:92 UHLc 1 1 – 8 fxp0 00:00:39:e3:38:99 UHLl 0 0 – 1 lo0 UHb 0 0 – 1 fxp0
    224/4 URS 0 0 32768 8 lo0

    route -n show -inet

    Destination Gateway Flags Refs Use Mtu Prio Iface 00:0d:9d:83:1b:92 UHLc 1 10 – 8 fxp0 00:00:39:e3:38:99 UHLl 0 0 – 1 lo0 <—————————

    how to chage iface lo0 to fxp0 ?
    is this a router's work ?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum