Netplan is a utility tool used in Ubuntu to manage network configurations. It allows users to easily configure and manage network settings such as IP addresses, gateways, and DNS servers. In this article, we will explore how to add a secondary IP address and assign a label using Netplan.
What is a Secondary IP Address?
A secondary IP address is an additional IP address that can be assigned to a network interface. It allows a single network interface to have multiple IP addresses, each serving a different purpose. This can be useful in scenarios where you need to host multiple websites or services on a single server.
Adding a Secondary IP Address
To add a secondary IP address using Netplan, follow these steps:
- Open a terminal window. You can do this by pressing
Ctrl + Alt + Ton your keyboard. - Locate the Netplan configuration file for your network interface. The file is typically located in the
/etc/netplan/directory and has a.yamlextension. For example, if your network interface is namedeth0, the configuration file could be named01-netcfg.yaml. - Edit the Netplan configuration file using a text editor. You can use the
viornanoeditor, depending on your preference. For example, if you're using thenanoeditor, run the following command:sudo nano /etc/netplan/01-netcfg.yaml. - Inside the configuration file, locate the network interface section to which you want to add the secondary IP address. It should look similar to this:
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: true
Replace eth0 with the name of your network interface.
- Add the following lines below the
dhcp4: trueline:
addresses:
- primary_ip_address/primary_subnet
- secondary_ip_address/secondary_subnet
Replace primary_ip_address and primary_subnet with the primary IP address and subnet mask of your network interface. Replace secondary_ip_address and secondary_subnet with the secondary IP address and subnet mask you want to add.
- Save the changes and exit the text editor.
- Apply the Netplan configuration by running the following command:
sudo netplan apply.
Netplan will apply the changes and assign the secondary IP address to your network interface. You can verify the changes by running the ifconfig command in the terminal. You should see both the primary and secondary IP addresses listed under your network interface.
Assigning a Label to a Network Interface
Netplan also allows you to assign labels to network interfaces. A label is a user-friendly name that can be used to identify a network interface instead of its system-assigned name (e.g., eth0 or ens33). Assigning a label can make it easier to manage and configure network interfaces, especially in complex network setups.
To assign a label to a network interface using Netplan, follow these steps:
- Open a terminal window.
- Locate the Netplan configuration file for your network interface, as explained in the previous section.
- Edit the Netplan configuration file using a text editor, as explained in the previous section.
- Inside the configuration file, locate the network interface section to which you want to assign a label.
- Add the following line below the network interface section:
match:
name: interface_name
set-name: new_label
Replace interface_name with the system-assigned name of your network interface (e.g., eth0 or ens33). Replace new_label with the label you want to assign to the network interface.
- Save the changes and exit the text editor.
- Apply the Netplan configuration by running the following command:
sudo netplan apply.
Netplan will apply the changes and assign the label to your network interface. You can verify the changes by running the ip link show command in the terminal. You should see the label listed alongside the system-assigned name of your network interface.
By following these steps, you can easily add a secondary IP address and assign a label to a network interface using Netplan. This will allow you to configure and manage your network settings more effectively.
References
| Source | Link |
|---|---|
| Ubuntu Netplan Documentation | https://netplan.io/ |
| Ubuntu Documentation - Netplan | https://ubuntu.com/server/docs/network-netplan |