Loading... <p>iptables是Linux上常用的防火墙软件,下面vps侦探给大家说一下iptables的安装、清除iptables规则、iptables只开放指定端口、iptables屏蔽指定ip、ip段及解封、删除已添加的iptables规则等iptables的基本应用。</p><h2>1、安装iptables防火墙</h2><p>如果没有安装iptables需要先安装,<strong>CentOS执行:</strong></p><div style="border-right: #00a0c6 1px dashed; border-top: #00a0c6 1px dashed; border-left: #00a0c6 1px dashed; width: auto; border-bottom: #00a0c6 1px dashed; background-color: #ffffff"><span style="color: #ff0000">yum install iptables</span></div><p><strong>Debian/Ubuntu执行:</strong></p><div style="border-right: #00a0c6 1px dashed; border-top: #00a0c6 1px dashed; border-left: #00a0c6 1px dashed; width: auto; border-bottom: #00a0c6 1px dashed; background-color: #ffffff"><span style="color: #ff0000">apt-get install iptables</span></div><h2>2、清除已有iptables规则</h2><div style="border-right: #00a0c6 1px dashed; border-top: #00a0c6 1px dashed; border-left: #00a0c6 1px dashed; width: auto; border-bottom: #00a0c6 1px dashed; background-color: #ffffff"><span style="color: #ff0000">iptables -F<br />iptables -X<br />iptables -Z</span></div><h2>3、开放指定的端口</h2><div style="border-right: #00a0c6 1px dashed; border-top: #00a0c6 1px dashed; border-left: #00a0c6 1px dashed; width: auto; border-bottom: #00a0c6 1px dashed; background-color: #ffffff"><span style="color: #000000"><span style="">#允许本地回环接口(即运行本机访问本机)<br /></span></span><span style="color: #ff0000">iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT<br /><span style="color: #000000"># 允许已建立的或相关连的通行<br /></span>iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT<br /><span style="color: #000000">#允许所有本机向外的访问<br /></span>iptables -A OUTPUT -j ACCEPT<br /><span style="color: #000000"># 允许访问22端口<br /></span>iptables -A INPUT -p tcp --dport 22 -j ACCEPT<br /><span style="color: #000000">#允许访问80端口<br /></span>iptables -A INPUT -p tcp --dport 80 -j ACCEPT<br /><span style="color: #000000">#允许FTP服务的21和20端口<br /></span>iptables -A INPUT -p tcp --dport 21 -j ACCEPT<br />iptables -A INPUT -p tcp --dport 20 -j ACCEPT<br /><span style="color: #000000">#如果有其他端口的话,规则也类似,稍微修改上述语句就行<br />#禁止其他未允许的规则访问<br /></span>iptables -A INPUT -j REJECT<br />iptables -A FORWARD -j REJECT</span></div><h2>4、屏蔽IP</h2><div style="border-right: #00a0c6 1px dashed; border-top: #00a0c6 1px dashed; border-left: #00a0c6 1px dashed; width: auto; border-bottom: #00a0c6 1px dashed; background-color: #ffffff"><span style="color: #000000"><span style="">#如果只是想屏蔽IP的话“3、开放指定的端口”可以直接跳过。<br />#屏蔽单个IP的命令是<br /></span></span><span style="color: #ff0000">iptables -I INPUT -s 123.45.6.7 -j DROP<br /><span style="color: #000000">#封整个段即从123.0.0.1到123.255.255.254的命令<br /></span>iptables -I INPUT -s 123.0.0.0/8 -j DROP<br /><span style="color: #000000">#封IP段即从123.45.0.1到123.45.255.254的命令<br /></span>iptables -I INPUT -s 124.45.0.0/16 -j DROP<br /><span style="color: #000000">#封IP段即从123.45.6.1到123.45.6.254的命令是<br /></span>iptables -I INPUT -s 123.45.6.0/24 -j DROP</span></div><h2>4、查看已添加的iptables规则</h2><div style="border-right: #00a0c6 1px dashed; border-top: #00a0c6 1px dashed; border-left: #00a0c6 1px dashed; width: auto; border-bottom: #00a0c6 1px dashed; background-color: #ffffff"><span style="color: #ff0000">iptables -L -n</span></div><blockquote><p>v:显示详细信息,包括每条规则的匹配包数量和匹配字节数<br />x:在 v 的基础上,禁止自动单位换算(K、M) <span class="external-link"><a class="no-external-link" href="http://www.vpser.net/" target="_blank"><span style="color: #ffffff">vps侦探</span><i data-feather='external-link'></i></a></span><br />n:只显示IP地址和端口号,不将ip解析为域名</p></blockquote><h2>5、删除已添加的iptables规则</h2><p>将所有iptables以序号标记显示,执行:</p><div style="border-right: #00a0c6 1px dashed; border-top: #00a0c6 1px dashed; border-left: #00a0c6 1px dashed; width: auto; border-bottom: #00a0c6 1px dashed; background-color: #ffffff"><span style="color: #ff0000">iptables -L -n --line-numbers</span></div><p>比如要删除INPUT里序号为8的规则,执行:</p><div style="border-right: #00a0c6 1px dashed; border-top: #00a0c6 1px dashed; border-left: #00a0c6 1px dashed; width: auto; border-bottom: #00a0c6 1px dashed; background-color: #ffffff"><span style="color: #ff0000">iptables -D INPUT 8</span></div><h2>6、iptables的开机启动及规则保存</h2><p>CentOS上可能会存在安装好iptables后,iptables并不开机自启动,可以执行一下:</p><div style="border-right: #00a0c6 1px dashed; border-top: #00a0c6 1px dashed; border-left: #00a0c6 1px dashed; width: auto; border-bottom: #00a0c6 1px dashed; background-color: #ffffff"><span style="color: #ff0000">chkconfig --level 345 iptables on</span></div><p>将其加入开机启动。</p><p>CentOS上可以执行:<span style="color: #0000ff">service iptables save</span>保存规则。</p><p>另外更需要注意的是<span style="color: #0000ff">Debian/Ubuntu上iptables</span>是不会保存规则的。</p><p>需要按如下步骤进行,让网卡关闭是保存iptables规则,启动时加载iptables规则:</p><p>创建<span style="color: #ff0000">/etc/network/if-post-down.d/iptables </span>文件,添加如下内容:</p><div style="border-right: #00a0c6 1px dashed; border-top: #00a0c6 1px dashed; border-left: #00a0c6 1px dashed; width: auto; border-bottom: #00a0c6 1px dashed; background-color: #ffffff"><span style="color: #ff0000">#!/bin/bash<br />iptables-save > /etc/iptables.rules</span></div><p>执行:<span style="color: #0000ff">chmod +x /etc/network/if-post-down.d/iptables </span>添加执行权限。</p><p>创建<span style="color: #0000ff">/etc/network/if-pre-up.d/iptables </span>文件,添加如下内容:</p><div style="border-right: #00a0c6 1px dashed; border-top: #00a0c6 1px dashed; border-left: #00a0c6 1px dashed; width: auto; border-bottom: #00a0c6 1px dashed; background-color: #ffffff"><span style="color: #ff0000">#!/bin/bash<br />iptables-restore < /etc/iptables.rules</span></div><p>执行:<span style="color: #0000ff">chmod +x /etc/network/if-pre-up.d/iptables </span>添加执行权限。</p><p>关于更多的iptables的使用方法可以执行:iptables --help或网上搜索一下iptables参数的说明。</p> <hr class="content-copyright" style="margin-top:50px" /><blockquote class="content-copyright" style="font-style:normal"><p class="content-copyright">版权属于:大漠孤狼</p><p class="content-copyright">本文链接:<a class="content-copyright" href="https://www.dmgls.com/389.html">https://www.dmgls.com/389.html</a></p><p class="content-copyright">转载时须注明出处及本声明</p></blockquote> Last modification:July 14th, 2020 at 11:53 am © 允许规范转载 Support 如果觉得我的文章对你有用,请随意赞赏 Appreciate the author