Skip to content

Instantly share code, notes, and snippets.

@mcgrew
Created July 9, 2013 17:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcgrew/5959115 to your computer and use it in GitHub Desktop.
Save mcgrew/5959115 to your computer and use it in GitHub Desktop.
Systemd service to enable NAT routing.
wan_interface=ens32
lan_interface=ens33
[Unit]
Description=Routing
Wants=network.target
Before=network.target
[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=/etc/conf.d/routing
ExecStart=/usr/bin/iptables --table nat --append POSTROUTING --out-interface ${wan_interface} -j MASQUERADE
ExecStart=/usr/bin/iptables --append FORWARD --in-interface ${lan_interface} -j ACCEPT
ExecStart=/usr/bin/echo 1 > /proc/sys/net/ipv4/ip_forward
ExecStop=/usr/bin/iptables --flush
ExecStop=/usr/bin/iptables --table nat --flush
ExecStop=/usr/bin/iptables --delete-chain
ExecStop=/usr/bin/iptables --table nat --delete-chain
ExecStop=/usr/bin/echo 0 > /proc/sys/net/ipv4/ip_forward
[Install]
WantedBy=multi-user.target
@enricostano
Copy link

Is this still the way to go or maybe we can manage the IP forwarding differently since http://cgit.freedesktop.org/systemd/systemd/commit/?id=5a8bcb674f71a20e95df55319b34c556638378ce ? Thanks!

@kapil1024
Copy link

@mcgrew @enricostano, do you think below configuration can replace the systemd service described above?

#wan
[Match]
Name=ens32
[Network]
DHCP=ipv4
IPMasquerade=yes

#lan
[Match]
Name=ens33
[Network]
Address=0.0.0.0/28
DHCPServer=yes
IPForward=yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment