snat — Shorewall6 SNAT/Masquerade definition file




This file is used to define dynamic NAT (Masquerading) and to define Source NAT (SNAT). While still supported, its use is deprecated in favor of shorewall6-snat(5) which was introduced in Shorewall 5.0.14.


The entries in this file are order-sensitive. The first entry that matches a particular connection will be the one that is used.


If you have more than one ISP link, adding entries to this file will not force connections to go out through a particular link. You must use entries in shorewall6-rtrules(5) or PREROUTING entries in shorewall-tcrules(5) to do that.

The columns in the file are as follows.


Defines the type of rule to generate. Choices are:


Causes matching outgoing packages to have their source IP address set to the primary IP address of the interface specified in the DEST column. if lowport-highport is given, that port range will be used to assign a source port. If option random is used then port mapping will be randomized. MASQUERADE should only be used when the DEST interface has a dynamic IP address. Otherwise, SNAT should be used and should specify the interface's static address.


If you specify an address here, matching packets will have their source address set to that address. If ADD_SNAT_ALIASES is set to Yes or yes in shorewall6.conf(5) then Shorewall will automatically add this address to the INTERFACE named in the first column.

You may also specify a range of up to 256 IP addresses if you want the SNAT address to be assigned from that range in a round-robin fashion by connection. The range is specified by You may follow the port range with :random in which case assignment of ports from the list will be random. random may also be specified by itself in this column in which case random local port assignments are made for the outgoing connections.


You may follow the port range (or :random) with :persistent. This is only useful when an address range is specified and causes a client to be given the same source/destination IP pair. This feature replaces the SAME modifier which was removed from Shorewall in version 4.4.0.

You may also use the special value detect which causes Shorewall to determine the IP addresses configured on the interface named in the DEST column and substitute them in this column.

Finally, you may also specify a comma-separated list of ranges and/or addresses in this column.

DNS Names names are not allowed.

Normally, Netfilter will attempt to retain the source port number. You may cause netfilter to remap the source port by following an address or range (if any) by ":" and a port range with the format lowport-highport. If this is done, you must specify "tcp", "udp", "dccp" or "stcp" in the PROTO column.



Causes matching packets to be exempted from any following rules in the file.


where action is an action declared in shorewall6-actions(5) with the nat option. See for further information.

Normally Masq/SNAT rules are evaluated after those for one-to-one NAT (defined in shorewall6-nat(5)). If you want the rule to be applied before one-to-one NAT rules, follow the action name with "+": This feature should only be required if you need to insert rules in this file that preempt entries in shorewall6-nat(5).

SOURCE (Optional) - [interface|address[,address...][exclusion]]

Set of hosts that you wish to SNAT; one or more host or network addresses separated by comma. You may use ipset names preceded by a plus sign (+) to specify a set of hosts.

DEST - {interface|[:[dest-address[,dest-address]...[exclusion]]|?COMMENT}

Outgoing interface. This is usually your internet interface.

The interface must match an entry in shorewall6-interfaces(5). Shorewall allows loose matches to wildcard entries in shorewall6-interfaces(5). For example, ppp0 in this file will match a shorewall6-interfaces(5) entry that defines ppp+.

Where more that one internet provider share a single interface, the provider is specified by including the provider name or number in parentheses:


In that case, you will want to specify the interface's address for that provider as the SNAT parameter.

The interface may be qualified by adding the character ":" followed by a comma-separated list of destination host or subnet addresses to indicate that you only want to change the source IP address for packets being sent to those particular destinations. Exclusion is allowed (see shorewall6-exclusion(5)) as are ipset names preceded by a plus sign '+'.

Comments may be attached to Netfilter rules generated from entries in this file through the use of ?COMMENT lines. These lines begin with ?COMMENT; the remainder of the line is treated as a comment which is attached to subsequent rules until another ?COMMENT line is found or until the end of the file is reached. To stop adding comments to rules, use a line containing only ?COMMENT.

PROTO (Optional) - {-|[!]{protocol-name|protocol-number}[,...]|+ipset}

If you wish to restrict this entry to a particular protocol then enter the protocol name (from protocols(5)) or number here. See shorewall6-rules(5) for details.

Beginning with Shorewall 4.5.12, this column can accept a comma-separated list of protocols.

Beginning with Shorewall 4.6.0, an ipset name can be specified in this column. This is intended to be used with bitmap:port ipsets.

DPORT (Optional) - {-|[!]port-name-or-number[,port-name-or-number]...|+ipset}

If the PROTO column specifies TCP (6), UDP (17), DCCP (33), SCTP (132) or UDPLITE (136) then you may list one or more port numbers (or names from services(5)) or port ranges separated by commas.

Port ranges are of the form lowport:highport.

Beginning with Shorewall 4.6.0, an ipset name can be specified in this column. This is intended to be used with bitmap:port ipsets.

IPSEC (Optional) - [option[,option]...]

If you specify a value other than "-" in this column, you must be running kernel 2.6 and your kernel and iptables must include policy match support.

Comma-separated list of options from the following. Only packets that will be encrypted via an SA that matches these options will have their source address changed.


where number is specified using setkey(8) using the 'unique:number option for the SPD level.


where number is the SPI of the SA used to encrypt/decrypt packets.


IPSEC Encapsulation Protocol


sets the MSS field in TCP packets


IPSEC mode


only available with mode=tunnel


only available with mode=tunnel


Means that packets must match all rules.


Separates rules; can only be used with strict


When used by itself, causes all traffic that will be encrypted/encapsulated to match the rule.

MARK - [!]value[/mask][:C]

Defines a test on the existing packet or connection mark. The rule will match only if the test returns true.

If you don't want to define a test but need to specify anything in the following columns, place a "-" in this field.


Inverts the test (not equal)


Value of the packet or connection mark.


A mask to be applied to the mark before testing.


Designates a connection mark. If omitted, the packet mark's value is tested.

USER (Optional) - [!][user-name-or-number][:group-name-or-number][+program-name]

Only locally-generated connections will match if this column is non-empty.

When this column is non-empty, the rule matches only if the program generating the output is running under the effective user and/or group specified (or is NOT running under that id if "!" is given).



program must be run by joe


program must be run by a member of the 'kids' group


program must not be run by a member of the 'kids' group


#program named upnpd


The ability to specify a program name was removed from Netfilter in kernel version 2.6.14.

SWITCH - [!]switch-name[={0|1}]

Added in Shorewall 4.5.1 and allows enabling and disabling the rule without requiring shorewall restart.

The rule is enabled if the value stored in /proc/net/nf_condition/switch-name is 1. The rule is disabled if that file contains 0 (the default). If '!' is supplied, the test is inverted such that the rule is enabled if the file contains 0.

Within the switch-name, '@0' and '@{0}' are replaced by the name of the chain to which the rule is a added. The switch-name (after '@...' expansion) must begin with a letter and be composed of letters, decimal digits, underscores or hyphens. Switch names must be 30 characters or less in length.

Switches are normally off. To turn a switch on:

echo 1 > /proc/net/nf_condition/switch-name

To turn it off again:

echo 0 > /proc/net/nf_condition/switch-name

Switch settings are retained over shorewall restart.

Beginning with Shorewall 4.5.10, when the switch-name is followed by =0 or =1, then the switch is initialized to off or on respectively by the start command. Other commands do not affect the switch setting.

ORIGDEST - [-|address[,address]...[exclusion]|exclusion]

(Optional) This column may be included and may contain one or more addresses (host or network) separated by commas. Address ranges are not allowed. When this column is supplied, rules are generated that require that the original destination address matches one of the listed addresses. It is useful for specifying that SNAT should occur only for connections that were acted on by a DNAT when they entered the firewall.

This column was formerly labelled ORIGINAL DEST.

PROBABILITY - [probability]

Added in Shorewall 5.0.0. When non-empty, requires the Statistics Match capability in your kernel and ip6tables and causes the rule to match randomly but with the given probability. The probability is a number 0 < probability <= 1 and may be expressed at up to 8 decimal points of precision.


Example 1:

You have a simple 'masquerading' setup where eth0 connects to a DSL or cable modem and eth1 connects to your local network with subnet 2001:470:b:787::0/64

Your entry in the file will be:

        #ACTION      SOURCE                  DEST
        MASQUERADE   2001:470:b:787::0/64    eth0
Example 2:

Your sit1 interface has two public IP addresses: 2001:470:a:227::1 and 2001:470:b:227::1. You want to use the iptables statistics match to masquerade outgoing connections evenly between these two addresses.


       #ACTION                      SOURCE     DEST
       SNAT(2001:470:a:227::1)      ::/0       sit1              { probability=0.50 }
       SNAT(2001:470:a:227::2)      ::/0       sit




Frequently Used Articles

- FAQs - IPv4 Manpages - IPv6 Manpages - Configuration File Basics - Beginner Documentation - Troubleshooting

Shorewall 4.4/4.5/4.6 Documentation

Shorewall 4.0/4.2 Documentation

Shorewall 5.0 HOWTOs and Other Articles

- 6to4 and 6in4 Tunnels - Accounting - Actions - Aliased (virtual) Interfaces (e.g., eth0:0) - Anatomy of Shorewall - Anti-Spoofing Measures - AUDIT Target support - Bandwidth Control - Blacklisting/Whitelisting - Bridge/Firewall - Building Shorewall from GIT - Commands - Compiled Programs - Configuration File Basics - DHCP - DNAT - Docker - Dynamic Zones - ECN Disabling by host or subnet - Events - Extension Scripts - Fallback/Uninstall - FAQs - Features - Fool's Firewall - Forwarding Traffic on the Same Interface - FTP and Shorewall - Helpers/Helper Modules - Installation/Upgrade - IPP2P - IPSEC - Ipsets - IPv6 Support - ISO 3661 Country Codes - Kazaa Filtering - Kernel Configuration - KVM (Kernel-mode Virtual Machine) - Limiting Connection Rates - Linux Containers (LXC) - Linux-vserver - Logging - Macros - MAC Verification - Manpages (IPv4) (IPv6) - Manual Chains - Masquerading - Multiple Internet Connections from a Single Firewall - Multiple Zones Through One Interface - My Shorewall Configuration - Netfilter Overview - Network Mapping - No firewalling of traffic between bridge port - One-to-one NAT - Operating Shorewall - OpenVPN - OpenVZ - Packet Marking - Packet Processing in a Shorewall-based Firewall - 'Ping' Management - Port Forwarding - Port Information - Port Knocking (deprecated) - Port Knocking, Auto Blacklisting and Other Uses of the 'Recent Match' - PPTP - Proxy ARP - QuickStart Guides - Release Model - Requirements - Routing and Shorewall - Routing on One Interface - Samba - Shorewall Events - Shorewall Init - Shorewall Lite - Shorewall on a Laptop - Shorewall Perl - Shorewall Setup Guide - SMB - SNAT - Split DNS the Easy Way - Squid with Shorewall - Starting/stopping the Firewall - Static (one-to-one) NAT - Support - Tips and Hints - Traffic Shaping/QOS - Simple - Traffic Shaping/QOS - Complex - Transparent Proxy - UPnP - Upgrade Issues - Upgrading to Shorewall 4.4 (Upgrading Debian Lenny to Squeeze) - VPN - VPN Passthrough - White List Creation - Xen - Shorewall in a Bridged Xen DomU - Xen - Shorewall in Routed Xen Dom0

Top of Page