Fundamentals 6 min read

Understanding SNAT and DNAT: Definitions, How They Work, and Practical iptables Examples

This article explains the concepts of Source NAT (SNAT) and Destination NAT (DNAT), their mechanisms, primary use cases such as address sharing, load balancing, and security, and provides step‑by‑step iptables commands for implementing these rules in typical network scenarios.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Understanding SNAT and DNAT: Definitions, How They Work, and Practical iptables Examples

SNAT (Source Network Address Translation) replaces the source IP address of outbound packets from an internal network with a public IP address, enabling multiple internal devices to share one or more public IPs for Internet access.

How SNAT Works

When an internal device sends a packet to the external network, a NAT device (router or firewall) intercepts the packet and substitutes its source IP with the configured public IP, so external recipients see traffic coming from a single public address.

Key SNAT Use Cases

Address Sharing : Multiple internal hosts use a single public IP, conserving public address space.

Load Balancing : Replace internal server source IPs with the load balancer’s IP to distribute traffic.

Security : Hide internal IPs from external attackers.

Simplified Network Design : Provides flexibility in address planning and reduces configuration complexity.

DNAT (Destination Network Address Translation)

DNAT changes the destination IP address of inbound packets from the external network to an internal IP, allowing external users to reach specific internal services via a public IP.

How DNAT Works

When a packet arrives at the NAT device, it checks the destination IP and port, then rewrites them to the configured internal IP and port before forwarding the packet to the target host.

Key DNAT Use Cases

Port Mapping : Map a public IP and port (e.g., 1.2.3.4:80) to an internal server’s IP and port for remote access or web hosting.

Load Balancing : Distribute incoming traffic across multiple internal servers based on load‑balancing policies.

Security : Conceal internal IP addresses and ports, enhancing network security.

Practical Scenario Example

Assume an internal network 192.168.1.0/24, a public IP 1.2.3.4, an internal server 192.168.1.100 offering a web service, and a NAT device with internal IP 192.168.1.1 and external IP 1.2.3.4.

3.1 Adding an SNAT Rule

# Add SNAT rule
# Change source address of packets from 192.168.1.0/24 to 1.2.3.4
# Assume outbound interface is eth0
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 1.2.3.4

3.2 Adding a DNAT Rule

# Add DNAT rule
# Redirect traffic destined for 1.2.3.4:80 to internal server 192.168.1.100:80
iptables -t nat -A PREROUTING -d 1.2.3.4 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.100:80

These iptables commands illustrate how to configure SNAT for outbound traffic and DNAT for inbound traffic, enabling proper communication between internal and external networks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

NetworkingNATSNATiptablesaddress translationDNAT
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.