Skip to content

Instantly share code, notes, and snippets.

@mansurali901
Created April 10, 2018 15:33
Show Gist options
  • Save mansurali901/8ec785a4efb7c93d1fb0228a934448f5 to your computer and use it in GitHub Desktop.
Save mansurali901/8ec785a4efb7c93d1fb0228a934448f5 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script is written to make your Linux machine Router
# With this you can setup your linux machine as gateway.
# Author @ Mansur Ul Hasan
# Email @ [email protected]
# Defining interfaces for gateway.
INTERNET=eth1
LOCAL=eth0
# IMPORTANT: Activate IP-forwarding in the kernel!
# Disabled by default!
echo "1" > /proc/sys/net/ipv4/ip_forward
# Load various modules. Usually they are already loaded
# (especially for newer kernels), in that case
# the following commands are not needed.
# Load iptables module:
modprobe ip_tables
# activate connection tracking
# (connection's status are taken into account)
modprobe ip_conntrack
# Special features for IRC:
modprobe ip_conntrack_irc
# Special features for FTP:
modprobe ip_conntrack_ftp
# Deleting all the rules in INPUT, OUTPUT and FILTER
iptables --flush
# Flush all the rules in nat table
iptables --table nat --flush
# Delete all existing chains
iptables --delete-chain
# Delete all chains that are not in default filter and nat table
iptables --table nat --delete-chain
# Allow established connections from the public interface.
iptables -A INPUT -i $INTERNET -m state --state ESTABLISHED,RELATED -j ACCEPT
# Set up IP FORWARDing and Masquerading
iptables --table nat --append POSTROUTING --out-interface $INTERNET -j MASQUERADE
iptables --append FORWARD --in-interface $LOCAL -j ACCEPT
# Allow outgoing connections
iptables -A OUTPUT -j ACCEPT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment