Skip to content

Commit c297635

Browse files
committed
Applied suggested edits on blog post '2022-12-13-tut-access-to-public-ip-from-vms-containers-using-firewalld'
1 parent 9cc5f47 commit c297635

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

blog/_posts/2022-12-13-tut-access-to-public-ip-from-vms-containers-using-firewalld.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,42 @@ You are running some LXC containers on a host and you use Firewalld to forward p
1111

1212
## TL;DR
1313
- Ordinairly connections from containers to to services which are reached by port forwarding on Firewalld fail, because after outputting the packet to the public interface, it never returns and therefore Firewalld cannot process the port forward.
14-
- The soloution is destination NAT: `sudo firewall-cmd --zone=trusted --add-rich-rule='rule family="ipv4" destination address="123.123.123.123" forward-port port="80" protocol="tcp" to-port="80" to-addr="10.10.1.20"'`
14+
- The soloution is destination NAT: `sudo firewall-cmd --zone=trusted --add-rich-rule='rule family="ipv4" destination address="203.0.113.1" forward-port port="80" protocol="tcp" to-port="80" to-addr="10.10.1.20"'`
1515

1616
## The Scenario
1717
You have some LXC containers running on a host, the default LXD setup creates a virtual bridge to which all the containers are connected, they have their own private network say in the 10.10.1.0/24 subnet.
1818

19-
You use Firewalld to forward ports from the public internet to the containers. In this scenario when a container does a DNS lookup to which the answer is the public IP address of the LXD host and the container then tries to connect to say port 80 on that public IP it will fail. Why? The HTTP request is received on the input chain by firewalld, no processing is required as it appears to be destined for the public internet. Firewalld outputs the packet to the public interface of the host. Since the HTTP Proxy is not bound to the public interface, it is instead reached via port forwards in Firewalld, the connection fails. This is because Firewalld handles the port forwarding and, after outputting the packet to the public interface, it never returns and therefore Firewalld cannot process the port forward.
19+
You use Firewalld to forward ports from the public internet to the containers. In this scenario when a container does a DNS lookup to which the answer is the public IP address of the LXD host and the container then tries to connect to say port 80 on that public IP it will fail.
20+
21+
Why? The HTTP request is received on the input chain by firewalld, the packet is then output to the public interface of the host. Since the HTTP Proxy is not bound to the public interface the connection fails. It instead must be reached via port forwards in Firewalld. After outputting the packet to the public interface, it never returns and therefore Firewalld cannot process the port forward.
2022

2123
## The Solution
2224
Destination NAT rules in firewalld are the solution here.
2325

2426
I understand NAT would appear to be an obvious answer. Indeed if you simply enable masquerading on the zone which contains the container virtual network this will begin to work, this has the unintended consequence of also source NATing all incoming requests to the containers. This means client IPs will no longer visible to applications running in containers.
2527

26-
Destination NAT is applied on the input chain, before the routing decision, where it modifies the destination IP address of the packet based. In the example the diagram describes Firewalld recognises that the destination IP for the HTTP request is the public IP of the host. After this it takes the packet and changes the destination IP address to the internal IP address of the Web Proxy Container.
28+
Destination NAT is applied on the prerouting chain, before the routing decision, where it modifies the destination IP address of the packet based. In the example the diagram describes Firewalld recognises that the destination IP for the HTTP request is the public IP of the host. After this it takes the packet and changes the destination IP address to the internal IP address of the Web Proxy Container.
2729

2830
To setup DST-NAT on the example host, you would follow this procedure:
2931

3032
1. Execute these commands on the Firewalld host:
3133
```
32-
sudo firewall-cmd --zone=trusted --add-rich-rule='rule family="ipv4" destination address="123.123.123.123" forward-port port="80" protocol="tcp" to-port="80" to-addr="10.10.1.20"'
33-
sudo firewall-cmd --zone=trusted --add-rich-rule='rule family="ipv4" destination address="123.123.123.123" forward-port port="443" protocol="tcp" to-port="443" to-addr="10.10.1.20"'
34+
sudo firewall-cmd --zone=trusted --add-rich-rule='rule family="ipv4" destination address="203.0.113.1" forward-port port="80" protocol="tcp" to-port="80" to-addr="10.10.1.20"'
35+
sudo firewall-cmd --zone=trusted --add-rich-rule='rule family="ipv4" destination address="203.0.113.1" forward-port port="443" protocol="tcp" to-port="443" to-addr="10.10.1.20"'
3436
```
35-
**Explainer:**
36-
- These two rules apply DST-NAT to packets destined for 123.123.123.123, port 80 and port 443.
37+
__Explanation:__
38+
- These two rules apply DST-NAT to packets destined for 203.0.113.1, port 80 and port 443.
3739
- You will need to make sure `--zone=` matches the zone you have your container virtual network bound to.
38-
- In this example 123.123.123.123 is our public IP address, change this to match yours
40+
- In this example 203.0.113.1 is our public IP address, change this to match yours
3941
- In this example 10.10.1.20 is the internal IP address of the container running HTTP reverse proxy. Change this IP to match your setup.
40-
- If you add other protocols which where handled by port forwarding, you would just continue adding rules with the appropriate port numbers.eriy
42+
- If you add other protocols which where handled by port forwarding, you would just continue adding rules with the appropriate port numbers.
4143

4244
2. Verify the setup works. If it does, make the changes permanent by executing the following:
4345
```
44-
sudo firewall-cmd --permanent --zone=trusted --add-rich-rule='rule family="ipv4" destination address="123.123.123.123" forward-port port="80" protocol="tcp" to-port="80" to-addr="10.10.1.20"'
45-
sudo firewall-cmd --permanent --zone=trusted --add-rich-rule='rule family="ipv4" destination address="123.123.123.123" forward-port port="443" protocol="tcp" to-port="443" to-addr="10.10.1.20"'
46+
sudo firewall-cmd --permanent --zone=trusted --add-rich-rule='rule family="ipv4" destination address="203.0.113.1" forward-port port="80" protocol="tcp" to-port="80" to-addr="10.10.1.20"'
47+
sudo firewall-cmd --permanent --zone=trusted --add-rich-rule='rule family="ipv4" destination address="203.0.113.1" forward-port port="443" protocol="tcp" to-port="443" to-addr="10.10.1.20"'
4648
```
47-
**Explainer:**
49+
__Explanation:__
4850
- These two commands are the same as those from the first step with the simple addition of the `--permanent` flag to make them permanent
4951

5052
-----

0 commit comments

Comments
 (0)