Howto Configure Static IPv4 On FreeBSD On Hetzner Cloud - 2022-03-22
Abstract
One would think that it is quite easy to configure a static IPv4 address on a hetzner cloud server on FreeBSD, but that is not the case. It’s not hard either, but took me a couple of minutes to understand what the issue is.
The documentation
contains a paragraph about this topic but only mentions DHCP
for IPv4:
ifconfig_vtnet0="DHCP"
ifconfig_vtnet0_ipv6="inet6 <one IPv6 address from your subnet, e.g. 2001:db8:0:3df1::1>/64"
ipv6_defaultrouter="fe80::1%vtnet0"
They did the same for NetBSD, so my guess was that this is an uncommon configuration.
This post assumes the following:
- IPv4 address of your host:
1.2.3.4/32
- IPv4 address of the gateway:
172.31.1.1
- Network interface:
vtnet0
Issue
Without the static route, you’ll run into the following error message when trying to add the default gateway.
root@hetzner-cloud-test:~ # route add default 172.31.1.1
route: writing to routing socket: Network is unreachable
add net default: gateway 172.31.1.1 fib 0: Network is unreachable
Howto
Usually it is enough to configure your /etc/rc.conf
to something like:
ifconfig_vtnet0="inet 1.2.3.4 netmask 255.255.255.255"
defaultrouter="172.31.1.1"
But, to get around the Network is unreachable
error, we also need to create a
static route like so:
static_routes="vtnet0"
route_vtnet0="172.31.1.1 -iface vtnet0"
That’s it. Reboot and everything should work.
Alternatively, to make it work at runtime, run the following commands:
ifconfig vtnet0 inet 1.2.3.4 netmask 255.255.255.255
route add 172.31.1.1 -iface vtnet0
route add default 172.31.1.1