Setting up Firewall with IPTables
2 minutes read •
- Flush all
iptablesrules.
- List all rules.
# With line numbers
- Change default
INPUTpolicy fromACCEPTtoDROP. WARNING!: MIGHT BLOCK YOUR SSH CONNECTIONS!-Por--policyto set default policy.
- Block All connections from an IP.
-Ifor Inserting the rule.-Ato Append.-sis Source IP.-jis the Operation/Target (ACCEPT/DROP/REJECT).
# Block only one IP
# Block an entire subnet
- Delete rules. First list the rules with line numbers to see the line number of the rule you wanna delete.
# Usage
# Example
- Block all connections to a specific port on the server.
- Accept from an IP to a port on the server.
Making Changes permanent
As IP-Tables are not persistent, they will be deleted (“flushed”) with the next reboot.
- Once you are happy with your ruleset, save the new rules to the master iptables file:
- To make sure the iptables rules are started on a reboot we’ll create a new file:
- Add these lines to it:
#!/bin/sh
- The file needs to be executable so change the permissions:
- Rules can be stored something like this:
Another way is to use the package iptables-persistent.
- Install it.
- Enable the service.
Useful Rules
- Accepts all established inbound connections (IMPORTANT!)
- Allow SSH, HTTP, HTTPS.
- Allow ICMP ping (echo).
Note that blocking other types of icmp packets is considered a bad idea by some remove -m icmp –icmp-type 8 from this line to allow all kinds of icmp: https://security.stackexchange.com/questions/22711
- Default Deny Input and Allow Output.
Referances: