What rules are needed for an Ubuntu LAMP server only having minimum necessary ports open.

  • 1
    Exactly how minimal are we talking though? Since MySQL and PHP can be communicated with directly internally (hence the first item which allows loopback/localhost communication), there's no need to have their ports open to the universe). – Thomas Ward Jul 7 '12 at 14:05

Minimal? Depending on how minimal we're talking, this'd work (I'm including SSH as being needed in this though):

iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p udp --dport 80 -j ACCEPT
iptables -A INPUT -j REJECT --reject-with icmp-host-unreachable

If you want to add other ports or services to be internet facing, those ports can be opened with iptables -I INPUT [numberOfRules] -p [protocol] --dport [port or port:range] -j ACCEPT, where [numberOfRules] is the number of the rule which has the REJECT target, [protocol] is the protocol (TCP, UDP, etc.), and [port or port:range] is either a single port or a range of ports defined with start:end. Using comma separation also seems to work there.

  • LIMITATIONS: This does not include rules to allow for pings under certain circumstances, nor does it allow for any other connections on other ports from the outside. – Thomas Ward Jul 7 '12 at 14:06
  • Thanks, this will do until I move the database to another server. – el_pup_le Jul 7 '12 at 14:12
  • This will allow for outbound communication though, so if the DB moves to another server you wont need to open another port. The conntrack item will allow for the communication with the external server though, because your server initiated the communication (RELATED to an initiated connection started by your server, and ESTABLISHED because its, well, established.) – Thomas Ward Jul 7 '12 at 14:14
  • ^ The above comment is not entirely correct, the ports on the external server that the DBs are going to reside on will need to be opened. – Thomas Ward Jul 9 '12 at 16:24
  • FOR EVERYONE: I have further expanded and explained this answer at my blog, and have thrown a link up in the answer. – Thomas Ward Aug 3 '12 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.