Ubuntu 24.04 has introduced updates to its Netplan configuration, including the deprecation of the gateway4 directive. Instead, routes are now defined explicitly using the routes section. This guide will cover how to configure a Static IP, DHCP, and multi-routing with metrics using the updated Netplan syntax.


Step 1: Configuring a Static IP Address

Static IPs are critical for servers, ensuring a persistent network address.

  • Locate the Netplan Configuration File: Netplan configuration files are located in /etc/netplan/. List them with:
   ls /etc/netplan/

Example file: 01-netcfg.yaml.

  • Edit the Configuration File: Open the configuration file with a text editor:
   nano /etc/netplan/01-netcfg.yaml

Modify the file to include the static IP configuration:

   network:
     version: 2
     renderer: networkd
     ethernets:
       eth0:
         addresses:
           - 192.168.1.100/24
         routes:
           - to: default
             via: 192.168.1.1
         nameservers:
           addresses:
             - 8.8.8.8
             - 8.8.4.4

Explanation:

  • addresses: Specifies the static IP (192.168.1.100/24).
  • routes: Defines the default gateway (via: 192.168.1.1).
  • nameservers: Configures DNS servers (8.8.8.8 and 8.8.4.4).
  • Apply the Configuration: Save the file and apply the settings:
   netplan apply
  • Verify the Configuration: Check the assigned IP address and routing:
   ip a
   ip route

Step 2: Configuring DHCP

If you prefer dynamic IP assignment, configure your interface for DHCP.

  • Edit the Netplan File: Open the file:
   nano /etc/netplan/01-netcfg.yaml

Update the configuration:

   network:
     version: 2
     renderer: networkd
     ethernets:
       eth0:
         dhcp4: true
  • Apply the Configuration: Save and apply the changes:
   netplan apply
  • Verify the Configuration: Check the dynamically assigned IP:
   ip a

Step 3: Configuring Multi-Routing with Metrics

Multi-routing allows you to configure multiple gateways, prioritizing traffic with metrics.

  • Edit the Netplan File: Open the Netplan configuration file:
   nano /etc/netplan/01-netcfg.yaml

Update it for multi-routing:

   network:
     version: 2
     renderer: networkd
     ethernets:
       eth0:
         addresses:
           - 192.168.1.100/24
         routes:
           - to: default
             via: 192.168.1.1
             metric: 100
       eth1:
         addresses:
           - 192.168.2.100/24
         routes:
           - to: default
             via: 192.168.2.1
             metric: 200

Explanation:

  • eth0 and eth1: Two interfaces with different gateways.
  • metric: Determines route priority. Lower metrics have higher priority.
  • Apply the Configuration: Save and apply the configuration:
   netplan apply
  • Verify the Routing Table: Check the routing table to confirm configuration:
   ip route

Expected output:

   default via 192.168.1.1 dev eth0 proto static metric 100
   default via 192.168.2.1 dev eth1 proto static metric 200

Step 4: Testing Multi-Routing

  • Test Primary Route: Test the primary route with:
   traceroute -n google.com

Traffic should flow through the primary gateway (eth0).

  • Simulate Gateway Failure: Disable the primary interface:
   ip link set eth0 down

Re-run the traceroute:

   traceroute -n google.com

Traffic will now route through the secondary gateway (eth1).

  • Restore Primary Interface: Enable the primary interface again:
   ip link set eth0 up

Troubleshooting

YAML Syntax Validation:

Use yamllint to ensure there are no syntax errors in your configuration file:

yamllint /etc/netplan/01-netcfg.yaml

Connectivity Issues:

  • Verify the gateways are reachable:
  ping 192.168.1.1
  ping 192.168.2.1
  • Reapply the Netplan configuration if changes are not reflected:
  netplan apply

For robust and scalable hosting solutions, consider Shape.host’s Cloud VPS services. Shape.host provides a high-performance environment for Ubuntu 24.04 servers, ideal for projects requiring advanced network configurations like multi-routing.