FreeBSD Set a Default Route / Gateway

by on January 13, 2008 · 7 comments· LAST UPDATED October 31, 2008

in , ,

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

A. The route command is used under FreeBSD to manually manipulate the network routing tables. It provides six commands:
=> add : Add a route.

=> flush :Remove all routes.

=> delete : Delete a specific route.

=> change : Change aspects of a route (such as its gateway).

=> get : Lookup and display the route for a destination.

=> monitor : Continuously report any changes to the routing information base, routing lookup misses, or suspected network partitionings.

Task: View / Display FreeBSD Routing Table

Use netstat command with -r option:
$ netstat -r
$ netstat -rn

Output:

Routing tables
Internet:
Destination        Gateway            Flags    Refs      Use  Netif Expire
default            61.221.xx.yy       UGS         0      247    em1
10                 10.10.110.5        UGS         0       50    em0
10.10.110/26       link#1             UC          0        0    em0
10.10.110.5        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
127.0.0.1          127.0.0.1          UH          0        0    lo0

The first two line displays default routes.

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 192.168.1.254

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:
defaultrouter="192.168.1.254"
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 stop
# /etc/rc.d/routing start

Further readings:

  • Read man pages netstat, route, rc.conf
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 7 comments… read them below or add one }

1 thierry April 1, 2008 at 2:34 pm

# /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

Reply

2 Asmat Ali July 25, 2008 at 9:37 am

Thanks its wounder full.

Reply

3 nixCraft October 31, 2008 at 7:12 am

Asmat,

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

Reply

4 Manousos Bouloukakis June 6, 2009 at 7:26 am

Thanks for the compete tutorial. Really good!

M.

Reply

5 S.Ali August 27, 2011 at 7:27 pm

you are great my friend , keep the good work.

Reply

6 Karthik March 4, 2012 at 3:46 am

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

to

/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.

Reply

7 Pol April 27, 2013 at 11:27 am

Thanks!

Pol

Reply

Leave a Comment

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

Previous Faq:

Next Faq: