• iptables详解


    iptables中基本的命令参数

    iptables是一款基于命令行的防火墙策略管理工具,具有大量参数,学习难度较大。好在对于日常的防火墙策略配置来讲,大家无需深入了解诸如“四表五链”的理论概念,只需要掌握常用的参数并做到灵活搭配即可,这就足以应对日常工作了。

    iptables命令可以根据流量的源地址、目的地址、传输协议、服务类型等信息进行匹配,一旦匹配成功,iptables就会根据策略规则所预设的动作来处理这些流量。另外,再次提醒一下,防火墙策略规则的匹配顺序是从上至下的,因此要把较为严格、优先级较高的策略规则放到前面,以免发生错误。表8-1总结归纳了常用的iptables命令参数。再次强调,我们无需死记硬背这些参数,只需借助下面的实验来理解掌握即可。

    表8-1                                              iptables中常用的参数以及作用

    参数

    作用

    -P

    设置默认策略

    -F

    清空规则链

    -L

    查看规则链

    -A

    在规则链的末尾加入新规则

    -I num

    在规则链的头部加入新规则

    -D num

    删除某一条规则

    -s

    匹配来源地址IP/MASK,加叹号“!”表示除这个IP外

    -d

    匹配目标地址

    -i网卡名称

    匹配从这块网卡流入的数据

    -o网卡名称

    匹配从这块网卡流出的数据

    -p

    匹配协议,如TCP、UDP、ICMP

    --dport num

    匹配目标端口号

    --sport num

    匹配来源端口号

     

    在iptables命令后添加-L参数查看已有的防火墙规则链

     

    [root@xiejun ~]# iptables -L

    Chain INPUT (policy ACCEPT)

    target prot opt source destination 

    ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED

    ACCEPT all -- anywhere anywhere 

    INPUT_direct all -- anywhere anywhere 

    INPUT_ZONES_SOURCE all -- anywhere anywhere 

    INPUT_ZONES all -- anywhere anywhere 

    ACCEPT icmp -- anywhere anywhere 

    REJECT all -- anywhere anywhere reject-with icmp-host-prohibited

    ………………省略部分输出信息………………

     

    在iptables命令后添加-F参数清空已有的防火墙规则链

     

    [root@xiejun ~]# iptables -F

    [root@xiejun ~]# 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

    ………………省略部分输出信息………………

     

    把INPUT规则链的默认策略设置为拒绝

     

    [root@xiejun ~]# iptables -P INPUT DROP

    [root@xiejun ~]# iptables -L

    Chain INPUT (policy DROP)

    target prot opt source destination 

    …………省略部分输出信息………………

     

    前文提到,防火墙策略规则的设置有两种:通和堵。当把INPUT链设置为默认拒绝后,就要在防火墙策略中写入允许策略了,否则所有到来的流量都会被拒绝掉。另外,需要注意的是,规则链的默认拒绝动作只能是DROP,而不能是REJECT。

    向INPUT链中添加允许ICMP流量进入的策略规则

    在日常运维工作中,经常会使用ping命令来检查对方主机是否在线,而向防火墙的INPUT规则链中添加一条允许ICMP流量进入的策略规则就默认允许了这种ping命令检测行为。

     

    [root@xiejun ~]# iptables -I INPUT -p icmp -j ACCEPT

    [root@xiejun ~]# ping -c 4 192.168.10.10

    PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.

    64 bytes from 192.168.10.10: icmp_seq=1 ttl=64 time=0.156 ms

    64 bytes from 192.168.10.10: icmp_seq=2 ttl=64 time=0.117 ms

    64 bytes from 192.168.10.10: icmp_seq=3 ttl=64 time=0.099 ms

    64 bytes from 192.168.10.10: icmp_seq=4 ttl=64 time=0.090 ms

    --- 192.168.10.10 ping statistics ---

    4 packets transmitted, 4 received, 0% packet loss, time 2999ms

    rtt min/avg/max/mdev = 0.090/0.115/0.156/0.027 ms

     

    删除INPUT规则链中刚刚加入的那条策略(允许ICMP流量),并把默认策略设置为允许

     

    [root@xiejun ~]# iptables -D INPUT 1

    [root@xiejun ~]# iptables -P INPUT ACCEPT

    [root@xiejun ~]# iptables -L

    Chain INPUT (policy ACCEPT)

    target prot opt source destination

    ………………省略部分输出信息………………

     

    将INPUT规则链设置为只允许指定网段的主机访问本机的22端口,拒绝来自其他所有主机的流量

     

    [root@xiejun ~]# iptables -I INPUT -s 192.168.10.0/24 -p tcp --dport 22 -j 

    ACCEPT

    [root@xiejun ~]# iptables -A INPUT -p tcp --dport 22 -j REJECT

    [root@xiejun ~]# iptables -L

    Chain INPUT (policy ACCEPT)

    target prot opt source destination 

    ACCEPT tcp -- 192.168.10.0/24 anywhere tcp dpt:ssh

    REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable

    ………………省略部分输出信息………………

     

    再次重申,防火墙策略规则是按照从上到下的顺序匹配的,因此一定要把允许动作放到拒绝动作前面,否则所有的流量就将被拒绝掉,从而导致任何主机都无法访问我们的服务。另外,这里提到的22号端口是ssh服务使用的(有关ssh服务,请见下一章),刘遄老师先在这里挖坑,等大家学完第9章后可再验证这个实验的效果。

    在设置完上述INPUT规则链之后,我们使用IP地址在192.168.10.0/24网段内的主机访问服务器(即前面提到的设置了INPUT规则链的主机)的22端口,效果如下:

     

    [root@Client A ~]# ssh 192.168.10.10

    The authenticity of host '192.168.10.10 (192.168.10.10)' can't be established.

    ECDSA key fingerprint is 70:3b:5d:37:96:7b:2e:a5:28:0d:7e:dc:47:6a:fe:5c.

    Are you sure you want to continue connecting (yes/no)? yes

    Warning: Permanently added '192.168.10.10' (ECDSA) to the list of known hosts.

    root@192.168.10.10's password: 此处输入对方主机的root管理员密码

    Last login: Sun Feb 12 01:50:25 2017

    [root@Client A ~]#

     

    然后,我们再使用IP地址在192.168.20.0/24网段外的主机访问服务器的22端口,效果如下,就不会提示连接请求被拒绝了(Connection failed):

     

    [root@Client B ~]# ssh 192.168.10.10

    Connecting to 192.168.10.10:22...

    Could not connect to '192.168.10.10' (port 22): Connection failed.

     

    向INPUT规则链中添加拒绝所有人访问本机12345端口的策略规则

     

    [root@xiejun ~]# iptables -I INPUT -p tcp --dport 12345 -j REJECT

    [root@xiejun ~]# iptables -I INPUT -p udp --dport 12345 -j REJECT

    [root@xiejun ~]# iptables -L

    Chain INPUT (policy ACCEPT)

    target prot opt source destination 

    REJECT udp -- anywhere anywhere udp dpt:italk reject-with icmp-port-unreachable

    REJECT tcp -- anywhere anywhere tcp dpt:italk reject-with icmp-port-unreachable

    ACCEPT tcp -- 192.168.10.0/24 anywhere tcp dpt:ssh

    REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable

    ………………省略部分输出信息………………

     

    向INPUT规则链中添加拒绝192.168.10.5主机访问本机80端口(Web服务)的策略规则

     

    [root@xiejun ~]# iptables -I INPUT -p tcp -s 192.168.10.5 --dport 80 -j REJECT

    [root@xiejun ~]# iptables -L

    Chain INPUT (policy ACCEPT)

    target prot opt source destination 

    REJECT tcp -- 192.168.10.5 anywhere tcp dpt:http reject-with icmp-port-unreachable

    REJECT udp -- anywhere anywhere udp dpt:italk reject-with icmp-port-unreachable

    REJECT tcp -- anywhere anywhere tcp dpt:italk reject-with icmp-port-unreachable

    ACCEPT tcp -- 192.168.10.0/24 anywhere tcp dpt:ssh

    REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable

    ………………省略部分输出信息………………

     

    向INPUT规则链中添加拒绝所有主机访问本机10001024端口的策略规则

     

    [root@xiejun ~]# iptables -A INPUT -p tcp --dport 1000:1024 -j REJECT

    [root@xiejun ~]# iptables -A INPUT -p udp --dport 1000:1024 -j REJECT

    [root@xiejun ~]# iptables -L

    Chain INPUT (policy ACCEPT)

    target prot opt source destination 

    REJECT tcp -- 192.168.10.5 anywhere tcp dpt:http reject-with icmp-port-unreachable

    REJECT udp -- anywhere anywhere udp dpt:italk reject-with icmp-port-unreachable

    REJECT tcp -- anywhere anywhere tcp dpt:italk reject-with icmp-port-unreachable

    ACCEPT tcp -- 192.168.10.0/24 anywhere tcp dpt:ssh

    REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable

    REJECT tcp -- anywhere anywhere tcp dpts:cadlock2:1024 reject-with icmp-port-

    unreachable

    REJECT udp -- anywhere anywhere udp dpts:cadlock2:1024 reject-with icmp-port-

    unreachable

    ………………省略部分输出信息………………

     

    有关iptables命令的知识讲解到此就结束了,大家是不是意犹未尽?考虑到Linux防火墙的发展趋势,大家只要能把上面的实例吸收消化,就可以完全搞定日常的iptables配置工作了。但是请特别注意,使用iptables命令配置的防火墙规则默认会在系统下一次重启时失效,如果想让配置的防火墙策略永久生效,还要执行保存命令:

     

    [root@xiejun ~]# service iptables save

    iptables: Saving firewall rules to /etc/sysconfig/iptables: [ OK ]

  • 相关阅读:
    iOS开发 代码 或 <Home+Power>截屏
    正弦水波纹波动画
    CAGradientLayer + UIBezierPath 为视图画渐变背景色
    字符串(String)和 字符(Character)
    Python+selenium爬取智联招聘的职位信息
    用Python写一个随机密码生成器
    golang文件处理函数openfile与linux系统的文件函数的耦合
    单向列表的实现代码,以及注释解释笔记
    CLion中出现错误add_dependencies called with incorrect number of arguments解决
    c语言指针的简单实例
  • 原文地址:https://www.cnblogs.com/smileface/p/8483523.html
Copyright © 2020-2023  润新知