This service is designed to provide a simple firewall and NAT (Network Address Translation) functionality for Linux systems. It allows you to set up basic firewall rules and enable NAT to share an internet connection with devices on a local network.
graph TD
A[Internet] --> B[wlan0]
B --> C[Firewall & NAT]
C <--> D[eth0]
E1[Ethernet connected IoT device] <--> D
D <--> F[Machine running Linux]
subgraph Components
F --> G1[iptables]
F --> G2[kmod]
F --> G3[netfilter-persistent]
F --> G4[iptables-persistent]
end
style A fill:#f96,stroke:#333,stroke-width:2px,color:#000
style B fill:#69f,stroke:#333,stroke-width:2px,color:#000
style C fill:#9f9,stroke:#333,stroke-width:2px,color:#000
style D fill:#fc9,stroke:#333,stroke-width:2px,color:#000
style E1 fill:#9cf,stroke:#333,stroke-width:2px,color:#000
style F fill:#c9f,stroke:#333,stroke-width:2px,color:#000
style G1 fill:#f9f,stroke:#333,stroke-width:1px,color:#000
style G2 fill:#ff9,stroke:#333,stroke-width:1px,color:#000
style G3 fill:#9ff,stroke:#333,stroke-width:1px,color:#000
style G4 fill:#9f9,stroke:#333,stroke-width:1px,color:#000
To ensure the firewall and NAT script operates correctly, you need to have the following packages installed on your system:
Used to set up, maintain, and inspect the tables of IP packet filter rules in the Linux kernel.
Used for managing kernel modules, which are necessary for loading the appropriate network-related modules required by the script.
Provides a framework for managing firewall rules across reboots.
Saves and restores iptables rules upon system startup and shutdown.
Install the required packages using the following commands:
sudo apt-get update
sudo apt-get install iptables kmodsudo apt-get install netfilter-persistent iptables-persistentTo verify the installation of iptables, you can run:
iptables --versionkmod --versionsudo systemctl status netfilter-persistent
sudo systemctl status iptables-persistentEnsure that these packages are properly installed and configured before running the firewall and NAT script.
The script loads necessary kernel modules using depmod and modprobe.
Enables IP forwarding and dynamic address handling.
Clears any existing iptables rules and sets default policies to ensure a clean state before applying new rules.
Allows outgoing connections from eth0 to wlan0. Allows established and related incoming connections from wlan0 to eth0. Logs any other forwarded packets.
Enables SNAT (Source Network Address Translation) on wlan0 to allow devices on eth0 to access the internet via wlan0.
The stop_firewall function disables IP forwarding and clears all iptables rules, reverting to default policies.
Save the script as /etc/init.d/simple-firewall-nat.
chmod +x /etc/init.d/simple-firewall-nat
#### Start the Firewall:
service simple-firewall-nat start
#### Stop the Firewall:
service simple-firewall-nat stop
#### Restart the Firewall:
service simple-firewall-nat restart
#### Check the Status:
service simple-firewall-nat statusThis script is compatible with a raspberrypi / docker setup and provide the necessary firewall functionality, allowing the management of NAT and forwarding rules for network interfaces.