Software & AppsOperating SystemLinux

How To Set Up a Network Card with Two IP Addresses in Ubuntu 18.04

Ubuntu 1

In this guide, we will learn how to set up a network card with two IP addresses in Ubuntu 18.04. This can be useful in various scenarios, such as when you want to segregate network traffic or when you need to have redundant network connections for high availability.

Quick Answer

To set up a network card with two IP addresses in Ubuntu 18.04, you can modify the netplan configuration file to use static IP addresses instead of DHCP. Simply add the second IP address to the "addresses" field, configure the default gateway and DNS servers, and apply the changes using the "netplan apply" command.

Prerequisites

Before you start, make sure you have:

  • A Ubuntu 18.04 system with a network card.
  • Sudo or root access to the system.

Understanding Network Interfaces

In Ubuntu 18.04, network interfaces are managed by the netplan utility. The configuration files for netplan are located in the /etc/netplan/ directory. The default configuration file is typically named 01-netcfg.yaml or 50-cloud-init.yaml.

Configuring the Network Interface

Let’s start by opening the netplan configuration file with a text editor. In this example, we will use nano:

sudo nano /etc/netplan/01-netcfg.yaml

The configuration file will look something like this:

network:
 version: 2
 renderer: networkd
 ethernets:
 enp0s3:
 dhcp4: yes

In this configuration, enp0s3 is the name of the network interface, and dhcp4: yes indicates that the interface is configured to use DHCP to obtain an IP address.

Setting Up Two IP Addresses

To set up two IP addresses, we need to change the configuration to use static IP addresses instead of DHCP. We will also add a second IP address to the addresses field. Here is an example:

network:
 version: 2
 renderer: networkd
 ethernets:
 enp0s3:
 dhcp4: no
 addresses: [192.168.0.2/24, 172.16.0.2/16]

In this configuration, dhcp4: no disables DHCP, and addresses specifies the two IP addresses for the interface. The /24 and /16 after the IP addresses are CIDR notation for the subnet mask.

Configuring the Default Gateway and DNS

Next, we need to configure the default gateway and DNS servers. Here is an example:

network:
 version: 2
 renderer: networkd
 ethernets:
 enp0s3:
 dhcp4: no
 addresses: [192.168.0.2/24, 172.16.0.2/16]
 gateway4: 192.168.0.1
 nameservers:
 addresses: [8.8.8.8, 8.8.4.4]

In this configuration, gateway4: 192.168.0.1 specifies the default gateway, and nameservers: addresses: [8.8.8.8, 8.8.4.4] specifies the DNS servers.

Applying the Configuration

After modifying the netplan configuration file, you can apply the changes with the following command:

sudo netplan apply

This command will apply the new configuration immediately. You can verify the new configuration with the ip addr show command.

Conclusion

That’s it! You have now learned how to set up a network card with two IP addresses in Ubuntu 18.04. Remember to replace enp0s3 with the name of your network interface, and adjust the IP addresses and gateway addresses according to your network configuration.

For more information on netplan and its configuration options, you can check out the official netplan documentation.

Remember, having multiple IP addresses on a single network interface can be useful in many scenarios, but it can also complicate your network configuration. Always plan your network configuration carefully to avoid potential issues.

Can I set up multiple IP addresses on a single network card in Ubuntu 18.04?

Yes, you can set up multiple IP addresses on a single network card in Ubuntu 18.04 by modifying the netplan configuration file.

How do I open the netplan configuration file in Ubuntu 18.04?

You can open the netplan configuration file in Ubuntu 18.04 by using a text editor such as nano or vi. For example, you can use the command sudo nano /etc/netplan/01-netcfg.yaml to open the configuration file.

What is CIDR notation?

CIDR notation is a compact representation of an IP address and its associated subnet mask. It consists of the IP address followed by a slash (/) and the number of network bits in the subnet mask. For example, /24 represents a subnet mask of 255.255.255.0, and /16 represents a subnet mask of 255.255.0.0.

How do I configure the default gateway and DNS servers?

To configure the default gateway and DNS servers, you need to add the gateway4 field for the default gateway and the nameservers field for the DNS servers in the netplan configuration file. Specify the IP addresses accordingly.

Leave a Comment

Your email address will not be published. Required fields are marked *