Configure NAT using nftables
With nftables, you can configure the masquerading, source (SNAT), destination NAT (DNAT), and redirect types. With NAT, you can modify the source or destination IP address.
NAT types Copy linkLink copied!
The nftables framework supports different network address translation (NAT) types.
- Masquerading and source NAT (SNAT)
-
Use one of these NAT types to change the source IP address of packets. For example, Internet Service Providers (ISPs) do not route private IP ranges, such as
10.0.0.0/8. If you use private IP ranges in your network and users should be able to reach servers on the internet, map the source IP address of packets from these ranges to a public IP address.Masquerading and SNAT are very similar to one another. The differences are:
- Masquerading automatically uses the IP address of the outgoing interface. Therefore, use masquerading if the outgoing interface uses a dynamic IP address.
- SNAT sets the source IP address of packets to a specified IP and does not dynamically look up the IP of the outgoing interface. Therefore, SNAT is faster than masquerading. Use SNAT if the outgoing interface uses a fixed IP address.
- Destination NAT (DNAT)
- Use this NAT type to rewrite the destination address and port of incoming packets. For example, if your web server uses an IP address from a private IP range and is, therefore, not directly accessible from the internet, you can set a DNAT rule on the router to redirect incoming traffic to this server.
- Redirect
- This type is a special case of DNAT that redirects packets to the local machine depending on the chain hook. For example, if a service runs on a different port than its standard port, you can redirect incoming traffic from the standard port to this specific port.
Configure masquerading by using nftables Copy linkLink copied!
Masquerading enables a router to dynamically change the source IP of packets sent through an interface to the IP address of the interface. This means that if the interface gets a new IP assigned, nftables automatically uses the new IP when replacing the source IP.
About this task Copy linkLink copied!
Replace the source IP of packets leaving the host through the ens3 interface to the IP set on ens3.
Procedure Copy linkLink copied!
Configure source NAT using nftables Copy linkLink copied!
On a router, you can use Source NAT (SNAT) to change the IP of packets sent through an interface to a specific IP address. The router then replaces the source IP of outgoing packets.
Procedure Copy linkLink copied!
Configure destination NAT using nftables Copy linkLink copied!
Use Destination NAT (DNAT) to redirect traffic on a router to a host that is not directly accessible from the internet.
About this task Copy linkLink copied!
For example, with DNAT the router redirects incoming traffic sent to port 80 and 443 to a web server with the IP address 192.0.2.1.
Procedure Copy linkLink copied!
Configure a redirect using nftables Copy linkLink copied!
The redirect feature is a special case of destination network address translation (DNAT) that redirects packets to the local machine depending on the chain hook.
About this task Copy linkLink copied!
For example, you can redirect incoming and forwarded traffic sent to port 22 of the local host to port 2222.