FreeBSD Set a Default Route / Gateway

How do I modify or set a default route for my FreeBSD server? How do I make default route configuration persistent?

You need to use the route command on a FreeBSD to manually manipulate the network routing tables. It provides six commands as follows:

  1. add : Add a route.
  2. flush :Remove all routes.
  3. delete : Delete a specific route.
  4. change : Change aspects of a route (such as its gateway).
  5. get : Lookup and display the route for a destination.
  6. monitor : Continuously report any changes to the routing information base, routing lookup misses, or suspected network partitionings.

Task: View / Display FreeBSD Routing Table

Use the netstat command with -r option as follows:
$ netstat -r
$ netstat -rn

Sample outputs:

Routing tables
Destination        Gateway            Flags    Refs      Use  Netif Expire
default            61.221.xx.yy       UGS         0      247    em1
10               UGS         0       50    em0
10.10.110/26       link#1             UC          0        0    em0        00:1b:0d:e6:58:40  UHLW        2        0    em0   1145
61.221.xx.yy/29    link#2             UC          0        0    em1
61.221.xx.yy       00:1b:0d:e6:57:c0  UHLW        2        0    em1   1055
61.221.xx/24       link#2             UC          0        0    em1          UH          0        0    lo0

The first two line displays default routes. To just print IPv4 routing table, enter:
# netstat -4 -r -n
Sample outputs:

Fig.01: FreeBSD Display Default Routing Table Command

Fig.01: FreeBSD Display Default Routing Table Command

To just print IPv6 routing table, enter:
# netstat -6 -r -n

Task: FreeBSD Set a default route

All network packets that cannot be sent according to the previous entries of the routing table are sent through the following default gateway:
# route add default

How do I save routing information to a configuration file?

If you reboot FreeBSD box, the routing configuration will be lost i.e. the routing information will not persist. You need to edit /etc/rc.conf file to set defaultroute:
# vi /etc/rc.conf
Set default route by editing defaultrouter variable:
Save and close the file.

Task: Start and Stop FreeBSD Configured Interfaces

To apply changes to a configuration file, you need to stop and restart the corresponding FreeBSD networking interface. The following command will also update routing information:
# /etc/rc.d/netif restart
# /etc/rc.d/routing restart

# service netif restart
# service routing restart

How can I change or the default gateway from the FreeBSD CLI?

Use the following commands:
### [ delete the default route/gateway ] ###
# route del default
### [ now setup as the default gateway for my FreeBSD box ] ###
route add default

Verify it:
# netstat -r -n

Further readings:
  • Read man pages netstat, route, rc.conf, service

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

🐧 9 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
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 jobs killall kill pidof pstree pwdx time
Searchinggrep 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
9 comments… add one
  • thierry Apr 1, 2008 @ 14:34

    # /etc/rc.d/netif restart
    # /etc/rc.d/netif stop
    # /etc/rc.d/netif start
    these commands make two times the same action: up down the interface.
    But these commands don’t renew the routes. to renew the route you must do
    /etc/rc.d/routing stop
    /etc/rc.d/routing start

  • Asmat Ali Jul 25, 2008 @ 9:37

    Thanks its wounder full.

  • 🐧 nixCraft Oct 31, 2008 @ 7:12


    Thanks for the heads up. The faq has been updated.

  • Manousos Bouloukakis Jun 6, 2009 @ 7:26

    Thanks for the compete tutorial. Really good!


  • S.Ali Aug 27, 2011 @ 19:27

    you are great my friend , keep the good work.

  • Karthik Mar 4, 2012 @ 3:46

    Please change
    /etc/rc.d/routing stop
    /etc/rc.d/routing start


    /etc/rc.d/routing restart

    Most of the time one will be on a ssh session, above commands leads to
    big problems, latter one is always safer.

  • Pol Apr 27, 2013 @ 11:27



  • vincent Mar 23, 2017 @ 18:00

    netstart does not work, it s netstat

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