Network Configuration FreeBSD

General Information

Network configuration in FreeBSD can be a bit difficult because there are a few different text files to edit in order for your changes to be permanent. Hopefully this guide will clear some things up.

Requirements

  1. Local root access on the box or be able to su to root.
  2. A SSH client that supports ANSI colors such as puTTy or SecureCRT (if you aren’t on the box).
  3. Your favorite text editor (I like nano).

Configuration

I will first show you how to configure your network on-the-fly and then show you how to make the changes persistent.

IP Addresses

Before you can set your IP address you need to know which interface to modify. To do this, first run ifconfig(8) to see which interfaces you have.

# ifconfig -a
fxp0: flags=8943 mtu 1500
        options=8
        inet6 fe80::202:b3ff:fe07:9387%fxp0 prefixlen 64 scopeid 0x1
        ether 00:02:b3:07:93:87
        media: Ethernet autoselect (100baseTX )
        status: active
plip0: flags=108810 mtu 1500
lo0: flags=8049 mtu 16384
        inet 127.0.0.1 netmask 0xff000000
        inet6 ::1 prefixlen 128
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x3

In this example, I have fxp0 as my main interface. lo0 is the loopback so you don’t need to touch it. Now here’s how to set the IP to 192.168.0.2 with a subnet mask of 255.255.255.0.

# ifconfig fxp0 192.168.0.2 netmask 255.255.255.0

Now when you view your interfaces, you will see something like this:

# ifconfig -a
fxp0: flags=8943 mtu 1500
        options=8
        inet 192.168.0.2 netmask 0xffffff00 broadcast 192.168.0.255
        inet6 fe80::202:b3ff:fe07:9387%fxp0 prefixlen 64 scopeid 0x1
        ether 00:02:b3:07:93:87
        media: Ethernet autoselect (100baseTX )
        status: active
plip0: flags=108810 mtu 1500
lo0: flags=8049 mtu 16384
        inet 127.0.0.1 netmask 0xff000000
        inet6 ::1 prefixlen 128
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x3

You now have an IP address set, but if you want it set permanently, add it to your /etc/rc.conf:

# echo 'ifconfig_fxp0="inet 192.168.0.254  netmask 255.255.255.0"' >> /etc/rc.conf

Note: Be sure to change fxp0 to whatever your NIC is.

If you have multiple NICs that you want to use, just use the same procedure to add an address to their interface.

IP Aliases

If you have one NIC, but want it to listen on multiple IP addresses, you need to add aliases with a 32-bit mask.

# ifconfig fxp0 alias 192.168.0.100 netmask 255.255.255.255

Now if you look at your configuration you’ll see the additional IP.

fxp0: flags=8943 mtu 1500
        options=8
        inet 192.168.0.2 netmask 0xffffff00 broadcast 192.168.0.255
        inet6 fe80::202:b3ff:fe07:9387%fxp0 prefixlen 64 scopeid 0x1
        inet 192.168.0.100 netmask 0xffffffff broadcast 192.168.0.100
        ether 00:02:b3:07:93:87
        media: Ethernet autoselect (100baseTX )
        status: active
plip0: flags=108810 mtu 1500
lo0: flags=8049 mtu 16384
        inet 127.0.0.1 netmask 0xff000000
        inet6 ::1 prefixlen 128
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x3

You now have an IP address alias set, but if you want it set permanently, add it to your /etc/rc.conf file:

# echo 'ifconfig_fxp0_alias0="inet 192.168.0.100  netmask 255.255.255.255"' >> /etc/rc.conf

Note: Be sure to change fxp0 to whatever your NIC is.

If your NIC is to have multiple aliases, in your /etc/rc.conf file increment alias0 for each additional alias. For example, if my NIC had two aliases, my /etc/rc.conf would look something like this:

ifconfig_fxp0_alias0="inet 192.168.0.100  netmask 255.255.255.255"
ifconfig_fxp0_alias1="inet 192.168.0.101  netmask 255.255.255.255"

Default Gateway

In order to route any traffic, a default gateway needs to be set up.

# route add default 192.168.0.1

Note: Change 192.168.0.1 to whatever your default route should be.

To make this route persistent, add it to your /etc/rc.conf

# echo 'defaultrouter="192.168.0.1"' >> /etc/rc.conf

You can add more routes if you need to with route(8) and then view your routing table with netstat(8).

# netstat -r

DNS Servers

You DNS servers get added in /etc/resolv.conf.

# nano -w /etc/resolv.conf

nameserver 68.1.56.33
nameserver 68.1.56.34

Additional Notes

Instead of adding all the information to /etc/rc.conf and running the commands to set the changes, you can just add all changes to /etc/rc.conf and then issue the following command to set your network parameters:

# /etc/netstart

Now your network is all set up. Enjoy.

Speak Your Mind

*