ubuntu中防火墙iptables配置
特别说明:此文章转载于(Ubuntu配置iptables防火墙)
搭建web服务器希望通过默认http的默认端口80去访问,由于云服务器的80端口是默认不开启的,所以需要重新配置iptables,使浏览器能够直接访问。
特别注意一定要使用超管权限,不然部分语句会报错
1.查看系统是否安装防火墙
root@localhost:/usr# which iptables
/sbin/iptables
root@localhost:/usr# whereis iptables
iptables: /sbin/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz
如果是这样的信息,那么表明iptables就是安装了的。
如果没有安装,那么使用sudo apt-get install iptables
安装。
2.查看防火墙的配置信息
运行命令iptables -L
如果成功配置应该是下面这幅图展现的内容,如果没有配置,则显示的是红框的内容。
3.新建规则文件
mkdir /etc/iptables #先新建目录,本身无此目录
vim /etc/iptables/rules.v4
文件rules.v4的内容编辑如下
*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:syn-flood - [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
-A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
-A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
-A syn-flood -j REJECT --reject-with icmp-port-unreachable
COMMIT
4.使防火墙生效
iptables-restore < /etc/iptables/rules.v4
5.创建文件,添加以下内容,使防火墙开机启动
vim /etc/network/if-pre-up.d/iptables
文件iptables的内容编辑如下
#!/bin/bash
iptables-restore < /etc/iptables/rules.v4
当然针对云服务器,没有必要,一直都处于开机状态
6.添加文件的执行权限
chmod +x /etc/network/if-pre-up.d/iptables
7.查看规则是否生效
iptables -L -n
Ubuntu中没有直接停止关闭iptables的命令,像service iptables stop这类命令,是centos才有的。关闭的话,可以暂时开放所有端口作为替代方案
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT