Skip to content

Instantly share code, notes, and snippets.

@phanletrunghieu
Forked from tehmoon/iptables-reload.sh
Created April 22, 2019 09:19
Show Gist options
  • Save phanletrunghieu/ec1c3305b879cac8b1da3f8757f77ac5 to your computer and use it in GitHub Desktop.
Save phanletrunghieu/ec1c3305b879cac8b1da3f8757f77ac5 to your computer and use it in GitHub Desktop.
IPtables and docker reload!
#!/bin/sh
set -e
## SEE https://medium.com/@ebuschini/iptables-and-docker-95e2496f0b45
CWD=$(cd "$(dirname "${0}")"; pwd -P)
FILE="${CWD}/$(basename "${0}")"
chown root:root "${FILE}"
chmod o-rwx "${FILE}"
## Restore/install the PREROUTING rules for the DOCKER-BLOCK chain
## Careful about the reverse order
/sbin/iptables -t nat -N DOCKER-BLOCK || true
/sbin/iptables -t nat -I PREROUTING -m addrtype --dst-type LOCAL -j RETURN
/sbin/iptables -t nat -I PREROUTING -m addrtype --dst-type LOCAL -j DOCKER-BLOCK
/sbin/iptables -t nat -D PREROUTING -m addrtype --dst-type LOCAL -j RETURN || true
/sbin/iptables -t nat -D PREROUTING -m addrtype --dst-type LOCAL -j DOCKER-BLOCK || true
## Restore/install the PREROUTING rules for the DOCKER chain in case docker starts after
/sbin/iptables -t nat -N DOCKER || true
/sbin/iptables -t nat -I PREROUTING -m addrtype --dst-type LOCAL -m state --state ESTABLISHED -j DOCKER
/sbin/iptables -t nat -D PREROUTING -m addrtype --dst-type LOCAL -m state --state ESTABLISHED -j DOCKER || true
## Flush the rules of DOCKER-BLOCK
/sbin/iptables -t nat -F DOCKER-BLOCK
#/sbin/iptables -t nat -A DOCKER-BLOCK -p tcp -m tcp --dport 8080 -m state --state NEW -j DOCKER
## The INPUT chain is set to drop, then we flush it and reinstall the rules.
## Finally we restore the policy on the chain
/sbin/iptables -t filter -P INPUT DROP
/sbin/iptables -t filter -F INPUT
/sbin/iptables -t filter -A INPUT -i lo -j ACCEPT
/sbin/iptables -t filter -A INPUT -p tcp -m tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
/sbin/iptables -t filter -A INPUT -m state --state ESTABLISHED -j ACCEPT
/sbin/iptables -t filter -A INPUT -j DROP
/sbin/iptables -t filter -P INPUT ACCEPT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment