Loading... <p>iptables是Linux上常用的防火墙软件,下面vps侦探给大家说一下iptables的安装、清除iptables规则、iptables只开放指定端口、iptables屏蔽指定ip、ip段及解封、删除已添加的iptables规则等iptables的基本应用。 <br />1、安装iptables防火墙 </p> <p>如果没有安装iptables需要先安装,CentOS执行: <br /><font color="#0080ff">yum install iptables</font> </p> <p>Debian/Ubuntu执行: <br /><font color="#00ff80">apt-get install iptables</font> <br />2、清除已有iptables规则 <br /><font color="#0080ff">iptables -F <br /> iptables -X <br /> iptables -Z</font> <br />3、开放指定的端口 <br />#允许本地回环接口(即运行本机访问本机) <br /> <font color="#0080ff">iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT</font> <br /> # 允许已建立的或相关连的通行 <br /><font color="#00ff80"> iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT</font> <br /> #允许所有本机向外的访问 <br /><font color="#0080ff"> iptables -A OUTPUT -j ACCEPT</font> <br /> # 允许访问22端口 <br /><font color="#0080ff"> iptables -A INPUT -p tcp --dport 22 -j ACCEPT</font> <br /> #允许访问80端口 <br /><font color="#0080ff"> iptables -A INPUT -p tcp --dport 80 -j ACCEPT</font> <br /> #允许FTP服务的21和20端口 <br /><font color="#0080ff"> iptables -A INPUT -p tcp --dport 21 -j ACCEPT <br /> iptables -A INPUT -p tcp --dport 20 -j ACCEPT</font> <br /> #如果有其他端口的话,规则也类似,稍微修改上述语句就行 <br /> #禁止其他未允许的规则访问 <br /><font color="#0080ff"> iptables -A INPUT -j REJECT <br /> iptables -A FORWARD -j REJECT</font> <br />4、屏蔽IP <br />#如果只是想屏蔽IP的话“3、开放指定的端口”可以直接跳过。 <br /> #屏蔽单个IP的命令是 <br /><font color="#0080ff"> iptables -I INPUT -s 123.45.6.7 -j DROP</font> <br /> #封整个段即从123.0.0.1到123.255.255.254的命令 <br /><font color="#0080ff"> iptables -I INPUT -s 123.0.0.0/8 -j DROP</font> <br /> #封IP段即从123.45.0.1到123.45.255.254的命令 <br /><font color="#0080ff"> iptables -I INPUT -s 124.45.0.0/16 -j DROP</font> <br /> #封IP段即从123.45.6.1到123.45.6.254的命令是 <br /><font color="#0080ff"> iptables -I INPUT -s 123.45.6.0/24 -j DROP</font> <br />4、查看已添加的iptables规则 <br /><font color="#0080ff">iptables -L -n</font> </p> <p>v:显示详细信息,包括每条规则的匹配包数量和匹配字节数 <br /> x:在 v 的基础上,禁止自动单位换算(K、M) vps侦探 <br /> n:只显示IP地址和端口号,不将ip解析为域名 <br />5、删除已添加的iptables规则 </p> <p>将所有iptables以序号标记显示,执行: <br /><font color="#0080ff">iptables -L -n --line-numbers</font> </p> <p>比如要删除INPUT里序号为8的规则,执行: <br /><font color="#0080ff">iptables -D INPUT 8</font> <br />6、iptables的开机启动及规则保存 </p> <p>CentOS上可能会存在安装好iptables后,iptables并不开机自启动,可以执行一下: <br /><font color="#0080ff">chkconfig --level 345 iptables on</font> </p> <p>将其加入开机启动。 </p> <p>CentOS上可以执行:<font color="#0080ff">service iptables save</font>保存规则。 </p> <p>另外更需要注意的是Debian/Ubuntu上iptables是不会保存规则的。 </p> <p>需要按如下步骤进行,让网卡关闭是保存iptables规则,启动时加载iptables规则: </p> <p>创建/etc/network/if-post-down.d/iptables 文件,添加如下内容: <br /><font color="#0080ff">#!/bin/bash <br /> iptables-save > /etc/iptables.rules</font> </p> <p>执行:chmod +x /etc/network/if-post-down.d/iptables 添加执行权限。 </p> <p>创建/etc/network/if-pre-up.d/iptables 文件,添加如下内容: <br /><font color="#0080ff">#!/bin/bash <br /> iptables-restore < /etc/iptables.rules</font> </p> <p>执行:chmod +x /etc/network/if-pre-up.d/iptables 添加执行权限。 </p> <p>关于更多的iptables的使用方法可以执行:iptables --help或网上搜索一下iptables参数的说明。</p> Last modification:October 6, 2023 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 如果觉得我的文章对你有用,请随意赞赏
9 comments
再难也要学http://www.jejy.cn/
我想知道windows和linux哪个比较好用???
这个吗!!
博主真精通这些,我都看的眼花缭乱
是 的,越是不懂,就要越要去学,
对啊!很多人都害怕失败!!
我靠,博主你是不是样样都精通啊,搞了这么多个模块,基本上涉及到网络的技术你都在搞啊
好难啊http://www.jejy.cn/
哪能样样精通啊!知识喜欢未知的东西!!