Software & AppsOperating SystemLinux

Correctly Routing Between Two Interfaces with Netplan in Ubuntu 18.04

Ubuntu 10

In this article, we will delve into the process of correctly routing between two interfaces using Netplan in Ubuntu 18.04. We’ll cover everything from the basics of Netplan to the detailed steps involved in the routing process.

Quick Answer

To correctly route between two interfaces with Netplan in Ubuntu 18.04, you need to configure the Netplan YAML file with the network interfaces and routing rules, enable IP forwarding, set up firewall rules using iptables, and apply the changes by restarting the networking service.

Introduction to Netplan

Netplan is a utility for easily configuring networking on a linux system. It reads YAML based configuration files which describe the network interfaces and, from these descriptions, will generate the necessary configuration options for any chosen renderer tool.

Pre-requisites

Before we begin, ensure you have:

  • A system running Ubuntu 18.04.
  • Two network interfaces that you want to route traffic between.
  • Root or sudo access to the system.

Removing Existing Route Rules

Firstly, it is important to remove any existing route rules that may interfere with the routing setup. You can do this using the ip route command.

Configuring Netplan

Open the Netplan configuration file with the command:

sudo nano /etc/netplan/<config-file>.yaml

Replace <config-file> with the actual name of your Netplan configuration file.

In this file, you will define the network interfaces and the routing rules. Here’s an example:

network:
 version: 2
 renderer: networkd
 ethernets:
 enp7s0:
 addresses: [172.16.0.1/30]
 routes:
 - to: 192.168.254.240/28
 via: 172.16.0.1
 on-link: true
 dhcp4: no
 enp8s0:
 addresses: [192.168.254.254/28]
 routes:
 - to: 172.16.0.0/30
 via: 192.168.254.254
 on-link: true
 dhcp4: no

In this configuration:

  • enp7s0 and enp8s0 are the names of the network interfaces.
  • addresses specifies the IP addresses assigned to the interfaces.
  • routes defines the routing rules. to is the destination network, via is the next hop IP address, and on-link specifies that the next hop is directly connected to this link.
  • dhcp4: no disables DHCP for IPv4.

Enabling IP Forwarding

Next, enable IP forwarding by uncommenting the line net.ipv4.ip_forward=1 in the /etc/sysctl.conf file. You can use the command sudo nano /etc/sysctl.conf to open the file and make the necessary changes.

Verify that IP forwarding is enabled by running the command cat /proc/sys/net/ipv4/ip_forward. It should return 1.

Configuring Firewall Rules

To allow traffic forwarding between the subnets, configure the firewall rules using the iptables commands:

sudo iptables -A FORWARD -i enp7s0 -j ACCEPT
sudo iptables -A FORWARD -i enp8s0 -j ACCEPT
sudo iptables -t nat -A POSTROUTING -o enp7s0 -j MASQUERADE
sudo iptables -t nat -A POSTROUTING -o enp8s0 -j MASQUERADE

In these commands:

  • FORWARD is the chain where packets are processed that are intended for another network.
  • -i specifies the network interface.
  • -j ACCEPT sets the policy to accept the packets.
  • -t nat specifies the NAT table.
  • POSTROUTING is the chain where packets are altered as they are about to go out.
  • -o specifies the network interface.
  • MASQUERADE is the target that masks the private IP address with the public IP address.

Save the iptables rules so that they persist after a reboot using the command sudo iptables-save > /etc/iptables/rules.v4.

Applying the Changes

Finally, restart the networking service to apply the changes by running the command sudo systemctl restart networking.

With these configurations in place, your system should be able to route traffic between the two network interfaces. If you encounter any issues, please provide more details about the problem, and we can further investigate the issue.

Conclusion

In this article, we have covered how to correctly route between two interfaces using Netplan in Ubuntu 18.04. This process involves configuring Netplan, enabling IP forwarding, setting up firewall rules, and applying the changes. By following these steps, you can ensure that your system routes traffic correctly between the two interfaces.

What is Netplan?

Netplan is a utility for easily configuring networking on a Linux system. It reads YAML based configuration files which describe the network interfaces and generates the necessary configuration options for any chosen renderer tool.

How do I remove existing route rules?

You can remove existing route rules using the ip route command. For example, to remove a route rule, you can use the command sudo ip route del <destination_network>.

How do I open the Netplan configuration file?

You can open the Netplan configuration file using the command sudo nano /etc/netplan/<config-file>.yaml. Replace <config-file> with the actual name of your Netplan configuration file.

How do I enable IP forwarding?

To enable IP forwarding, uncomment the line net.ipv4.ip_forward=1 in the /etc/sysctl.conf file. You can use the command sudo nano /etc/sysctl.conf to open the file and make the necessary changes.

How do I verify if IP forwarding is enabled?

You can verify if IP forwarding is enabled by running the command cat /proc/sys/net/ipv4/ip_forward. If it returns 1, IP forwarding is enabled.

How do I configure firewall rules for traffic forwarding?

You can configure firewall rules for traffic forwarding using the iptables commands. For example, to allow traffic forwarding between two interfaces, you can use commands like sudo iptables -A FORWARD -i <interface> -j ACCEPT and sudo iptables -t nat -A POSTROUTING -o <interface> -j MASQUERADE.

How do I save `iptables` rules to persist after a reboot?

You can save iptables rules to persist after a reboot by using the command sudo iptables-save > /etc/iptables/rules.v4.

How do I restart the networking service to apply the changes?

You can restart the networking service to apply the changes by running the command sudo systemctl restart networking.

Leave a Comment

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