#!/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 @ mansurali901@gmail.com # Defining interfaces for gateway. INTERNET=wlp1s0 LOCAL=enp2s0.1 # 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 # 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