iptable 常用策略及其命令

iptable 常用策略及其命令

1.说明

精简于《Linux宝典 第四版》340-346页

2.表

说明
-t filter
-t NAT
-t mangle

3.查看当前的规则

iptables -L

4.drop 不同链上的包

iptables -p INPUT DROP
iptables -p OUTPUT DROP
iptables -p FORWARD DROP

5.接收或拒绝ICMP包

iptables -A INPUT -p ICMP -i eth0 -s 0/0 --icmp-type 8 -j ACCEPT
iptables -A INPUT -p ICMP -i eth0 -s 0/0 --icmp-type 11 -j ACCEPT

–icmp-type

  1. 类型8 允许计算机接收 回应回复消息 即可以ping
  2. 类型11 传输生存周期(TTL)数据包

任意来源

-s 0/0

4.配置localhost 的输入

iptables -A INPUT -p ALL -i lo -s 127.0.0.1 -j ACCEPT	//本地主机自身
iptables -A INPUT -p ALL -i lo -s 10.0.0.1 -j ACCEPT	//局域网
iptables -A INPUT -p ALL -i lo -s 124.45.67.89 -j ACCEPT //公网ip

5.配置eth1 的输入

iptables -A INPUT -p ALL -i eth1 -s 10.0.0.1/24 -j ACCEPT
iptables -A INPUT -p ALL -i eth1 -d 10.0.0.123 -j ACCEPT

6.配置输出

iptables -A OUTPUT -p ALL -s 127.0.0.1 -j ACCEPT	//本地主机自身
iptables -A OUTPUT -p ALL -s 10.0.0.1 -j ACCEPT		//局域网
iptables -A OUTPUT -p ALL -s 124.45.67.89 -j ACCEPT //公网ip
iptables -A OUTPUT -p ALL -i eth1 -s 10.0.0.1/24 -j ACCEPT
iptables -A OUTPUT -p ALL -i eth1 -d 10.0.0.123 -j ACCEPT

7.允许来自internet的某些包进入

iptables -A INPUT -p TCP -i eth0 -s 0/0  --destination-port 21 -j ACCEPT
iptables -A INPUT -p TCP -i eth0 -s 0/0  --destination-port 22 -j ACCEPT
iptables -A INPUT -p TCP -i eth0 -s 0/0  --destination-port 80 -j ACCEPT
iptables -A INPUT -p TCP -i eth0 -s 0/0  --destination-port 113 -j ACCEPT
iptables -A INPUT -p UDP -i eth0 -s 0/0  --destination-port 53 -j ACCEPT
iptables -A INPUT -p UDP -i eth0 -s 0/0  --destination-port 2074 -j ACCEPT
iptables -A INPUT -p UDP -i eth0 -s 0/0  --destination-port 4000 -j ACCEPT

–destination-port

端口号作用
21FTP服务
22安全shell服务
80web服务
113IDENRD认证

8.保存防火墙的设置

iptables-save > /root/iptables //自己定义的目录

9.恢复防火墙的设置

iptables-restore < /root/iptables

10.Linux 自动装载的 iptables配置的路径

如Fedora系统中
/etc/sysconfig/iptables

11.用nmap工具检查防火墙

在另一台计算机上输入一下命令

nmap 124.45.67.89 //待检查的主机的ip

12.使局域网上的计算机使用私有IP地址通过防火墙访问Internet

假设与Internet的链接是etho提供的

IP伪装(MASQUERADE)法

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE 

资源网络地址转化(source network address translation)

iptables -t nat -A POSTROUTING -o erh0 -j SNAT --to-source 12.12.12.12
//从etho 流出的包的source ip修改为12.12.12.12

对比

MASQUERADE会有一些额外开销,但是如果是拨号链接到Internet,ip地址不固定,就需要使用它。

13.开启Linux内核的 IP转发

临时

echo 1 > /proc/sys/net/ipv4/ip_forward
sysctl net.ipv4.ip_forward=1

永久

vi /etc/sysctl.conf
net.ipv4.ip_forward=1

14.开启Linux内核的 动态IP寻址

临时

echo 1 > /proc/sys/net/ipv4/ip_dynaddr
sysctl net.ipv4.ip_dynaddr=1

永久

vi /etc/sysctl.conf
net.ipv4.ip_dynaddr=1

15.通过iptables添加模块

modprobe ip_conntrack_irc.o ports=6668,6669

IRC 模块的默认端口是6667 可以在加载时添加其他端口号

16.使用iptables进行端口转发

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 3128

17.使用iptables配置透明代理

iptables -t nat -A PREROUTING -p tcp -d 15.15.15.15 --dport 80 -j DNAT
--to-ports 3128  --to-destination 10.0.0.25

负载分散

 --to-destination 10.0.0.1-10.0.0.25

18.iptables 脚本

在网上有许多现成的脚本
推荐 Oskar Andreasson – "Iptables Tutorial"的作者的一套脚本
http://iptables-tutorial.frozentux.net/scripts/
rc.firewall.txt

19.进一步了解iptables

http://iptables-tutorial.frozentux.net
www.netfilter.org
www.linuxguruz.com/iptables

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值