Getting IP addresss for your FreeBSD with DHCP is convenient and easy. However there are sometimes some cases where you want your FreeBSD machine to have static IP address. For example, when you might want to use your FreeBSD as a server. In this guide, I will show you how easy is settting Static IP address and hostname on FreeBSD 12.
Before you start, you must know your network gateway and what IP Address you want to assign as static IP address.
Table of Contents
Get Current IP Address
Run ifconfig command from terminal.
# ifconfig
Output
em0: flags=8802 metric 0 mtu 1500
options=81009b
ether 08:00:27:24:59:f7
media: Ethernet autoselect (1000baseT )
status: active
nd6 options=29
em1: flags=8843 metric 0 mtu 1500
options=81009b
ether 08:00:27:44:dc:28
inet 192.168.2.88 netmask 0xffffff00 broadcast 192.168.2.255
media: Ethernet autoselect (1000baseT )
status: active
nd6 options=29
lo0: flags=8049 metric 0 mtu 16384
options=680003
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x3
inet 127.0.0.1 netmask 0xff000000
groups: lo
nd6 options=21
I have two interfaces, em0 and em1 in my FreeBSD. I will use em1 interface and set static IP Address. This em1 interface already has IP Address from DHCP.
Get Network Gateway IP Address
Next, you will need network gateway IP address. If you don’t know your network gateway IP address, you can use netstat to get the network gateway IP address.
# netstat -r
Output
Routing tables
Internet:
Destination Gateway Flags Netif Expire
default 192.168.2.1 UGS em1
localhost link#3 UH lo0
192.168.2.0/24 link#2 U em1
192.168.2.88 link#2 UHS lo0
Internet6:
Destination Gateway Flags Netif Expire
::/96 localhost UGRS lo0
...
In FreeBSD machine, my network gateway IP address is 192.168.2.1.
Settting Static IP Address On FreeBSD 12
Add ip address with ifconfig command below. You will need a privileged user. You can run it as sudo or doas. In the example below I logged in as root.
# ifconfig em1 add 192.168.2.214 netmask 255.255.255.0
Also, read Docker Examples Using Python and REST API.
Using ifconfig add command to add static IP address won’t be persistent. If you reboot your FreeBSD machine, your static IP address will be gone. To make it persistent you have to add your IP address in file /etc/rc.conf.
Open file /etc/rc.conf and add this line.
ifconfig_em1="inet 192.168.2.215 netmask 255.255.255.0"
defaultrouter="192.168.2.1"
Change to interface name (em1) to your interface that you want to change.
Settting DHCP IP Address
To set DHCP IP Address, edit file /etc/rc.conf and add this line.
ifconfig_em1="DHCP"
Settting FreeBSD Hostname
To set a FreeBSD hostname, you can run hostname command below.
# hostname freebsd.localdomain
The above command will set hostname in your FreeBSD, but will not make your changes persistent. In other words, when you reboot, the hostname will change to original hostname. To set a FreeBSD hostname persistently, add this line in file /etc/rc.conf.
hostname="freebsd.localdomain"
Restart FreeBSD Network and Routing
You need to restart FreeBSD network and Routing so your changes in file /etc/rc.conf takes effect. Run this command in order if you are using SSH, so you don’t get lock out from SSH session.
# /etc/rc.d/netif restart && /etc/rc.d/routing restart
I hope this guide helped you to set ip address and hostname on FreeBSD. Interested in FreeBSD? Get FreeBSD and try it on VirtualBox.