23

Currently I'm running a FreeBSD 9.1 and the default gateway is already configured in the rc.conf.

rc.conf:

defaultrouter = "10.0.0.1"

But now I want to change the default gateway without rebooting the system, is this possible?

7 Answers 7

22
route del default
route add default 1.2.3.4

Where 1.2.3.4 is the new gateway. You can even concatenate them onto the same line with a ;

Edit: This is FreeBSD, not Linux. The command is different. Please do not edit this Answer if you haven't read the Question carefully enough to determine the operating system being used.

3
  • 6
    Note: do this in console, not over ssh. If you must do this via ssh (or other network method), issue both commands at once, with ; or with && Jul 8, 2014 at 14:58
  • 4
    Or, use the generic safe method: 1) Log into a shell, shutdown/reboot in 15 minutes unless cancelled 2) Do unsafe things. 3) Cancel shutdown/reboot.
    – Hennes
    Jul 8, 2015 at 17:24
  • 3
    On Linux the commands ip route del default and ip route add default via 1.2.3.4 work. So the command is still relevant for Linux users too as it has quite a bit of resemblance. Jan 17, 2017 at 17:26
18

You can add a new default route and remove the old one using either the ip or route command. The commands below will replace the gateway with 192.0.2.1. Both command pairs do the same thing. FreeBSD and other OSs should have one or both programs, possibly with slightly different formats. (FreeBSD has the route command and excludes the gw keyword used in other implementations.) The commands man ip and/or man route should provide you with documentation on your specific implementation.

route add default 192.0.2.1
route del default 10.0.0.1

ip route add default via 192.0.2.1
ip route del default via 10.0.0.1 

There are multiple implementations of these commands, so the above may not match your implementation. Your implementation should have a man page with examples for common use cases such as adding and removing default gateways. Try man route and man ip to see how your implementation works.

Change 192.0.2.1 to your desired default gateway. The default gateway needs to be on one of networks you have a direct connection to. You can change your IP address in a similar manner. ip is a newer tool which will do most everything you need to do to view and manage IP addresses and routing on IPv4 and IPv6 networks. ifconfig is an an older tool for configuring IP addresses on an IPv4 network.

To make the change permanent, update your network configuration files in /etc. The file(s) vary depending on the distribution you are using.

At least one of these commands should be available on any Unix derived O/S. Different versions may work slightly differently. Check the man page for details on your O/S.

3
  • 4
    -1 Sorry, but these are Linux commands and don't work on FreeBSD.
    – Chris S
    Aug 21, 2013 at 1:59
  • 2
    @ChrisS updated to reference the changes for FreeBSD. route has multiple implementations with minor syntax changes.
    – BillThor
    Jul 9, 2015 at 4:31
  • 1
    vote up for "ip route" commands .
    – Sérgio
    Jul 10, 2018 at 15:48
15

(EDIT: this may be useful if you're on Linux, but the OP asked about FreeBSD)

Here's a one-liner:

ip route replace default via 1.2.3.4

Where 1.2.3.4 is the new gateway IP

credit

2
  • 2
    -1 Sorry, but these are Linux commands and don't work on FreeBSD.
    – Chris S
    May 22, 2015 at 15:49
  • @ChrisS ah, thanks for explaining the downvotes - fair enough, I hadn't spotted that. 👍 Jul 21, 2020 at 14:17
3

yes just change the route and restart your network.

/etc/rc.d/netif restart
1
  • 3
    That works, though it restarts a heck of a lot... and might break applications that are bound to interfaces.
    – Chris S
    Aug 21, 2013 at 1:59
1

You could try:

route add default gw 192.168.1.254 eth0
3
  • -1 Sorry, but these are Linux commands and don't work on FreeBSD.
    – Chris S
    May 22, 2015 at 15:49
  • Route add is part of my FreeBSD 8.4 install though. But yeah, many people here who answer for the wrong OS.
    – Hennes
    Jul 8, 2015 at 17:26
  • 1
    @Hennes the route add part works fine. GNU/Linux added the gw part, which doesn't work on any other *nix. Without the gw works on all the BSDs, Solaris, OSX, HPUX, AIX, etc.
    – Chris S
    Jul 9, 2015 at 19:10
1

It's very easy, you only need to type the next commands:

$ route del 0/0
$ route add 0/0 192.168.0.1

You gonna need to replace the '192.168.0.1' with the IP of your choice.

And to check the changes you can use 'netstat':

$ netstat -r

This command show the routing table of the system.

2
  • What is "0/0" ?
    – WWW
    Aug 21, 2013 at 15:20
  • 1
    @WWW '0/0' is the abbreviation of the network block '0.0.0.0/0', which is the same as 'default', but I prefer '0/0' (I suppose i'm more of the old school).
    – ifm
    Aug 21, 2013 at 17:23
0

Always first use netstat -nr to check the gateway what you have and what the details it has...you See 0.0.0.0. when its a default one and you should first think that why am I changing this gateway than use this command:

route add default gw your ip eth0
1
  • -1 Sorry, but these are Linux commands and don't work on FreeBSD.
    – Chris S
    Jul 9, 2015 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.