How to configure network settings in FreeBSD

Overview

In this tutorial, you will learn how to configure network settings in FreeBSD by setting static IP addresses and DHCP addresses. You will also learn how to set the hostname of a Freebsd host.

System Configuration File

Network configurations for FreeBSD are stored in the system configuration file. The path this file is /etc/rc.conf, it is where the server’s host name is set, as well as network interface configurations.

The following is an example of the rc.conf file used by FreeBSD.

hostname="freebsd12"
ifconfig_em0="DHCP"
ifconfig_em0_ipv6="inet6 accept_rtadv"
sshd_enable="YES"
# Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
dumpdev="AUTO"
  • The hostname value sets the host name of the FreeBSD server.
  • The ifconfig_em0 value sets the network configurations for the em0 network interface. In the example above, the interface is set to use DHCP.
  • The ifconfig_em0_ipv6 value is the IPv6 equvelant of the ifconfig_em0 value.
  • sshd_enable sets whether the SSH daemon is enabled or not.

Setting Hostname

The host name of a FreeBSD server is set in the /etc/rc.conf system configuration file. Look for the hostname key and change its value to the desired hostname.

  1. Open the system configuration file into a text editor.
    vi /etc/rc.conf
  2. Find the hostname key and update its value.
    hostname=server01
  3. Save your changes and exit the text editor.
  4. Apply your changes.
    ./etc/netstart

Setting a Static IPv4 Address

To set a static IP address you must specify a IP address with a netmask. The following example shows the the em0 network interface being assigned static IP address 192.168.0.10 with a network of 255.255.255.0, or /24.

  1. Open the system configuration file into a text editor, such as vim.
  2. Edit the ifconfig_ interface you want to assign a static IP address to using the following format.
    ifconfig_em0="inet 192.168.0.10 netmask 255.255.255.0"
  3. Save your changes and exit the text editor.
  4. Apply your new network settings.
    ./etc/netstart

Your new rc.conf file should look similar to the following after your changes.

hostname="freebsd12"
ifconfig_em0="inet 192.168.0.10 netmask 255.255.255.0"
ifconfig_em0_ipv6="inet6 accept_rtadv"
sshd_enable="YES"
# Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
dumpdev="AUTO"

You can verify the network changes were applied correctly by using the ifconfig command.

ifconfig em0
em0: flags=8843 metric 0 mtu 1500
	options=81009b
	ether 08:00:27:32:00:0a
	inet6 fe80::a00:27ff:fe32:a%em0 prefixlen 64 scopeid 0x1
	inet6 2607:fea8:4de0:b900:a00:27ff:fe32:a prefixlen 64 autoconf
	inet 192.168.0.10 netmask 0xffffff00 broadcast 10.0.0.255
	media: Ethernet autoselect (1000baseT )
	status: active
	nd6 options=23

Setting a DHCP Address

To set a DHCP address for a network interface on a FreeBSD host, you must edit the system configuration file /etc/rc.conf. For example, to set the network interface em0 to DHCP, you set would do the following.

ifconfig_em0="DHCP"
  1. Open the system configuration file into a text editor.
    vi /etc/rc.conf
  2. Update the network interface to use DHCP. In our example, the interface will be em0.
    ifconfig_em0="DHCP"
  3. Save your changes and exit the text editor.
  4. Apply your new network configurations.
    ./etc/netstart

Your system configuration file should resemble the following example, with the ifconfig_ interface you updated have the "DHCP" value.

hostname="freebsd12"
ifconfig_em0="DHCP"
ifconfig_em0_ipv6="inet6 accept_rtadv"
sshd_enable="YES"
# Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
dumpdev="AUTO"

To verify your changes applied successfully use the ifconfig command.

ifconfig em0