美文网首页
iptable学习与配置

iptable学习与配置

作者: 水电梁师傅 | 来源:发表于2018-03-24 14:56 被阅读22次

    sudo iptables -h > ~/iptables_help

    iptables 有表 (tables)
    的概念,每张表又包含不同链 (chains)
    ,大部分情况下我们仅需要使用 filter 和 nat 两张表的链就可以完成功能。
    使用以下命令可以查看 filter 表中的规则。
    sudo iptables -L -n
    参数解析 -L 列出规则, -n 不显示域名,命令默认显示的是 filter 表,可以通过 -t 参数来指定其他表

    假设我们作为 Linux 管理员,不希望有人从本机 ping 百度的服务器,那么就可以用到如下的 iptables 命令:
    sudo iptables -I OUTPUT -p icmp -d www.baidu.com -j DROP
    参数解析 -I 添加规则到链的最前面, -p 匹配协议, -d 匹配目的地址, -j DROP 将匹配的数据包实施丢掉动作

    在进行一系列复杂的防火墙配置时,大多数时候不建议直接调用 iptables 做规则修改。 因为任何一条错误的配置或者一个 typo (输入错误)都有可能导致严重的网络问题。
    一个最佳实践是导出现有规则到文本文件,对该文本文件进行编辑,进行检查无误后再将其导入。
    导出规则
    通过如下命令导出所有规则到文本文件
    sudo iptables-save > /home/ubuntu/iptables_rules

    -A OUTPUT -p icmp -d 114.114.114.114 -j DROP
    sudo iptables -A BLACKLIST -d 220.181.111.188 -j DROP
    如上配置后,会发现本机已经无法 ping BLACKLIST 中的目的地址了。
    如果后续要增加新的禁止访问的目的地址,则只需向 BLACKLIST 链添加新规则。
    如果需要限制另一台 PC 通过本网关转发访问外网,则只需添加一条 OUTPUT 链的源 IP 匹配规则并指向 -j BLACKLIST。

    使用命令将编辑过的规则重新导入 iptables
    sudo iptables-restore /home/ubuntu/iptables_rules
    使用 sudo iptables -L -n 查看是否有禁止 ping 114.114.114.114 的新增规则
    使用 sudo ping 114.114.114.114 查看新规则是否生效

    清除全部规则
    sudo iptables -F

    这时候 telnet 0 80 发现 80 端口可以工作了。
    退出telnet需要按 Ctrl + ] (右方括号) ,再按 q 回车。
    下面我们用 iptables 做一个端口映射
    sudo iptables -t nat -A OUTPUT -p tcp -d 127.0.0.1 --dport 8080 -j DNAT --to 127.0.0.1:80
    这里我们用到了 -t nat 参数,表示我们使用了 netfilter 的 nat 表。在 nat 表的 OUTPUT 链上做了一个 -j DNAT 转发,将访问内网 8080 端口的数据包转向了 80 端口。
    这时候 telnet 0 8080 发现 8080 端口也可以工作了。
    如果是监听外网的 8080 端口转发到 80 端口,则需要执行以下命令:
    sudo iptables -t nat -A PREROUTING -p tcp --dport 8080 -j REDIRECT --to-ports 80
    通过使用 PREROUTING 链直接将访问 8080 端口的数据包转发到 80 端口

    先检查是否安装了iptables

    service iptables status

    安装iptables

    yum install -y iptables

    升级iptables

    yum update iptables

    安装iptables-services

    yum install iptables-services -y

    vi /etc/sysconfig/iptables

    查看iptables现有规则

    iptables -L -n

    先允许所有,不然有可能会杯具

    iptables -P INPUT ACCEPT

    清空所有默认规则

    iptables -F

    清空所有自定义规则

    iptables -X

    所有计数器归0

    iptables -Z

    允许来自于lo接口的数据包(本地访问)

    iptables -A INPUT -i lo -j ACCEPT
    仔细看上面并没有列出 -s, -d 等等的规则,这表示:不论封包来自何处或去到哪里,只要是来自 lo 这个界面,就予以接受!这个观念挺重要的,就是『没有指定的项目,则表示该项目完全接受』的意思! 例如这个案例当中,关于 -s, -d...等等的参数没有规定时,就代表不论什么值都会被接受啰。

    这就是所谓的信任装置啦!假如你的主机有两张以太网络卡,其中一张是对内部的网域,假设该网卡的代号为 eth1 好了, 如果内部网域是可信任的,那么该网卡的进出封包就通通会被接受,那你就能够用:『iptables -A INPUT -i eth1 -j ACCEPT』 来将该装置设定为信任装置。不过,下达这个指令前要特别注意,因为这样等于该网卡没有任何防备了喔!

    开放22端口

    iptables -A INPUT -p tcp --dport 22 -j ACCEPT

    开放21端口(FTP)

    iptables -A INPUT -p tcp --dport 21 -j ACCEPT

    开放80端口(HTTP)

    iptables -A INPUT -p tcp --dport 80 -j ACCEPT

    开放443端口(HTTPS)

    iptables -A INPUT -p tcp --dport 443 -j ACCEPT

    允许ping

    iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT

    允许接受本机请求之后的返回数据 RELATED,是为FTP设置的

    iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

    其他入站一律丢弃

    iptables -P INPUT DROP

    所有出站一律绿灯

    iptables -P OUTPUT ACCEPT

    所有转发一律丢弃

    iptables -P FORWARD DROP
    iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT

    iptables -A INPUT -p tcp -m multiport --dport 20,21 -m state --state NEW -j ACCEPT --开启20,21端口
    iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT --开启21主动端口
    iptables -A INPUT -p tcp --dport 30000:31000 -j ACCEPT --开启被动端口

    ls -l /opt/test_ftp

    http://www.cnblogs.com/wolf-sun/p/6074614.html

    相关文章

      网友评论

          本文标题:iptable学习与配置

          本文链接:https://www.haomeiwen.com/subject/livfcftx.html