iptables

iptables配置

    iptables -L #列出规则

        -F 清除已有规则

        -n 不显示主机名


    iptables -I INPUT -p tcp --dport 22:80 -j ACCEPT #允许访问22到80端口

    iptables -I INPUT -p icmp -j ACCEPT #允许ICMP

    iptables -A INPUT -j REJECT #屏蔽其它访问

    iptables -D INPUT -p tcp --dport 80 -j ACCEPT #先删除

    iptables -I INPUT -p tcp --dport 80 -j REJECT #后拒绝

    iptables -nL #不显示主机名列出规则

    iptables -I INPUT -i lo -j ACCEPT #本机可以访问自己

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

    #本机可以访问其它主机

    iptables -D INPUT -p tcp --dport 80 -j ACCEPT #先删除

    iptables -I INPUT -p tcp -s 192.168.1.112 --dport 80 -j ACCEPT

    #只允许指定ip访问本机80端口

【如关闭8080端口:

iptables -I INPUT -p tcp --dport 8080 -j ACCEPT】

ftp主动模式(开放20端口) 客户端必须#iptables -F

    #vim /etc/vsftpd/vsftpd.conf #配置文件

    port_enable=yes

    connect_from_port_20=YES

    #iptables -I INPUT -p tcp --dport 21 -j ACCEPT #允许访问21端口 

ftp被动模式(开放'任意'端口)(默认)

    #vim /etc/vsftpd/vsftpd.conf

    port_enable=NO

    pasv_min_port=50000

    pasv_max_port=60000

    #iptables -I INPUT -p tcp --dport 21 -j ACCEPT #打开21号端口

    #iptables -I INPUT -p tcp --dport 50000:60000 -j ACCEPT

    #modprobe nf_conntrack_ftp #临时建立ftp被动

    #modprobe -l #查看内核模块

    #vim /etc/sysconfig/iptables-config #永久建立ftp被动

    IPTABLES_MODULES="nf_conntrack_ftp" #设置参数

常见端口

    http 80/tcp

    https 443/tcp

    smtp 25/tcp

    smtps 465/tcp

    pop3 110/tcp

    nfs 123/udp

    samba 137,138,139/tcp 445/tcp

    ftp 20/tcp,21/tcp

    ssh 22/tcp

    mysql 3306/tcp

    oracle 1521/tcp

规则设置

    iptables -F

    iptables -I INPUT -i lo -j ACCEPT

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

    iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT #指定网段

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

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

    iptables -I INPUT -p icmp -j ACCEPT

    iptables -A INPUT -j REJECT

    /etc/sysconfig/iptables #配置文件

    /etc/init.d/iptables save #执行以保存iptables设置

nat表规则配置

    SNAT 源地址转换  出口POSTROUTING

    DNAT 目标地址转换 进口PREROUTING


    iptables -t nat -F

    iptables -t nat -A PREROUTING -d 192.168.1.112 -p tcp --dport 80 -j DNAT

    --to 192.168.2.112:80

    iptables -t nat -L

6. 利用iptables防CC攻击

    connlimit模块 #限制每一个客户端ip的并发连接数

        --connlimit-above n #限制并发个数

        iptables -I INPUT -p tcp --syn --dport 80 -m connlimit

        --connlimit-above 100 -j REJECT

        iptables -I INPUT -p tcp --dport 80 -s 192.168.1.112 -m connlimit

        --connmlimit-above 10 -j REJECT

    Limit模快 #限速,控制流量

        iptables -A INPUT -m limit --limit 3/hour  #--limit-burst默认值为5


        iptables -A INPUT -p icmp -m limit --limit 1/m --limit-burst 10 -j ACCEPT

        iptables -A INPUT -p icmp -j DROP

1、关闭所有的 INPUT FORWARD OUTPUT 只对某些端口开放。

下面是命令实现:

iptables -P INPUT DROP

iptables -P FORWARD DROP

iptables -P OUTPUT DROP

再用命令 iptables -L -n 查看 是否设置好, 好看到全部 DROP 了

这样的设置好了,我们只是临时的, 重启服务器还是会恢复原来没有设置的状态

还要使用 service iptables save 进行保存

看到信息 firewall rules 防火墙的规则 其实就是保存在 /etc/sysconfig/iptables

可以打开文件查看 vi /etc/sysconfig/iptables

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容