Adding a static IP to an interface configured with DHCP

I have installed FreeBSD 14.3 in a VPS.
I select DHCP so the provider can provision the IP, but I have also a floating IP I can assign to that interface.

I have tried the following in /etc/rc.conf:

Code:
ifconfig_vtnet0="DHCP"
ifconfig_vtnet0_alias0="inet W.X.Y.Z netmask 255.255.255.255"

But after rebooting the system the static IP does not appear configured (you do not see using ifconfig)

However if I just type as root:

Code:
ifconfig vtnet0 W.X.Y.Z netmask 255.255.255.255 alias

Then the static IP is added normally to the DHCP assigned one.

Is that the right way to add both DHCP + static IP in /etc/rc.conf?
Why the manual command works but the entry in /etc/rc.conf does not?
 
The IP assigned by DHCP is the primary IP of the VPS.
The static IP is a floating static IP also assigned to that VPS, but you need to configure it manually in the server.
 
You're probably going to get the same assigned IP from DHCP, so just configure both addresses statically. Keep in mind that you will need to configure defaultrouter too, and set DNS correctly in /etc/resolv.conf.
 
I mean the numbers.
Publishing real public IP addresses in a forum isn’t recommended.
Both addresses are valid public IPs from different network segments, and the cloud provider handles their routing and reachability.
The manual command configures the second IP address on the host and it works.

What it is not working is when you put in /etc/rc.conf.
 
You're probably going to get the same assigned IP from DHCP, so just configure both addresses statically. Keep in mind that you will need to configure defaultrouter too, and set DNS correctly in /etc/resolv.conf.
I guess it works and it is a valid workaround, but I would like to understand why this does not work.
 
I have installed FreeBSD 14.3 in a VPS.
I select DHCP so the provider can provision the IP, but I have also a floating IP I can assign to that interface.

I have tried the following in /etc/rc.conf:

Code:
ifconfig_vtnet0="DHCP"
ifconfig_vtnet0_alias0="inet W.X.Y.Z netmask 255.255.255.255"

But after rebooting the system the static IP does not appear configured (you do not see using ifconfig)

However if I just type as root:

Code:
ifconfig vtnet0 W.X.Y.Z netmask 255.255.255.255 alias

Then the static IP is added normally to the DHCP assigned one.

Is that the right way to add both DHCP + static IP in /etc/rc.conf?
Why the manual command works but the entry in /etc/rc.conf does not?
With a DHCP server you can allocate a specific IP address by MAC address

Under dnsmasq you can use something like this:

dhcp-host=00:1d:72:96:4e:0b,192.168.1.250,x6,24h
 
You can put something like this in /etc/dhclient.conf:
Code:
alias {
      interface "vtnet0";
      fixed-address W.X.Y.Z;
      option subnet-mask 255.255.255.255;
}
 
So doing the command manually works, kind of implies to me "timing" when using rc.conf. (I could be wrong, feel free to flog me) but would the alias actually be applied after the interace got it from DHCP? Is it possible that the alias is applied when the device is discovered (before DHCP completes) and then DHCP simply wipes out the alias?

If so, then I think didal is on the correct track (Assumption here is that block of code executes after DHCP client completes on that interface and adds the alias address).
 
You can put something like this in /etc/dhclient.conf:
Code:
alias {
      interface "vtnet0";
      fixed-address W.X.Y.Z;
      option subnet-mask 255.255.255.255;
}

Thanks I have tried this approach but unfortunately the static IP is still not added as an alias.
I am trying the static approach now.
 
You're probably going to get the same assigned IP from DHCP, so just configure both addresses statically. Keep in mind that you will need to configure defaultrouter too, and set DNS correctly in /etc/resolv.conf.

It seems DHCP does not support a dynamically assigned IP and a static one (this is stated by dhclient.conf man page).
The DHCP client seems to support a non standard addition via alias as pointed out by didal/mer.

However the alias seems not to work and it is not standard, so now I am trying the approach you suggested (two static IP addresses). Which is perfectly fine since the primary IP will not change, it is always the same.

This is the configuration that is expected, where a.a.a.a and b.b.b.b are both static IPs.

Code:
$ netstat -rn
Routing tables

Internet:
Destination        Gateway            Flags         Netif Expire
default            172.31.1.1         UGS          vtnet0
a.a.a.a        link#2             UH              lo0
127.0.0.1          link#2             UH              lo0
b.b.b.b    link#2             UH              lo0
172.31.1.1         link#1             UHS          vtnet0

I have tried this un /etc/rc,conf:

Code:
ifconfig_vtnet0="inet a.a.a.a netmask 255.255.255.255"
ifconfig_vtnet0_alias="inet b.b.b.b netmask 255.255.255.255"
defaultrouter="172.31.1.1"

/etc/resolv.conf contains the name resolvers.

Now both IPs appear for interface vtnet0, but the default route is not present:

Code:
$ netstat -rn
Routing tables

Internet:
Destination        Gateway            Flags         Netif Expire
a.a.a.a        link#2             UH              lo0
127.0.0.1          link#2             UH              lo0
b.b.b.b    link#2             UH              lo0

I guess I am missing to tell the system an additional route, which is: 172.31.1.1 is in vtnet0, how do I do that?
 
Code:
ifconfig_vtnet0="inet a.a.a.a netmask 255.255.255.255"
Make a note of the subnet mask you got when you used DHCP. Use the same subnet mask, not /32. The /32 is fine on the alias.

Now both IPs appear for interface vtnet0, but the default route is not present:
The thing about routing addresses is that they must be in a so-called "directly connected" network. It cannot be reached if it's outside your broadcast domain (i.e. subnet). So it simply bailed out and failed to apply.

Some providers give you a such a "whacky" gateway address though[*]. But DHCP would have failed to apply that too. If it worked on DHCP then the gateway address is likely within your subnet, so you just have to set the subnet mask of the primary IP address correctly.

[*] If you do end up with this, there are ways to set a couple of static routes and get it to work, but it requires a bit of fiddling with the routing table.
 
Make a note of the subnet mask you got when you used DHCP. Use the same subnet mask, not /32. The /32 is fine on the alias.


The thing about routing addresses is that they must be in a so-called "directly connected" network. It cannot be reached if it's outside your broadcast domain (i.e. subnet). So it simply bailed out and failed to apply.

Some providers give you a such a "whacky" gateway address though[*]. But DHCP would have failed to apply that too. If it worked on DHCP then the gateway address is likely within your subnet, so you just have to set the subnet mask of the primary IP address correctly.

[*] If you do end up with this, there are ways to set a couple of static routes and get it to work, but it requires a bit of fiddling with the routing table.

The subnet of the primary IP address a.a.a.a is 255.255.255.255.
This is confirmed by using DHCP and ifconfig.

And DHCP alone gives you this routing table:

Code:
$ netstat -rn
Routing tables

Internet:
Destination        Gateway            Flags         Netif Expire
default            172.31.1.1         UGS          vtnet0
a.a.a.a        link#2             UH              lo0
127.0.0.1          link#2             UH              lo0
172.31.1.1         link#1             UHS          vtnet0

And networking works fine in DHCP (and if you later perform the command ifconfig manually you even have your additional floating static IP.)
 
Make a note of the subnet mask you got when you used DHCP. Use the same subnet mask, not /32. The /32 is fine on the alias.


The thing about routing addresses is that they must be in a so-called "directly connected" network. It cannot be reached if it's outside your broadcast domain (i.e. subnet). So it simply bailed out and failed to apply.

Some providers give you a such a "whacky" gateway address though[*]. But DHCP would have failed to apply that too. If it worked on DHCP then the gateway address is likely within your subnet, so you just have to set the subnet mask of the primary IP address correctly.

[*] If you do end up with this, there are ways to set a couple of static routes and get it to work, but it requires a bit of fiddling with the routing table.

Got some help to configure the route to the default gateway:

Code:
ifconfig_vtnet0="inet a.a.a.a netmask 255.255.255.255"
ifconfig_vtnet0_alias0="inet b.b.b.b netmask 255.255.255.255"
static_routes="lan"
route_lan="-host 172.31.1.1 -interface vtnet0"
defaultrouter="172.31.1.1"

Now it works, both IPs are present and the routing is fully configured.
 
Back
Top