This page explains setting up a static IP address on the FreeBSD Unix system using the command-line options.
I decided to migrate my older Dell M6700 laptop running FreeBSD to Intel NUC because my Dell laptop is slowly dying, and finding parts such as the battery is problematic now. So I installed FreeBSD 13 but forgot to set up static IP. So right now, it is using the IP address assigned by DHCP. Setting a static IP address is desired because I use this as a backup server and run other services. So let us see the steps for setting up a static IP on the FreeBSD box.
|Est. reading time||3 minutes|
Configuring Static IP address on FreeBSD Server
The procedure to set up a static IP address on FreeBSD is as follows:
Step 1 – Finding FreeBSD interface name
Use the ifconfig command as follows:
# ifconfig -a |
# ifconfig em0 # em0 is my Ethernet device
One can use the dmesg command as follows along with the grep command too:
# grep -i ethernet /var/run/dmesg.boot
# dmesg | grep -i ethernet
Here is what I see:
mlx5en: Mellanox Ethernet driver 3.6.0 (December 2020) em0: Ethernet address: 54:b2:03:09:15:88
Step 2 – View the current settings
Simply query the /etc/rc.conf file contains static or DHCP network configuration for the FreeBSD system. Run the cat command or less command/more command:
# more /etc/rc.conf
# grep em0 /etc/rc.conf
The following line indicate that em0 is using the DHCP to obtain IP address and other information.
My goal is to reconfigure DHCP to a static IP address as follows:
- IP : 192.168.2.20
- Netmask : 255.255.255.0
- DNS1 : 192.168.2.236
- DNS2 : 192.168.2.237
- Default router: 192.168.2.254
Step 3 – FreeBSD configure static IP Address
Edit the the /etc/rc.conf, run:
# vim /etc/rc.conf
Replace it as follows:
ifconfig_em0="inet 192.168.2.20 netmask 255.255.255.0"
Step 4 – Set up a default router
Edit/append/update the following in the /etc/rc.conf
Save and close the file when using vim.
Step 5 – Define DNS server IP address for FreeBSD
Next, edit the resolver configuration file /etc/resolv.conf file, run:
# vim /etc/resolv.conf
search sweet.home nameserver 192.168.2.236 nameserver 192.168.2.237
Save and close the file.
Step 6 – Restart the networking and routing service on FreeBSD
WARNING! Restarting networking services over SSH sessions is risky. Make sure you entered the correct IP address in /etc/rc.conf file. Before continuing with the following command, make sure you stop and adjust firewall rules. If everything is correct and you are using ssh, just reboot the FreeBSD box.
Use the service command to restart networking services on FreeBSD when you are connected to console:
# service netif restart && service routing restart
For SSH based session reboot the FreeBSD box:
Step 6 – Configure static IP FreeBSD verification steps
Again use the ifconfig command and other commands as follows:
$ ifconfig | more
$ ifconfig em0
# finding dns info #
$ cat /etc/resolv.conf
$ host -t ns cyberciti.biz 192.168.2.237
# finding routing info #
$ route -n get nixcraft.com
$ route -n get 220.127.116.11
$ route -n get 192.168.2.25
$ netstat -4 -r -n
See “FreeBSD Set a Default Route / Gateway” for more info.
I have explained how to configure a static IP address on the FreeBSD server or desktop by editing the file named /etc/rc.conf. For more information, see the following documentation using the man command:
man 5 rc.conf