Title: Understanding Default Routing and Overriding it on Ubuntu Server AWS EC2 when losing connection after VPN client configuration
Introduction
In this article, we will discuss the issue of losing connection on an Ubuntu Server AWS EC2 after configuring a VPN client. We will cover key concepts such as default routing, understanding when it gets overridden, and solutions to resolve the issue.
Default Routing
By default, AWS EC2 instances use the instance's private IP address for internal communication and the public IP address for external communication. The routing table on the instance determines how traffic is directed.
Overriding Default Routing
When you configure a VPN client on an Ubuntu Server AWS EC2, it may override the default routing table. This can cause the instance to lose its connection to the internet.
Solutions
Static Route
You can add a static route to the routing table to direct traffic through the VPN interface. Here's an example of how to add a static route:
sudo ip route add default via <VPN_GATEWAY> dev <VPN_INTERFACE>
Replace <VPN_GATEWAY> with the IP address of the VPN gateway and <VPN_INTERFACE> with the name of the VPN interface.
iptables
Another solution is to use iptables to redirect all outgoing traffic through the VPN interface. Here's an example of how to do this:
sudo iptables -t nat -A POSTROUTING -o <VPN_INTERFACE> -j MASQUERADE
Replace <VPN_INTERFACE> with the name of the VPN interface.
Code Block Example
# Add a static route
sudo ip route add default via 10.8.0.1 dev tun0
# Redirect all outgoing traffic through the VPN interface
sudo iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
References
Summary
When configuring a VPN client on an Ubuntu Server AWS EC2, the default routing table may get overridden, causing the instance to lose its connection to the internet. We discussed two solutions to this issue: adding a static route and using iptables to redirect all outgoing traffic through the VPN interface.