Home FreeBSD Set Static IP Address And DNS On FreeBSD

Set Static IP Address And DNS On FreeBSD

By sk
14.3K views

In this brief guide, we will learn how to configure or set static IP address and DNS on a FreeBSD system from commandline. The steps provided below are tested on FreeBSd 12 and FreeBSD 13.1 versions.

First, let us set static IP address in FreeBSD.

1. Configure Static IP Address In FreeBSD

First of all, find the list of available network interfaces in your FreeBSD system.

To do so, simply run ifconfig command as root:

# ifconfig

This will list your current network configuration:

vtnet0: flags=8863<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
	options=4c07bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,TSO6,LRO,VLAN_HWTSO,LINKSTATE,TXCSUM_IPV6>
	ether da:48:4c:63:23:d2
	inet 192.168.1.50 netmask 0xffffff00 broadcast 192.168.1.255
	media: Ethernet autoselect (10Gbase-T <full-duplex>)
	status: active
	nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
	options=680003<RXCSUM,TXCSUM,LINKSTATE,RXCSUM_IPV6,TXCSUM_IPV6>
	inet6 ::1 prefixlen 128
	inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2
	inet 127.0.0.1 netmask 0xff000000
	groups: lo
	nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
View Network Interfaces In FreeBSD
View Network Interfaces In FreeBSD

As you see in the above output, the network card name is vtnet0. We are going to configure IP address for this network interface.

You may also have noticed that an IP address (192.168.1.50) is currently set to vtnet0. It is assigned from my DHCP server. We are going to change the current IP and set a static IP address of our choice.

To set static IP address in FreeBSD, edit /etc/rc.conf file as root:

# vi /etc/rc.conf

Add/modify the following lines:

[...]
ifconfig_vtnet0="inet 192.168.1.60 netmask 255.255.255.0"
defaultrouter="192.168.1.101"
[...]
Set Static IP Address In FreeBSD
Set Static IP Address In FreeBSD

Here, my new IP address is 192.168.1.60, subnet is 255.255.255.0 and default gateway is 192.168.1.101. Change these values according to your setup. Finally, press ESC and type :wq and press ENTER key to save the file and exit.

Restart the network using the following commands:

# /etc/rc.d/netif restart 
# /etc/rc.d/routing restart

Alternatively, you can use the following commands to restart network services:

# sh /etc/rc 
# /etc/netstart

Now check if the IP address has been changed by running ifconfig command:

View IP Address In FreeBSD
View IP Address In FreeBSD

2. Set DNS In FreeBSD

If you want to access Internet on your FreeBSD system, you should set the DNS nameservers details.

To setup DNS in FreeBSD, edit /etc/resolv.conf file as root:

# vi /etc/resolv.conf

Add/modify the nameserver IP address details:

nameserver 1.1.1.1 
nameserver 8.8.8.8

Press ESC and type :wq to save the file and close it. Restart the network to take effect the changes.

# /etc/rc.d/netif restart 
# /etc/rc.d/routing restart

That's it. Now you can access Internet from your FreeBSD system.

3. Auto Configure IP Address From DHCP Server

To configure network card to obtain IP address from a DHCP server, add or modify the following lines in /etc/rc.conf file:

ifconfig_vtnet0="DHCP"

Or,

ifconfig_DEFAULT="DHCP inet6 accept_rtadv"

Save and close the file. Restart networking service or reboot your system to take effect the changes.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More