This guide details how to override the default gateway assigned by DHCP.
-
Open the Network Configuration File Use
nano
(or any text editor of your choice) to open the network configuration file:sudo nano /usr/lib/systemd/network/80-ec2.network
to add the Gateway=172.31.96.100 line.
-
View the Network Configuration File Content After editing, you can view the contents to confirm the changes:
cat /usr/lib/systemd/network/80-ec2.network
Below is an example of the content inside the 80-ec2.network
file:
[Match]
Driver=ena ixgbevf vif
[Link]
MTUBytes=9001
[Network]
DHCP=yes
IPv6DuplicateAddressDetection=0
LLMNR=no
DNSDefaultRoute=yes
Gateway=172.31.96.100
[DHCPv4]
UseHostname=no
UseDNS=yes
UseNTP=yes
UseDomains=yes
[DHCPv6]
UseHostname=no
UseDNS=yes
UseNTP=yes
WithoutRA=solicit
- Check the Current Route Table
Before restarting the network service, check the current route table:
Expected Output:
route -n
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.31.96.1 0.0.0.0 UG 512 0 0 enX0 172.31.0.2 172.31.96.1 255.255.255.255 UGH 512 0 0 enX0 172.31.96.0 0.0.0.0 255.255.240.0 U 512 0 0 enX0 172.31.96.1 0.0.0.0 255.255.255.255 UH 512 0 0 enX0
- Restart the Network Service
Apply any changes made by restarting the
systemd-networkd
service:sudo systemctl restart systemd-networkd
- Check the Updated Route Table
After restarting, verify that the changes are effective:
Updated Output:
route -n
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.31.96.100 0.0.0.0 UG 0 0 0 enX0 0.0.0.0 172.31.96.1 0.0.0.0 UG 512 0 0 enX0 172.31.0.2 172.31.96.1 255.255.255.255 UGH 512 0 0 enX0 172.31.96.0 0.0.0.0 255.255.240.0 U 512 0 0 enX0 172.31.96.1 0.0.0.0 255.255.255.255 UH 512 0 0 enX0
- Ping Test
Finally, perform a ping test to confirm network connectivity:
Output:
ping -c3 www.google.com
PING www.google.com (172.253.62.99) 56(84) bytes of data. 64 bytes from bc-in-f99.1e100.net (172.253.62.99): icmp_seq=1 ttl=104 time=2.75 ms 64 bytes from bc-in-f99.1e100.net (172.253.62.99): icmp_seq=2 ttl=104 time=2.30 ms 64 bytes from bc-in-f99.1e100.net (172.253.62.99): icmp_seq=3 ttl=104 time=2.48 ms --- www.google.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 2.298/2.506/2.747/0.184 ms