Configuring additional ip addresses in freebsd vps
Read this guide to configuring additional ip addresses in freebsd vps (ipv4 and ipv6).

This article provides a guide to configuring additional IP addresses in FreeBSD VPS servers.

Guide to Configuring Additional IP Addresses in FreeBSD VPS (IPv4 and IPv6)

This guide covers the steps required for configuring additional IP addresses in FreeBSD VPS. The process involves configuring the network interface and ensuring the IP addresses are persistent across reboots. The guide assumes you have root or sudo access to your VPS.

Prerequisites

  1. A FreeBSD 14 VPS: Ensure that FreeBSD 14 is installed and running on your VPS.
  2. Network Information: You should have the additional IPv4 and/or IPv6 addresses assigned by your hosting provider, as well as the gateway and netmask/subnet details.
  3. Root or Sudo Privileges: You need administrative rights to modify network configurations.

TL;DR: Skip to Configuration Guide

Launch freebsd vps from $2. 49/mo!

FreeBSD Network Configuration Overview

FreeBSD’s network configuration is highly flexible and powerful, designed to allow fine-grained control over network interfaces, routing, and security.

It supports a wide range of network protocols, such as IPv4, IPv6, and can be tailored to various use cases, such as server environments, gateways, and embedded systems.

SEE ALSO: Launch a FreeBSD 14 VPS Server

Understanding FreeBSD’s network configuration is essential for system administrators working with FreeBSD systems, particularly in VPS or dedicated server environments.

1. Network Interfaces

In FreeBSD, network interfaces are identified by names like em0, vtnet0, re0, etc., depending on the type of hardware or virtual interface present. These interfaces handle traffic for both IPv4 and IPv6 and can be configured with multiple addresses, gateways, and even bonding or VLANs.

Listing Network Interfaces

To list all active network interfaces, use:

ifconfig

This command provides a detailed view of each interface’s configuration, including IP addresses, MAC addresses, and more.

Configuring Interfaces

The most common way to configure an interface temporarily is using the ifconfig command. For example, to assign an IP address:

ifconfig em0 inet 192.168.1.10 netmask 255.255.255.0

For IPv6:

ifconfig em0 inet6 2001:db8::1 prefixlen 64

Changes made using ifconfig are temporary and will be lost after a reboot unless made persistent through system configuration files, which we will discuss in the next section.

2. Network Initialization Files

FreeBSD uses several files to manage network settings. These files are read during boot and control the system’s network behavior. The most important file is /etc/rc.conf.

/etc/rc.conf

The /etc/rc.conf file is where you define persistent network configurations. This includes IP addresses, gateways, and other system services. For example, to assign an IPv4 address persistently to an interface, you would add the following line to /etc/rc.conf:

ifconfig_em0="inet 192.168.1.10 netmask 255.255.255.0"

For IPv6:

ifconfig_em0_ipv6="inet6 2001:db8::1 prefixlen 64"

Other common network-related configurations include:

  • Gateway Setup:
  defaultrouter="192.168.1.1"
ipv6_defaultrouter="2001:db8::1"
  • Enabling DHCP:
  ifconfig_em0="DHCP"

This will allow FreeBSD to dynamically obtain an IP address from a DHCP server.

3. IP Address Configuration

FreeBSD supports the configuration of both IPv4 and IPv6 addresses. As discussed above, IP addresses can be assigned to an interface temporarily with ifconfig or permanently via /etc/rc.conf.

Aliasing IP Addresses

FreeBSD allows you to assign multiple IP addresses to a single network interface using IP aliasing. This is useful for hosting multiple services on different IPs from the same machine.

For IPv4:

ifconfig_em0_alias0="inet 192.168.1.20 netmask 255.255.255.255"

For IPv6:

ifconfig_em0_alias0="inet6 2001:db8::2 prefixlen 64"

Each additional alias increments the alias number (alias1, alias2, etc.).

Static IP Assignment

A static IP is typically set in /etc/rc.conf as mentioned earlier. This ensures that the IP address remains consistent across reboots, which is important for services like web or database servers.

4. Routing Configuration

Routing is essential for enabling the system to send packets to different networks, particularly when communicating beyond the local subnet.

Default Gateway

The default gateway is the IP address through which traffic destined for other networks is routed. To configure a default gateway, add the following lines to /etc/rc.conf:

For IPv4:

defaultrouter="192.168.1.1"

For IPv6:

ipv6_defaultrouter="2001:db8::1"
Static Routes

In cases where multiple network routes are needed, you can configure static routes. Static routes can be defined in /etc/rc.conf like this:

static_routes="office"
route_office="-net 10.0.0.0/24 192.168.1.254"

This example routes all traffic to the 10.0.0.0/24 network via the gateway 192.168.1.254.

5. Networking Tools

FreeBSD comes with several powerful tools for managing and diagnosing network issues.

Common Networking Tools
  • ping: Used to test connectivity between the local machine and a remote host:
  ping 192.168.1.1
  • ping6: Used for testing IPv6 connectivity:
  ping6 2001:db8::1
  • traceroute: Useful for tracing the route packets take to a destination:
  traceroute 8.8.8.8
  • netstat: Displays network statistics, including active connections and routing tables:
  netstat -rn
  • sockstat: Lists active sockets and their associated processes:
  sockstat -4 # For IPv4
sockstat -6 # For IPv6
  • ifconfig: Used to configure and display network interface parameters.

These tools are essential for troubleshooting networking problems, whether related to connectivity, routing, or packet loss.

6. Firewall Integration

FreeBSD supports various firewall solutions, such as PF (Packet Filter), IPFW, and IPFilter, to control network traffic.

PF (Packet Filter)

PF is a powerful firewall that allows administrators to define rules for allowing, blocking, and logging traffic. To enable PF in FreeBSD, add the following line to /etc/rc.conf:

pf_enable="YES"

Firewall rules are typically stored in /etc/pf.conf, where you can define your policies:

block in all
pass out all keep state
pass in proto tcp to any port 22

This simple example blocks all incoming traffic, allows all outgoing traffic, and specifically allows incoming SSH traffic on port 22.

IPFW (IP Firewall)

IPFW is another firewall solution integrated into FreeBSD’s kernel. It’s configured via the ipfw command and can be enabled by adding:

firewall_enable="YES"
firewall_type="open"

You can configure IPFW rules dynamically using the ipfw command, such as:

ipfw add allow tcp from any to me 22

This rule allows TCP traffic to port 22 (SSH).

Networking Summary

FreeBSD’s networking configuration is highly customizable, making it ideal for a wide range of use cases, from simple server setups to complex multi-homed systems with custom routing and firewall rules.

By understanding the core components, such as network interfaces, routing, and tools like ifconfig and pf, administrators can fine-tune their network settings to meet the needs of their specific environment.

Configuring static and dynamic IP addresses, gateways, and aliases is straightforward, while powerful tools like PF and IPFW ensure tight control over traffic flow. With FreeBSD’s robust and flexible networking stack, administrators can implement secure and efficient network infrastructures.

Configuring Additional IP Addresses on Your FreeBSD VPS

Now that we’ve reviewed the basics of FreeBSD networking, we’re ready to take a look at configuring additional IPs on our FreeBSD VPS server.

Step 1: Determine Your Network Interface

First, you need to know which network interface is used by your VPS. To list the network interfaces, login via SSH as root-equivalent user, and use the following command:

ifconfig

Typical network interfaces include em0, vtnet0, or re0. In this example, we’ll assume your interface is vtnet0, but replace this with your actual interface name where necessary.

Step 2: Configure Additional IPv4 Addresses

To configure additional IPv4 addresses, you can temporarily add an IP address using the ifconfig command.

2.1. Add IPv4 Address Temporarily

To add an additional IPv4 address, use the following command:

ifconfig vtnet0 inet your_additional_ipv4 netmask 255.255.255.255 alias
  • Replace vtnet0 with your network interface name.
  • Replace your_additional_ipv4 with the actual IPv4 address you want to configure.

For example:

ifconfig vtnet0 inet 192.0.2.100 netmask 255.255.255.255 alias

Note: The alias keyword ensures that the IP address is added as an additional IP without removing the existing configuration.

2.2. Make the IPv4 Address Persistent

To make the additional IPv4 address persistent across reboots, you need to edit the /etc/rc.conf file.

Open /etc/rc.conf in a text editor such as vi or nano:

vi /etc/rc.conf

Add the following lines to the file to configure the new IP address:

ifconfig_vtnet0_alias0="inet your_additional_ipv4 netmask 255.255.255.255"

For example:

ifconfig_vtnet0_alias0="inet 192.0.2.100 netmask 255.255.255.255"
  • If you need to configure multiple additional IPv4 addresses, you can increment the alias number:
ifconfig_vtnet0_alias1="inet 192.0.2.101 netmask 255.255.255.255"

2.3. Restart Network Services

After updating /etc/rc.conf, restart the network service to apply the changes:

service netif restart

Alternatively, you can reboot the system for the changes to take effect:

reboot

Step 3: Configure Additional IPv6 Addresses

3.1. Add IPv6 Address Temporarily

To add an additional IPv6 address, use the following command:

ifconfig vtnet0 inet6 your_additional_ipv6 prefixlen 64 alias
  • Replace your_additional_ipv6 with the IPv6 address you wish to configure.

For example:

ifconfig vtnet0 inet6 2001:db8::100 prefixlen 64 alias

Note: Adjust the prefix length (prefixlen 64) based on your IPv6 allocation. Typically, /64 is the standard, but consult your hosting provider if unsure.

3.2. Make the IPv6 Address Persistent

To make the IPv6 address persistent across reboots, add the following lines to /etc/rc.conf:

ifconfig_vtnet0_alias0="inet6 your_additional_ipv6 prefixlen 64"

For example:

ifconfig_vtnet0_alias0="inet6 2001:db8::100 prefixlen 64"

To configure multiple additional IPv6 addresses, increment the alias number:

ifconfig_vtnet0_alias1="inet6 2001:db8::101 prefixlen 64"

3.3. Restart Network Services

After updating /etc/rc.conf, restart the network services for the changes to take effect:

service netif restart

Alternatively, reboot the system:

reboot

Step 4: Verify the Configuration

Once the network interfaces are configured, it is essential to verify that the additional IP addresses are working.

4.1. Verify with ifconfig

Run ifconfig to check that the additional IP addresses are assigned to the network interface:

ifconfig vtnet0

The output should list the additional IPv4 and IPv6 addresses.

4.2. Test Connectivity

  • IPv4: Use ping to test the additional IPv4 address:
  ping your_additional_ipv4
  • IPv6: Use ping6 to test the additional IPv6 address:
  ping6 your_additional_ipv6

If the IP addresses respond correctly, the configuration has been successfully applied.

Step 5: Troubleshooting

Here are some common issues and solutions if your IP addresses are not functioning correctly:

5.1. Incorrect Netmask or Prefix Length

Ensure that you are using the correct netmask for IPv4 (255.255.255.255 for single IP addresses) and the correct prefix length for IPv6 (usually /64).

5.2. Missing Gateway Configuration

If you need to route traffic through specific gateways, ensure that the default gateway for IPv4 and IPv6 is correctly configured in /etc/rc.conf. Example:

defaultrouter="your_default_gateway_ipv4"
ipv6_defaultrouter="your_default_gateway_ipv6"

5.3. Firewall Rules

If you are using a firewall (such as pf or ipfw), ensure that it allows traffic for the new IP addresses.

5.4. Check Logs

If something goes wrong, check the system logs for network-related issues:

tail -f /var/log/messages

Launch freebsd vps from $2. 49/mo!

Conclusion

Configuring additional IP addresses in FreeBSD VPS involves updating the network interface with ifconfig and making the changes persistent by modifying /etc/rc.conf.

This guide walked you through FreeBSD network configuration basics, and adding both IPv4 and IPv6 addresses to your FreeBSD VPS (ensuring persistent configuration across reboots) and verifying connectivity of your network.

By following these steps, you should be able to add as many IP addresses as your hosting provider allows and ensure they are properly configured in your FreeBSD system.

Share this:
Avatar of editorial staff

Editorial Staff

Rad Web Hosting is a leading provider of web hosting, Cloud VPS, and Dedicated Servers in Dallas, TX.
lg