Default gateway deleted after setting new static IP

Hi all,

I'm a bit puzzled.

This is my /etc/rc.conf:
Code:
[..]
ifconfig_em0="inet 192.168.10.3 netmask 255.255.255.0"
defaultrouter="192.168.10.1"
[..]

defaultrouter sets the default gateway (right?)

If I now change my IP, e.g.

Code:
$ ifconfig em0 inet 192.168.10.5 netmask 255.255.255.0

Code:
$ ping www.freebsd.org
PING wfe0.nyi.freebsd.org (96.47.72.84): 56 data bytes
ping: sendto: No route to host
ping: sendto: No route to host
ping: sendto: No route to host
^C
--- wfe0.nyi.freebsd.org ping statistics ---
3 packets transmitted, 0 packets received, 100.0% packet loss

Taking netstat -r before and after change of IP I see that the default gateway has been removed:

Code:
$ diff before after
5d4
< default            home               UGS         em0
8c7
< 192.168.10.3       link#1             UHS         lo0
---
> 192.168.10.5       link#1             UHS         lo0

Why and how does a change in IP with ipconfig, remove the default gateway?
 
Changing the IP address takes the interface offline for a short period, it first removes the old IP. Then routes (this can include the default gateway) that are associated with the interface and that old IP disappear too. This will happen with a service netif restart too. After changing the IP address (or restarting netif), you need to run service routing start to add the routes (including the default gateway) again.
 
SirDice,

Great, thank you for your explanation.

I did a # route add default 192.168.10.1 and everything was up and running again (apparently). I never did a service routing start. Duly noted, however.
 
Sure, you can add the route(s) by hand of course, just like you changed the IP by hand. A service routing start will simply apply defaultrouter and any static routes you may have defined in rc.conf.

The preferred way would be to change the IP in rc.conf, then run service netif restart em0 and service routing restart, this will just apply whatever was set in rc.conf. Doing so will also make sure your rc.conf is set correctly. Using ifconfig(8) and route(8) will only make temporary changes, when you reboot those settings will be gone.
 
Back
Top