I'm working on a server and am supposed to configure it, such that requests on port 8080 are allowed only from a specific IP. (On other http(s) ports nothing is listening anyway.)
Everything else shall stay unrestricted.
After researching about /etc/host
and iptables I wrote this script to activate (and deactivate) that rule:
#!/bin/bash
if ["$1" != 'restrict'] && ["$1" != 'restore']
then
echo "Usage:"
echo "'${0##*/} restrict' to activate rules."
echo "'${0##*/} restore' to deactivate rules."
echo "'${0##*/}' to show this help."
exit 0
fi
# Set default policies for INPUT, FORWARD and OUTPUT chains
# Allowing everything else.
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
# Flush all current rules from iptables
iptables -F
if [$1 == "restrict"]
then
# Allow HTTP connections on tcp port 8080 from X.X.X.X
# [I]nserting the rules as first in the chain.
iptables -I INPUT -p tcp --dport 8080 -s X.X.X.X -j ACCEPT
# Denying all other connections on tcp port 8080
# [A]ppending the rule as last in the chain.
iptables -A INPUT -p tcp --dport 8080 -j DROP
fi
# Save settings
/sbin/service iptables save
# List rules
iptables -L -v
Since I only have access to the machine via SSH, I don't wanna screw up and lock myself out. Thus I'd like to ask if this script
- works?
- will do what is desired?
- won't do anything else?
- stays nonvolatile?