OpenBSD: Static Routing Configuration

by on July 6, 2010 · 3 comments· LAST UPDATED July 7, 2010

in

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
Internet:
Destination        Gateway            Flags   Refs      Use   Mtu  Prio Iface
default            7x.8y.2z.6y        UGS        0     7524     -     8 em1
10/8               10.8.18.17         UGS        1     3250     -     8 em0
10.8.18/24         link#1             UC         1        0     -     4 em0
10.8.18.17         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              127.0.0.1          UGRS       0        0 33160     8 lo0
127.0.0.1          127.0.0.1          UH         1      355 33160     4 lo0
224/4              127.0.0.1          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 192.168.1.0/24 network using 192.168.1.254 router, enter:

# route add -net 192.168.1.0/24 192.168.1.254
# route -n show
# ping 192.168.1.254

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 192.168.1.0/24 192.168.1.254

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 192.168.1.0/24 192.168.1.254
!route add -net 192.168.2.0/24 192.168.2.254

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:

202.54.1.254

Static Routing With Two Network Interface Configuration

Consider the following two interfaces:

 +-----+
 | em0 |---> 192.168.1.1 (gateway 192.168.1.254)
 +-----+
 +-----+
 | em1 |---> 202.54.1.1 (gateway 202.51.1.254)
 +-----+

em0 Configuration

# cat /etc/hostname.em0
Sample outputs:

inet 192.168.1.1 255.255.255.0
!route add -net 192.168.1.0/24 192.168.1.254

em1 Configuration

# cat /etc/hostname.em1
Sample outputs:

inet 202.54.1.1 255.255.255.0

Default Gateway

# cat /etc/mygate
Sample outputs:

202.51.1.254

DNS configuration

# cat /etc/resolv.conf
Sample outputs:

domain nixcraft.net.in
search nixcraft.net.in
nameserver 192.168.1.100
nameserver 192.168.1.200
nameserver 202.54.1.19

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 192.168.1.254
# ping 202.54.1.19
# ping cyberciti.biz
# dig google.com

References:

  • OpenBSD man page route, and netstat
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 3 comments… read them below or add one }

1 loris July 6, 2010 at 12:49 pm

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

Reply

2 nixCraft July 6, 2010 at 1:10 pm

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

Reply

3 Charan October 12, 2010 at 9:23 pm

Hi Vivek,

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

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: