Skip to content

Instantly share code, notes, and snippets.

@tankywoo
Created May 22, 2013 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tankywoo/5624887 to your computer and use it in GitHub Desktop.
Save tankywoo/5624887 to your computer and use it in GitHub Desktop.
iptables startup script, put it in /etc/init.d/
#!/bin/bash
#
### BEGIN INIT INFO
# Provides: iptables
# Required-Start: mountkernfs $local_fs
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Set up iptables rules
### END INIT INFO
if [ $(id -u) -ne 0 ] ; then
CMD_IPTABLES="sudo iptables"
else
CMD_IPTABLES="iptables"
fi
IPTABLES_RAW="${CMD_IPTABLES} -t raw"
CONF_DIR="/etc/iptables"
IPTABLES_RULES=${CONF_DIR}/iptables_rules
IPSET_RULES=${CONF_DIR}/ipset_rules
case "$1" in
start)
if [[ -f ${IPSET_RULES} && -f ${IPTABLES_RULES} ]]; then
ipset restore < ${IPSET_RULES}
iptables-restore < ${IPTABLES_RULES}
fi
;;
stop)
${CMD_IPTABLES} -F
${CMD_IPTABLES} -X
${CMD_IPTABLES} -Z
${IPTABLES_RAW} -F
${IPTABLES_RAW} -X
ipset destroy
;;
restart)
$0 stop
$0 start
;;
status)
echo "Filter Rules:"
echo "--------------"
${CMD_IPTABLES} -L -v
echo ""
echo "Raw Rules:"
echo "--------------"
${CMD_IPTABLES} -t raw -L -v
echo ""
echo "NAT Rules:"
echo "-------------"
${CMD_IPTABLES} -t nat -L -v
echo ""
echo "Mangle Rules:"
echo "----------------"
${CMD_IPTABLES} -t mangle -L -v
;;
*)
echo "Usage: $0 {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0
@cbr1
Copy link

cbr1 commented Feb 20, 2018

thk!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment