linux iptables 配置总结(WEB服务器)
来源:互联网 发布:桌面切换软件 编辑:程序博客网 时间:2024/05/17 01:59
前段时间一朋友的web服务器让人家给捅了,导致了服务器流量异常,被机房警告了。我帮忙看了下,重新配置了防火墙,暂时解决了。顺便把iptables的配置做了总结,以备以后查询。
iptables是基于内核的防火墙,功能非常强大,iptables内置了filter,nat和mangle三张表。我们一般就使用filter就可以了,也就是过滤数据包。filter 又可以分为INPUT 、OUTPUT 、FORWARD 三条规则链条,分别代表了入站、出站、转发。
1、查看当前防火墙规则
# iptables -L
默认防火墙装好后是没有规则的,如下
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
如果有规则,视情况决定是否要清除所有的规则重建。清除规则前重点查看 (policy ACCEPT) ,如果为 (policy DROP) ,你一旦把规则清楚了,那所有的请求都会拒绝,也就是说你在用ssh远程操作的话,你也会被拒绝,连不上了。先设置防火墙默认接受所有的请求,如下操作:
# iptables -P INPUT ACCEPT
# iptables -P FORWARD ACCEPT
# iptables -P OUTPUT ACCEPT
然后再清空规则:
# iptables -F
# iptables -X
2、查看当前防火墙配置文件
# more /etc/sysconfig/iptables
如果没有该文件,则执行创建命令
# /etc/rc.d/init.d/iptables save
3、修改防火墙配置文件
# vi /etc/sysconfig/iptables
*filter
#表示filter表
:INPUT DROP [0:0]
#入站规则为丢弃,默认不能与外部通讯
:FORWARD DROP [0:0]
#转发规则为丢弃,默认不能与外部通讯
:OUTPUT DROP [0:0]
#出站规则为丢弃,默认不能与外部通讯
#开始设置入站规则---------------------------------------
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#设置当连接状态为RELATED和ESTABLISHED时,允许数据进入服务器。
-A INPUT -i lo -j ACCEPT
#允许内部数据循回
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
#允许SSH请求连接
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
#允许网站80端口连接
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 11 -j ACCEPT
#允许服务器响应PING
#开始设置出站规则---------------------------------------
-A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
#设置当连接状态为RELATED和ESTABLISHED时,允许数据出服务器。比如用户访问网站的数据返回
-A OUTPUT -p udp -m udp --dport 53 -j ACCEPT
#允许访问外部DNS,比如网站发通知邮件时需要解析smtp主机
-A OUTPUT -o lo -j ACCEPT
#允许内部数据循回
-A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT
#允许访问外网网站(注意这里是主动访问外网,不需要可以去掉)
-A OUTPUT -p tcp -m tcp --dport 25 -j ACCEPT
#允许发邮件
-A OUTPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A OUTPUT -p icmp -m icmp --icmp-type 11 -j ACCEPT
#允许ping外网
COMMIT
4、重启生效
# service iptables restart
重启防火墙时,会自动加载/etc/sysconfig/iptables里的规则
5、查看防火墙的执行情况
# iptables -L -n -v
如下:
Chain INPUT (policy DROP 179K packets, 13M bytes)
pkts bytes target prot opt in out source destination
302K 110M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
796 52238 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
6615 396K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
1429 81144 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80
18 568 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmp type 8
1 96 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmp type 11
Chain FORWARD (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 17842 packets, 1394K bytes)
pkts bytes target prot opt in out source destination
297K 177M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
10567 777K ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:53
796 52238 ACCEPT all -- * lo 0.0.0.0/0 0.0.0.0/0
76 4560 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:25
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmp type 8
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmp type 11
0 0
- linux iptables 配置总结(WEB服务器)
- linux WEB全能服务器配置(总结)
- linux WEB全能服务器配置(总结)
- iptables WEB服务器配置示例
- linux web服务器,防火墙iptables最简配置
- linux 下纯web服务器iptables 规则
- 服务器iptables配置(1)
- 阿里云服务器linux配置iptables(1)
- 使用iptables配置linux服务器请求转发
- 使用iptables配置linux服务器请求转发
- linux云服务器iptables防火墙快速配置
- linux下常用iptables配置实例总结
- web服务器iptables策略
- Linux iptables配置(上)
- Linux下通过iptables配置工具限制ip访问服务器
- linux配置web服务器常用命令
- linux中iptables总结
- Linux(CentOS)下,防火墙iptables配置
- 蓝桥杯 回文数
- pushlet 之 Pushlet使用手把手实例
- POJ 3273 Monthly Expense (二分枚举)
- 正则入门:边界的定义与相对性
- oracle日期函数大全
- linux iptables 配置总结(WEB服务器)
- (hdu step 3.1.3)母牛的故事(简单递推)
- HDOJ 4499 Cannon
- Pushlet的工作原理
- poj2965The Pilots Brothers' refrigerator
- "cannot be resolved or is not a field"问题解决
- Java程序中异常的相关知识
- PHP微缩图片
- Too many arguments to function call ,expected 0,have3