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
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 4 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
> 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
Thanks for the heads up! The Q & A has been updated.
Hi Vivek,
can I setup static route based on hostname instead of IP address.
192.168.1.1 is router’s adreess .
i do
——–
ifconfig fxp0 192.168.1.242
route change -inet 192.168.1.0/24 192.168.1.1
sh /etc/netstart
——–
but
ping 8.8.8.8 —-> fail
perhaps because
route -n show -inet
——————————————
change net 192.168.1.0/24: gateway 192.168.1.1
Routing tables
Internet:
Destination Gateway Flags Refs Use Mtu Prio Iface
127/8 127.0.0.1 UGRS 0 0 32768 8 lo0
127.0.0.1 127.0.0.1 UHl 0 0 32768 1 lo0
192.168.1/24 192.168.1.1 UCP 1 0 – 8 fxp0
192.168.1/24 192.168.1.242 UCP 0 0 – 8 fxp0
192.168.1.91 00:0d:9d:83:1b:92 UHLc 1 1 – 8 fxp0
192.168.1.242 00:00:39:e3:38:99 UHLl 0 0 – 1 lo0
192.168.1.255 192.168.1.242 UHb 0 0 – 1 fxp0
224/4 127.0.0.1 URS 0 0 32768 8 lo0
route -n show -inet
Internet:
Destination Gateway Flags Refs Use Mtu Prio Iface
192.168.1.91 00:0d:9d:83:1b:92 UHLc 1 10 – 8 fxp0
192.168.1.242 00:00:39:e3:38:99 UHLl 0 0 – 1 lo0 <—————————
how to chage iface lo0 to fxp0 ?
is this a router's work ?