If you get the gateway wrong on a FreeBSD VPS, you can lose SSH access very fast. This guide shows you how to set IPs, gateways, and DNS in /etc/rc.conf, test them, and roll back if needed. Also, it explains extra IP blocks from your provider.
All addresses below are examples. Replace them with the values from your provider's panel.
What You Need First
On a VPS, your provider controls the network. They set the gateway, the IP block size, and sometimes which MAC address can use an IP. If you guess these values, your server goes offline.
Get these details from your provider before you set the FreeBSD static IP gateway:
- Interface name. On a KVM VPS, it is usually
vtnet0
- IPv4 address, netmask, and gateway
- IPv6 address, prefix length, and gateway
- DNS server addresses
- Any extra routed subnet
Also, you must make sure you can open your provider's browser console. If SSH stops working, the console is your way back in.
If your server is not ready yet, you can follow our FreeBSD 15.1 setup guide. You can run FreeBSD on a KVM Linux VPS, so check that your plan lists FreeBSD before you order.
Once you have these details and console access, you are ready to start the steps below.
Check Your Current Network
Log in as root and look at what the server currently uses:
ifconfig -lifconfig vtnet0netstat -rncat /etc/resolv.conf
The command ifconfig -l shows the names of your network interfaces. Use the name you see on your server, not the example vtnet0. Also, write down your current IP and gateway. If SSH works now, these values are your safe backup.
Then, use the command below to back up the config file:
cp -p /etc/rc.conf /etc/rc.conf.good
Protect SSH Access With a Timer
A wrong network setting can cut your SSH access. To stay safe, you must set a timer. If you lose the connection, the timer restores your old config and restarts the network by itself. If everything works, you cancel the timer.
This command creates a small script. After 5 minutes, it restores your old rc.conf and restarts the network:
cat > /root/net-rollback.sh <<'EOF'sleep 300cp -p /etc/rc.conf.good /etc/rc.confservice netif restartservice routing restartEOF
Run these two commands to start the timer. After that, you can safely edit your network settings:
chmod 700 /root/net-rollback.shdaemon -p /var/run/net-rollback.pid /bin/sh /root/net-rollback.sh
Note: The timer starts counting as soon as you run the second command. If you need more time, change sleep 300 in the script to a bigger number, such as sleep 600 for 10 minutes.
Set a FreeBSD Static IP Gateway in rc.conf
Now it is time to set your IP addresses and gateways. In FreeBSD, you can do this in the /etc/rc.conf file. Open the file with your desired text editor:
Remove any old DHCP line for the interface, such as ifconfig_vtnet0="DHCP". Then, add your values:
ifconfig_vtnet0="inet 203.0.113.10 netmask 255.255.255.0"defaultrouter="203.0.113.1" ifconfig_vtnet0_ipv6="inet6 2001:db8:100::10 prefixlen 64"ipv6_defaultrouter="2001:db8:100::1"
ifconfig_vtnet0 sets the IPv4 address and netmask.
defaultrouter sets the IPv4 default gateway.
ifconfig_vtnet0_ipv6 sets the IPv6 address. Use the _ipv6 name, not the IPv4 variable.
ipv6_defaultrouter sets the IPv6 default gateway.
Also, you can use sysrc instead of an editor. It edits rc.conf safely:
sysrc ifconfig_vtnet0="inet 203.0.113.10 netmask 255.255.255.0"sysrc defaultrouter="203.0.113.1"
Note: Some providers give you fe80::1 as the IPv6 gateway. This kind of address only works on your local link, so you must add the interface name after it:
ipv6_defaultrouter="fe80::1%vtnet0"
Handle Gateways Outside Your Subnet
For a FreeBSD static IP gateway on a VPS, many providers give you a single IP with a /32 mask, and the gateway is not inside that subnet. Then, FreeBSD cannot reach the gateway, and the default route fails.
You can fix it by adding a host route to the gateway on the interface:
ifconfig_vtnet0="inet 198.51.100.10 netmask 255.255.255.255"static_routes="gw"route_gw="-host 198.51.100.1 -interface vtnet0"defaultrouter="198.51.100.1"
The host route tells FreeBSD that the gateway is directly connected to your server. After that, the default route can use it. If your provider's panel says "on-link gateway" or "point-to-point", this is the setup you need.
A routed subnet is an extra block of IPs that the provider sends to your main IP. It is not a new LAN. Your FreeBSD static IP gateway stays the same, and you add the extra IPs as aliases:
ifconfig_vtnet0_alias0="inet 203.0.113.20 netmask 255.255.255.255"ifconfig_vtnet0_alias1="inet6 2001:db8:200::1 prefixlen 128"
Use a /32 mask for IPv4 aliases and /128 for IPv6 aliases. If you add them with the full subnet mask, FreeBSD thinks the whole IP block is on your local network. Then, it looks for those IPs locally instead of sending traffic to the gateway. This can break your traffic, and the problem is hard to spot.
Note: Do not add a second default route for a routed subnet.
Set DNS with resolv.conf
DNS turns domain names into IP addresses. Without it, ping 9.9.9.9 works but ping freebsd.org does not. On FreeBSD, you can set DNS servers in the /etc/resolv.conf file. Edit the file:
Add these lines. Each one is a DNS server. If your provider gave you DNS servers, use those instead:
nameserver 9.9.9.9nameserver 1.1.1.1nameserver 2606:4700:4700::1111
If resolv.conf goes back to the old values after a reboot, something is rewriting it. This is usually a DHCP setting or resolvconf. Remove the DHCP setting for that interface, or add your DNS servers to /etc/resolvconf.conf.
Set Up a Local DNS Cache on FreeBSD
Instead of using only outside DNS servers, you can run a small DNS resolver on your own server. FreeBSD already includes one, called local_unbound. It saves recent answers, so lookups are faster, and it checks that answers are real (DNSSEC).
Run these three commands in order. After the last one, the file should show nameserver 127.0.0.1:
sysrc local_unbound_enable=YESservice local_unbound startcat /etc/resolv.conf
Then, test it with the command below:
drill freebsd.org @127.0.0.1
If it fails, set local_unbound_enable=NO, stop the service, and put your upstream nameserver lines back.
Apply the Changes Safely
At this point, you can restart the network to apply the settings. Keep your current SSH window open while you do this:
service netif restart; service routing restart
netif applies your IP settings. routing applies your gateways and static routes.
Your SSH session may freeze for a few seconds. Wait, then open a second SSH window from your computer:
- If it connects, the change worked.
- If the server does not come back, wait five minutes. The rollback timer will restore your old config. If that does not work, use your provider's console.
Verify Connectivity
Run these checks from the working session to confirm the FreeBSD static IP gateway is active:
ifconfig vtnet0netstat -rn -f inetnetstat -rn -f inet6route -n get defaultroute -6 -n get default
You should see your IP addresses on the interface. Also, you must see a default route with the right gateway for both IPv4 and IPv6. Now test the path step by step:
ping -c 3 203.0.113.1ping -c 3 9.9.9.9ping6 -c 3 2001:db8:100::1ping6 -c 3 2606:4700:4700::1111drill freebsd.org Adrill freebsd.org AAAA
Read the results in this order:
- Gateway ping fails: the address, mask, or interface is wrong.
- Gateway works, but the internet ping fails: the default route is wrong.
- IP pings work, but drill fails: it is a DNS problem.
Tips: On FreeBSD 15.1, ping -6 also works if you prefer it over ping6.
Once all tests pass, stop the timer and keep the new config:
kill "$(cat /var/run/net-rollback.pid)"rm /root/net-rollback.shcp -p /etc/rc.conf /etc/rc.conf.good
Finally, reboot the server once, but only when you have console access. If the settings do not take effect after a reboot, the setup is not complete.
Fix Common FreeBSD SSH and Network Errors
These are the most common problems with a FreeBSD static IP gateway and their fixes:
- Wrong interface name. Check with
ifconfig -l. A typo means no address at boot.
- Wrong netmask. A
/24 on a /32 setup hides the gateway.
- Gateway outside the subnet. Add the host route from the earlier section.
- IPv6 link-local gateway without
%vtnet0. The route is rejected.
- MAC-locked IP. Some providers only allow an IP on the MAC address they assigned. If you changed the NIC or cloned the VM, ask the provider.
- Firewall rules. A blocked ICMPv6 breaks IPv6 neighbor discovery, so IPv6 stops after a while. Check the FreeBSD PF firewall setup guide for safe rules.
- Duplicate IP. Another server on the same network uses your address. Ping loss will come and go.
If your server's network goes down, you may need a way in that doesn't use the OS network. A dedicated server with IPMI access gives you a console even when the OS network is broken.
FreeBSD Network Settings: Quick Reference
You can use this table to find the right rc.conf setting quickly. Replace the example values with the details from your provider:
| Task |
rc.conf setting |
| IPv4 address |
ifconfig_vtnet0="inet IP netmask MASK" |
| IPv4 gateway |
defaultrouter="GATEWAY" |
| IPv6 address |
ifconfig_vtnet0_ipv6="inet6 IP prefixlen 64" |
| IPv6 gateway |
ipv6_defaultrouter="GATEWAY" |
| Extra IP |
ifconfig_vtnet0_alias0="inet IP netmask 255.255.255.255" |
| Apply |
service netif restart; service routing restart |
Conclusion
A working FreeBSD static IP gateway setup needs the right interface name, the right mask, a reachable gateway, and working DNS. You must back up rc.conf, set a rollback timer, apply the change, and test each layer.
Always keep console access ready, and follow your provider's network details.
For more details, you can check the official FreeBSD Handbook network chapter.