需求:
把80,22,21端口放行,但22端口指定一个IP段,只容许这个IP段的IP访问的时候,才可访问到,其余段的一律拒绝; 使用脚原本实现
RELATED状态,这是一个边缘的一个状态
好比:客户端和服务端相互了通讯,创建完链接以后,还会有一些额外的连接出来,这时候状态就变成了RELATED(若牢牢只有ESTABLISHED,而没有RELATED,颇有可能致使其余的通讯被禁掉,由于默认策略是INPUT DROP)
ESTABLISHED状态, 保持链接
有时,在没有增长-m --state这条规则,致使增长了80或21端口,可是不能正常通讯,加这条规则的目的是为了让通讯更加顺畅vim
编辑脚本bash
[root@yong-02 ~]# vim /usr/local/sbin/iptables.sh
添加内容:网络
#!/bin/bash ipt="/usr/sbin/iptables" #这里ipt是定义个一个变量(写脚本的时候,写全局的路径,就是绝对路径,就是后面再加载它 ,用变量去代替,看着更加简单) $ipt -F #清空以前的规则——>在没有 -t 指定表的时候,默认的就是filter表 $ipt -P INPUT DROP #把IPPUT的策略给扔掉 $ipt -P OUTPUT ACCEPT #把OUTPUT放行 $ipt -P FORWARD ACCEPT #把FORWARD放行 $ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT #增长规则,-m --state 指定了状态,并针对这些状态放行(-m --state这种用法并很少见, 可是这条规则必须写进来,目的是让相关>的数据包放行) $ipt -A INPUT -s 192.168.180.0/24 -p tcp --dport 22 -j ACCEPT #把该网段的22端口数据包放行——>这里的IP段根据本身的IP段来作实验 $ipt -A INPUT -p tcp --dport 80 -j ACCEPT #把80端口数据包放行 $ipt -A INPUT -p tcp --dport 21 -j ACCEPT #把21端口数据包放行
执行脚本:tcp
[root@yong-02 ~]# sh /usr/local/sbin/iptables.sh
[root@yong-02 ~]# iptables -nvL Chain INPUT (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 29 1924 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT tcp -- * * 192.168.180.0/24 0.0.0.0/0 tcp dpt:22 0 0 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:21 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 17 packets, 1492 bytes) pkts bytes target prot opt in out source destination
注意:为何这里要用脚本:由于这里有INPUT DROP,确定要把你的远程链接给断开,因此须要执行脚本把你的命令批量执行。spa
iptables -I INPUT -p icmp --icmp-type 8 -j DROP 这个规则会产生一个效果,让你ping外面的机器还能够ping通,可是ping本机的时候,就不会通了rest
[root@yong-02 ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP
注意:这里会发现可ping通外面的网络,但本身的虚拟机和物理机则没法链接code
[root@yong-02 ~]# ping www.qq.com PING www.qq.com (192.168.180.135) 56(84) bytes of data.
这时用本身的物理机去ping虚拟机,会发现没法链接ip
C:\Users\YueYong>ping 192.168.180.135 正在 Ping 192.168.180.135 具备 32 字节的数据: 请求超时。 请求超时。 请求超时。 请求超时。 192.168.180.135 的 Ping 统计信息: 数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),
这时,再来删除这条命令get
[root@yong-02 ~]# iptables -D INPUT -p icmp --icmp-type 8 -j DROP
service iptables restart 重启iptables服务虚拟机
[root@yong-02 ~]# service iptables restart //重启iptables服务 Redirecting to /bin/systemctl restart iptables.service [root@yong-02 ~]# iptables -nvL //这里会看到还没禁掉以前的规则 Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 34 2304 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 1 76 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 24 packets, 1980 bytes) pkts bytes target prot opt in out source destination