<!DOCTYPE html>
<html lang="zh-Hans">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=5">
    <title>Linux IPTables：如何添加防火墙规则 | 云图网</title>
    <meta name="keywords" content="AI,Huawei,华为">
<meta name="description" content="摘要：本文介绍了如何使用“iptables -A”命令添加 iptables 防火墙规则。 本文分享自华为云社区《Linux IPTables：如何添加防火墙规则（使用允许 SSH 示例）》，作者：Tiamo_T 。 本文介绍了如何使用“iptables -A”命令添加 iptables 防火墙规则。 “-A”用于追加…">
<meta property="og:type" content="article">
<meta property="og:url" content="https://blog.ytso.com/tech/pnotes/169608.html">
<meta property="og:site_name" content="云图网">
<meta property="og:title" content="Linux IPTables：如何添加防火墙规则">
<meta property="og:description" content="摘要：本文介绍了如何使用“iptables -A”命令添加 iptables 防火墙规则。 本文分享自华为云社区《Linux IPTables：如何添加防火墙规则（使用允许 SSH 示例）》，作者：Tiamo_T 。 本文介绍了如何使用“iptables -A”命令添加 iptables 防火墙规则。 “-A”用于追加…">
<link rel="canonical" href="https://blog.ytso.com/tech/pnotes/169608.html">
<meta name="applicable-device" content="pc,mobile">
<meta http-equiv="Cache-Control" content="no-transform">
<link rel="shortcut icon" href="https://imgcdn.ytso.com/wp-content/uploads/2024/10/20241015181503433.jpg">
<link rel='dns-prefetch' href='//cdnjs.cloudflare.com' />
<style id='wp-img-auto-sizes-contain-inline-css' type='text/css'>
img:is([sizes=auto i],[sizes^="auto," i]){contain-intrinsic-size:3000px 1500px}
/*# sourceURL=wp-img-auto-sizes-contain-inline-css */
</style>
<link rel='stylesheet' id='stylesheet-css' href='https://blog.ytso.com/wp-content/themes/justnews/css/style.css?ver=6.21.5' type='text/css' media='all' />
<link rel='stylesheet' id='material-icons-css' href='https://blog.ytso.com/wp-content/themes/justnews/themer/assets/css/material-icons.css?ver=6.21.5' type='text/css' media='all' />
<link rel='stylesheet' id='remixicon-css' href='https://cdnjs.cloudflare.com/ajax/libs/remixicon/4.2.0/remixicon.min.css?ver=6.9.4' type='text/css' media='all' />
<link rel='stylesheet' id='font-awesome-css' href='https://blog.ytso.com/wp-content/themes/justnews/themer/assets/css/font-awesome.css?ver=6.21.5' type='text/css' media='all' />
<style id='wp-block-library-inline-css' type='text/css'>
:root{--wp-block-synced-color:#7a00df;--wp-block-synced-color--rgb:122,0,223;--wp-bound-block-color:var(--wp-block-synced-color);--wp-editor-canvas-background:#ddd;--wp-admin-theme-color:#007cba;--wp-admin-theme-color--rgb:0,124,186;--wp-admin-theme-color-darker-10:#006ba1;--wp-admin-theme-color-darker-10--rgb:0,107,160.5;--wp-admin-theme-color-darker-20:#005a87;--wp-admin-theme-color-darker-20--rgb:0,90,135;--wp-admin-border-width-focus:2px}@media (min-resolution:192dpi){:root{--wp-admin-border-width-focus:1.5px}}.wp-element-button{cursor:pointer}:root .has-very-light-gray-background-color{background-color:#eee}:root .has-very-dark-gray-background-color{background-color:#313131}:root .has-very-light-gray-color{color:#eee}:root .has-very-dark-gray-color{color:#313131}:root .has-vivid-green-cyan-to-vivid-cyan-blue-gradient-background{background:linear-gradient(135deg,#00d084,#0693e3)}:root .has-purple-crush-gradient-background{background:linear-gradient(135deg,#34e2e4,#4721fb 50%,#ab1dfe)}:root .has-hazy-dawn-gradient-background{background:linear-gradient(135deg,#faaca8,#dad0ec)}:root .has-subdued-olive-gradient-background{background:linear-gradient(135deg,#fafae1,#67a671)}:root .has-atomic-cream-gradient-background{background:linear-gradient(135deg,#fdd79a,#004a59)}:root .has-nightshade-gradient-background{background:linear-gradient(135deg,#330968,#31cdcf)}:root .has-midnight-gradient-background{background:linear-gradient(135deg,#020381,#2874fc)}:root{--wp--preset--font-size--normal:16px;--wp--preset--font-size--huge:42px}.has-regular-font-size{font-size:1em}.has-larger-font-size{font-size:2.625em}.has-normal-font-size{font-size:var(--wp--preset--font-size--normal)}.has-huge-font-size{font-size:var(--wp--preset--font-size--huge)}.has-text-align-center{text-align:center}.has-text-align-left{text-align:left}.has-text-align-right{text-align:right}.has-fit-text{white-space:nowrap!important}#end-resizable-editor-section{display:none}.aligncenter{clear:both}.items-justified-left{justify-content:flex-start}.items-justified-center{justify-content:center}.items-justified-right{justify-content:flex-end}.items-justified-space-between{justify-content:space-between}.screen-reader-text{border:0;clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal!important}.screen-reader-text:focus{background-color:#ddd;clip-path:none;color:#444;display:block;font-size:1em;height:auto;left:5px;line-height:normal;padding:15px 23px 14px;text-decoration:none;top:5px;width:auto;z-index:100000}html :where(.has-border-color){border-style:solid}html :where([style*=border-top-color]){border-top-style:solid}html :where([style*=border-right-color]){border-right-style:solid}html :where([style*=border-bottom-color]){border-bottom-style:solid}html :where([style*=border-left-color]){border-left-style:solid}html :where([style*=border-width]){border-style:solid}html :where([style*=border-top-width]){border-top-style:solid}html :where([style*=border-right-width]){border-right-style:solid}html :where([style*=border-bottom-width]){border-bottom-style:solid}html :where([style*=border-left-width]){border-left-style:solid}html :where(img[class*=wp-image-]){height:auto;max-width:100%}:where(figure){margin:0 0 1em}html :where(.is-position-sticky){--wp-admin--admin-bar--position-offset:var(--wp-admin--admin-bar--height,0px)}@media screen and (max-width:600px){html :where(.is-position-sticky){--wp-admin--admin-bar--position-offset:0px}}
/*wp_block_styles_on_demand_placeholder:69c84bb066552*/
/*# sourceURL=wp-block-library-inline-css */
</style>
<style id='classic-theme-styles-inline-css' type='text/css'>
/*! This file is auto-generated */
.wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none}
/*# sourceURL=/wp-includes/css/classic-themes.min.css */
</style>
<link rel='stylesheet' id='wpcom-member-css' href='https://blog.ytso.com/wp-content/plugins/wpcom-member/css/style.css?ver=1.7.19' type='text/css' media='all' />
<script type="text/javascript" src="https://blog.ytso.com/wp-includes/js/jquery/jquery.min.js?ver=3.7.1" id="jquery-core-js"></script>
<script type="text/javascript" src="https://blog.ytso.com/wp-includes/js/jquery/jquery-migrate.min.js?ver=3.4.1" id="jquery-migrate-js"></script>
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="https://blog.ytso.com/xmlrpc.php?rsd" />
<meta name='description' content='摘要：本文介绍了如何使用“iptables -A”命令添加 iptables 防火墙规则。 本文分享自华为云社区《Linux IPTables：如何添加防火墙规则（使用允许 SSH 示例）》，作者：Tiamo_T 。 本文介绍了如何使用“iptables -A”命…' />
<meta name='keywords' content='AI,Huawei,华为' />
<style>:root{--theme-color: #08c; --theme-hover: #07c; --logo-height: 32px; --logo-height-mobile: 26px; --menu-item-gap: 28px; --member-login-bg: url('https://blog.ytso.com/loginwall.jpg'); --header-bg-color: #fff; --header-bg-image: none; --theme-border-radius-s: 3px; --theme-border-radius-m: 5px; --theme-border-radius-l: 8px; --theme-border-radius-xl: 12px; --thumb-ratio-default: 480 / 300; --thumb-ratio-post: 480 / 300; --post-video-ratio: 860 / 482;}</style>
<link rel="icon" href="https://imgcdn.ytso.com/wp-content/uploads/2024/10/20241015181503433.jpg" sizes="32x32" />
<link rel="icon" href="https://imgcdn.ytso.com/wp-content/uploads/2024/10/20241015181503433.jpg" sizes="192x192" />
<link rel="apple-touch-icon" href="https://imgcdn.ytso.com/wp-content/uploads/2024/10/20241015181503433.jpg" />
<meta name="msapplication-TileImage" content="https://imgcdn.ytso.com/wp-content/uploads/2024/10/20241015181503433.jpg" />
    <!--[if lte IE 11]><script src="https://blog.ytso.com/wp-content/themes/justnews/js/update.js"></script><![endif]-->
</head>
<body class="wp-singular post-template-default single single-post postid-169608 single-format-standard wp-theme-justnews lang-cn el-boxed header-fixed">
<header class="header header-fluid">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse" aria-label="menu">
                <span class="icon-bar icon-bar-1"></span>
                <span class="icon-bar icon-bar-2"></span>
                <span class="icon-bar icon-bar-3"></span>
            </button>
                        <div class="logo">
                <a href="https://blog.ytso.com/" rel="home">
                    <img src="https://imgcdn.ytso.com/wp-content/uploads/2021/12/20211207181156143.png" alt="云图网">
                </a>
            </div>
        </div>
        <div class="collapse navbar-collapse mobile-style-0">
            <nav class="primary-menu"><ul id="menu-justnews-footer-menu" class="nav navbar-nav wpcom-adv-menu"><li class="menu-item"><a href="https://blog.ytso.com/category/industrynews">业界资讯</a></li>
<li class="menu-item current-post-ancestor active menu-item-style menu-item-style-1 dropdown"><a target="_blank" href="https://blog.ytso.com/category/tech" class="dropdown-toggle">技术专区</a>
<ul class="dropdown-menu menu-item-wrap menu-item-col-5">
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/cloud">云计算</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/ai">人工智能</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/safety">信息安全</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/bigdata">大数据</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/dev">研发管理</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/webdev">大前端</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/opensource">开源</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/aiops">智能运维</a></li>
	<li class="menu-item current-post-ancestor current-post-parent active"><a href="https://blog.ytso.com/category/tech/pnotes">编程笔记</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/wp">WordPress</a></li>
</ul>
</li>
<li class="menu-item"><a href="https://blog.ytso.com/category/enterprise-strategic-planning">企业战略规划</a></li>
<li class="menu-item"><a href="https://blog.ytso.com/category/download">下载专区</a></li>
<li class="menu-item"><a href="https://blog.ytso.com/category/it%e6%b1%9f%e6%b9%96%e5%8f%b2">江湖史</a></li>
<li class="menu-item"><a href="https://blog.ytso.com/category/notes">随笔记录</a></li>
</ul></nav>            <div class="navbar-action">
                <div class="navbar-search-icon j-navbar-search"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-search"></use></svg></i></div><form class="navbar-search" action="https://blog.ytso.com/" method="get" role="search"><div class="navbar-search-inner"><i class="wpcom-icon wi navbar-search-close"><svg aria-hidden="true"><use xlink:href="#wi-close"></use></svg></i><input type="text" name="s" class="navbar-search-input" autocomplete="off" maxlength="100" placeholder="输入关键词搜索..." value=""><button class="navbar-search-btn" type="submit" aria-label="搜索"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-search"></use></svg></i></button></div></form>                    <div id="j-user-wrap">
                        <a class="login" href="https://blog.ytso.com/login">登录</a>
                        <a class="login register" href="https://blog.ytso.com/register">注册</a>
                    </div>
                                            <a class="wpcom-btn btn-primary btn-xs publish" href="https://blog.ytso.com/contribute">
                            <i class="fa fa-edit"></i> 投稿                        </a>
                                </div>
        </div>
    </div><!-- /.container -->
</header>

<div id="wrap">    <div class="wrap container">
        <ol class="breadcrumb" vocab="https://schema.org/" typeof="BreadcrumbList"><li class="home" property="itemListElement" typeof="ListItem"><a href="https://blog.ytso.com" property="item" typeof="WebPage"><span property="name" class="hide">云图网</span>首页</a><meta property="position" content="1"></li><li property="itemListElement" typeof="ListItem"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-arrow-right-3"></use></svg></i><a href="https://blog.ytso.com/category/tech" property="item" typeof="WebPage"><span property="name">技术专区</span></a><meta property="position" content="2"></li><li property="itemListElement" typeof="ListItem"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-arrow-right-3"></use></svg></i><a href="https://blog.ytso.com/category/tech/pnotes" property="item" typeof="WebPage"><span property="name">编程笔记</span></a><meta property="position" content="3"></li></ol>        <main class="main">
                            <article id="post-169608" class="post-169608 post type-post status-publish format-standard hentry category-pnotes tag-ai tag-huawei tag-183 entry">
                    <div class="entry-main">
                                                                        <div class="entry-head">
                            <h1 class="entry-title">Linux IPTables：如何添加防火墙规则</h1>
                            <div class="entry-info">
                                                                <time class="entry-date published" datetime="2021-09-24T00:55:21+08:00" pubdate>
                                    2021年9月24日 00:55                                </time>
                                <span class="dot">•</span>
                                <a href="https://blog.ytso.com/category/tech/pnotes" rel="category tag">编程笔记</a>                                                            </div>
                        </div>
                        
                                                <div class="entry-content text-indent text-justify">
                            <div id="toc">
<p class="toc-title"><strong>文章目录</strong><span class="toc-controller toc-controller-show">[隐藏]</span></p>
<ul>
<li class="toc-level2"><a href="#toc_1">防火墙规则参数</a>
<ul>
<li class="toc-level3"><a href="#toc_2">-p 用于协议</a></li>
<li class="toc-level3"><a href="#toc_3">-s 是源</a></li>
<li class="toc-level3"><a href="#toc_4">-d 是目的地</a></li>
<li class="toc-level3"><a href="#toc_5">-j 是目标</a></li>
<li class="toc-level3"><a href="#toc_6">-i 用于接口</a></li>
<li class="toc-level3"><a href="#toc_7">-o 用于输出接口</a></li>
</ul>
</li>
<li class="toc-level2"><a href="#toc_8">防火墙参数的附加选项</a>
<ul>
<li class="toc-level3"><a href="#toc_9">–sport 用于源端口（用于 -p tcp 或 -p udp）</a></li>
<li class="toc-level3"><a href="#toc_10">–dport 用于目标端口（用于 -p tcp 或 -p udp）</a></li>
<li class="toc-level3"><a href="#toc_11">–tcp-flags 用于 TCP 标志（用于 -p tcp）</a></li>
<li class="toc-level3"><a href="#toc_12">–icmp-type 用于 ICMP 类型（用于 -p icmp）</a></li>
</ul>
</li>
<li class="toc-level2"><a href="#toc_13">允许传入 SSH 连接的示例防火墙规则</a>
<ul>
<li class="toc-level3"><a href="#toc_14">1. 删除现有规则</a></li>
<li class="toc-level3"><a href="#toc_15">2. 只允许 SSH</a></li>
<li class="toc-level3"><a href="#toc_16">3.丢弃所有其他数据包</a></li>
<li class="toc-level3"><a href="#toc_17">4.查看SSH规则和测试</a></li>
</ul>
</li>
</ul>
</div>
<blockquote><p> <strong>摘要：</strong>本文介绍了如何使用“iptables -A”命令添加 iptables 防火墙规则。</p></blockquote>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">本文分享自华为云社区《<a href="https://www.oschina.net/action/GoToLink?url=https%3A%2F%2Fbbs.huaweicloud.com%2Fblogs%2F300668%3Futm_source%3Doschina%26utm_medium%3Dbbs-ex%26utm_campaign%3Dother%26utm_content%3Dcontent" target="_blank" rel="nofollow noopener">Linux IPTables：如何添加防火墙规则（使用允许 SSH 示例）</a>》，作者：Tiamo_T 。</p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">本文介绍了如何使用“iptables -A”命令添加 iptables 防火墙规则。</p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">“-A”用于追加。如果它让你更容易记住“-A”作为添加规则（而不是附加规则），那就没问题了。但是，请记住，“-A”在链的末尾添加了规则。</p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">同样，记住 -A 在末尾添加规则非常重要。</p>
<p> 通常，最后一条规则是丢弃所有数据包。如果您已经有一个丢弃所有数据包的规则，并且如果您尝试在命令行中使用“-A”来创建新规则，您最终会在当前的“丢弃所有数据包”规则之后添加新规则，这将使您的新规则几乎毫无用处。</p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">一旦您掌握了 iptables，并且当您在生产中实现它时，您应该使用 shell 脚本，您可以在其中使用 -A 命令添加所有规则。在那个 shell 脚本中，你的最后一行应该总是“丢弃所有数据包”规则。当您想要添加任何新规则时，请修改该 shell 脚本并将您的新规则添加到“丢弃所有数据包”规则之上。</p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">句法：</p>
<div>
<pre><code class="language-text">iptables -A chain firewall-rule</code></pre>
</div>
<ul>
<li>-A 链 - 指定应附加规则的链。例如，对传入数据包使用 INPUT 链，对传出数据包使用 OUTPUT。</li>
<li>firewall-rule – 各种参数构成防火墙规则。</li>
</ul>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">如果您不知道链是什么意思，<span style="color:#111111">最好先阅读</span><a href="https://www.oschina.net/action/GoToLink?url=https%3A%2F%2Fbbs.huaweicloud.com%2Fblogs%2F300564" rel="nofollow noopener" target="_blank"><span>iptables 基础知识</span></a><span style="color:#111111">。</span></p>
<p><span id="OSC_h2_1"></span> </p>
<h2 id="toc_1">防火墙规则参数</h2>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">以下参数可用于所有类型的防火墙规则。</p>
<p><span id="OSC_h3_2"></span> </p>
<h3 id="toc_2">-p 用于协议</h3>
<ul>
<li>指示规则的协议。</li>
<li>可能的值为 tcp、udp、icmp</li>
<li>使用“all”允许所有协议。当您不指定 -p 时，默认情况下将使用“所有”协议。使用“all”并始终指定协议并不是一个好习惯。</li>
<li>使用名称（例如：tcp）或编号（例如：6 表示 tcp）作为协议。</li>
<li>/etc/protocols 文件包含所有允许的协议名称和编号。</li>
<li>你也可以使用--protocol</li>
</ul>
<p><span id="OSC_h3_3"></span> </p>
<h3 id="toc_3">-s 是源</h3>
<ul>
<li>指示数据包的来源。</li>
<li>这可以是 IP 地址、网络地址或主机名</li>
<li>例如：-s 192.168.1.101 表示具体的ip地址</li>
<li>对于网络掩码，使用 /mask。例如：“-s 192.168.1.0/24”表示该网络的网络掩码为 255.255.255.0。这匹配 192.168.1.x 网络。</li>
<li>当您不指定来源时，它将匹配所有来源。</li>
<li>您还可以使用 –src 或 –source</li>
</ul>
<p><span id="OSC_h3_4"></span> </p>
<h3 id="toc_4">-d 是目的地</h3>
<ul>
<li>指示数据包的目的地。</li>
<li>这与“-s”相同（除了它代表目标主机、IP 地址或网络）</li>
<li>您还可以使用 –dst 或 –destination</li>
</ul>
<p><span id="OSC_h3_5"></span> </p>
<h3 id="toc_5">-j 是目标</h3>
<ul>
<li>j 代表“跳转到目标”</li>
<li>这指定了匹配此防火墙规则的数据包需要发生什么。</li>
<li>可能的值为 ACCEPT、DROP、QUEUE、RETURN</li>
<li>您还可以指定其他用户定义的链作为目标值。</li>
</ul>
<p><span id="OSC_h3_6"></span> </p>
<h3 id="toc_6">-i 用于接口</h3>
<ul>
<li>i 代表“输入接口”</li>
<li>您可能会忽略这一点并假设“-i”用于接口。请注意 -i 和 -o 都用于接口。但是，-i 用于输入接口，-o 用于输出接口。</li>
<li>指示传入数据包通过 INPUT、FORWARD 和 PREROUTING 链进入的接口。</li>
<li>例如：-i eth0 表示此规则应考虑通过接口 eth0 传入的数据包。</li>
<li>如果您不指定 -i 选项，则系统上所有可用的接口都将被考虑用于输入数据包。</li>
<li>您也可以使用 –in-interface</li>
</ul>
<p><span id="OSC_h3_7"></span> </p>
<h3 id="toc_7">-o 用于输出接口</h3>
<ul>
<li>o 代表“输出接口”</li>
<li>指示通过 INPUT、FORWARD 和 PREROUTING 链发送传出数据包的接口。</li>
<li>如果您不指定 -o 选项，则系统上所有可用的接口都将被视为输出数据包。</li>
<li>你也可以使用--out-interface</li>
</ul>
<p><span id="OSC_h2_8"></span> </p>
<h2 id="toc_8">防火墙参数的附加选项</h2>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">上面的一些防火墙参数依次具有可以与它们一起传递的自己的选项。以下是一些最常见的选项。</p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">要使用这些参数选项，您应该在防火墙规则中指定相应的参数。例如，要使用“–sport”选项，您应该在防火墙规则中指定“-p tcp”（或“-p udp”）参数。</p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start"><strong>注意：</strong>所有这些选项前面都有两个破折号。例如，sport 前面有两个连字符。</p>
<p><span id="OSC_h3_9"></span> </p>
<h3 id="toc_9">–sport 用于源端口（用于 -p tcp 或 -p udp）</h3>
<ul>
<li>默认情况下，所有源端口都匹配。</li>
<li>您可以指定端口号或名称。例如，要在防火墙规则中使用 SSH 端口，请使用“–sport 22”或“–sport ssh”。</li>
<li>/etc/services 文件包含所有允许的端口名称和编号。</li>
<li>在规则中使用端口号比使用端口名更好（为了性能）。</li>
<li>要匹配端口范围，请使用冒号。例如，22:100 匹配从 22 到 100 的端口号。</li>
<li>您也可以使用 –source-port</li>
</ul>
<p><span id="OSC_h3_10"></span> </p>
<h3 id="toc_10">–dport 用于目标端口（用于 -p tcp 或 -p udp）</h3>
<ul>
<li>一切都与 –sport 相同，除了这是针对目标端口。</li>
<li>您也可以使用 –destination-port</li>
</ul>
<p><span id="OSC_h3_11"></span> </p>
<h3 id="toc_11">–tcp-flags 用于 TCP 标志（用于 -p tcp）</h3>
<ul>
<li>这可以包含以逗号分隔的多个值。</li>
<li>可能的值有：SYN、ACK、FIN、RST、URG、PSH。您也可以使用 ALL 或 NONE</li>
</ul>
<p><span id="OSC_h3_12"></span> </p>
<h3 id="toc_12">–icmp-type 用于 ICMP 类型（用于 -p icmp）</h3>
<ul>
<li>当您使用icmp协议“-p icmp”时，您还可以使用“-icmp-type”参数指定ICMP类型。</li>
<li>例如：“-icmp-type 0”用于“Echo Reply”，“-icmp-type 8”用于“Echo”。</li>
</ul>
<p><span id="OSC_h2_13"></span> </p>
<h2 id="toc_13">允许传入 SSH 连接的示例防火墙规则</h2>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">现在您了解了防火墙规则的各种参数（及其选项），让我们构建一个示例防火墙规则。</p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">在这个例子中，让我们只允许传入的 SSH 连接到服务器。所有其他连接都将被阻止（包括 ping）。</p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">警告：使用防火墙规则可能会导致您的系统无法访问。如果您不知道自己在做什么，您可能会将自己（和其他所有人）锁定在系统之外。因此，仅在没有人使用的测试系统上进行所有学习，并且如果您被锁定，您可以访问控制台以重新启动 iptables。</p>
<p><span id="OSC_h3_14"></span> </p>
<h3 id="toc_14">1. 删除现有规则</h3>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">如果您已经有一些 iptables 规则，请在删除现有规则之前进行备份。</p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">删除所有现有规则并允许防火墙接受所有内容。<span style="color:#111111">使用我们之前讨论过的</span><a href="https://www.oschina.net/action/GoToLink?url=https%3A%2F%2Fbbs.huaweicloud.com%2Fblogs%2F300584" rel="nofollow noopener" target="_blank"><span>iptables flush</span></a><span style="color:#111111">清理所有现有规则并从头开始。</span></p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">测试以确保您能够从外部 ssh 和 ping 此服务器。</p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">完成此示例后，您将只能通过 SSH 连接到此服务器。您将无法从外部 ping 此服务器。</p>
<p><span id="OSC_h3_15"></span> </p>
<h3 id="toc_15">2. 只允许 SSH</h3>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">仅允许与此服务器的传入 SSH 连接。您可以从任何地方通过 ssh 连接到此服务器。</p>
<div>
<pre><code class="language-text">iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT</code></pre>
</div>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">上面的 iptables 命令有以下 4 个组件。</p>
<ul>
<li>“-A INPUT”——这表明我们正在向 INPUT 链追加（或添加）一条新规则。因此，此规则适用于传入流量。</li>
<li>“-i eth0” – 将根据此规则检查通过接口 eth0 的传入数据包。</li>
<li>“-p tcp –dport 22” – 此规则适用于 TCP 数据包。这有一个名为“-dport 22”的 tcp 选项，它表示服务器上此规则的目标端口是 22（即 ssh）。</li>
<li>“-j ACCEPT”——跳转到接受，它只是接受数据包。</li>
</ul>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">简单来说，上述规则可以表述为：所有通过 eth0 for ssh 传入的数据包都将被接受。</p>
<p><span id="OSC_h3_16"></span> </p>
<h3 id="toc_16">3.丢弃所有其他数据包</h3>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">一旦您指定了接受数据包的自定义规则，您还应该有一个默认规则来丢弃任何其他数据包。</p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">这应该是您在 INPUT 链中的最后一条规则。</p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">要丢弃所有传入的数据包，请执行以下操作。</p>
<div>
<pre><code class="language-text">iptables -A INPUT -j DROP</code></pre>
</div>
<p><span id="OSC_h3_17"></span> </p>
<h3 id="toc_17">4.查看SSH规则和测试</h3>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">要查看当前的 iptables 防火墙规则，请使用“iptables -L”命令。</p>
<div>
<pre><code class="language-text"># iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh
DROP       all  --  anywhere             anywhere</code></pre>
</div>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">从上面的输出中可以看出，它依次具有以下两条规则。</p>
<ul>
<li>接受所有传入的 ssh 连接</li>
<li>丢弃所有其他数据包。</li>
</ul>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">与其从命令行添加防火墙规则，不如创建一个包含规则的 shell 脚本，如下所示。</p>
<div>
<pre><code class="language-text"># vi iptables.sh
iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -j DROP

# sh -x iptables.sh
+ iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT
+ iptables -A INPUT -j DROP

# iptables -L INPUT
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh
DROP       all  --  anywhere             anywhere</code></pre>
</div>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">与 iptables append/add 命令类似，iptables 可用的其他命令很少。</p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start">&nbsp;</p>
<p style="color:#121212; margin-left:0; margin-right:0; text-align:start"><strong><a href="https://www.oschina.net/action/GoToLink?url=https%3A%2F%2Fbbs.huaweicloud.com%2Fblogs%3Futm_source%3Doschina%26utm_medium%3Dbbs-ex%26utm_campaign%3Dother%26utm_content%3Dcontent" target="_blank" rel="nofollow noopener">点击关注，第一时间了解华为云新鲜技术~</a></strong></p>
<div v-for="(o, i) in groupList" :key="i" class="group-item">
<div class="group-item-title" :class="o.type?'group-item-title-select':'group-item-title-not-select'" :style="{backgroundColor: groupColor[i]}" @click="chengeGroup(i)">
<div class="group-item-name">
                {{o.name}}
                </div>
</p></div>
<div v-if="o.type" class="group-circle">
<div v-for="(m, j) in circleList" v-if="m.circle === o.id"  :key="j" class="circle-item" :class="[m.type?'name-select':'name-not-select']"
                      :style="{backgroundColor: circleColor[j]==='red'?'#fff':circleColor[j], color:circleColor[j]==='red'?'red':'black'}" @click="chengeCirle(j)"><br />
                        {{m.name}}
                </div>
</p></div>
</p></div>
<div class="entry-readmore"><div class="entry-readmore-btn"></div></div>                                                        <div class="entry-copyright"><p>原创文章，作者：Maggie-Hunter，如若转载，请注明出处：https://blog.ytso.com/tech/pnotes/169608.html</p></div>                        </div>

                        <div class="entry-tag"><a href="https://blog.ytso.com/tag/ai" rel="tag">AI</a><a href="https://blog.ytso.com/tag/huawei" rel="tag">Huawei</a><a href="https://blog.ytso.com/tag/%e5%8d%8e%e4%b8%ba" rel="tag">华为</a></div>
                        <div class="entry-action">
                            <div class="btn-zan" data-id="169608"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-thumb-up-fill"></use></svg></i> 赞 <span class="entry-action-num">(0)</span></div>
                                                    </div>

                        <div class="entry-bar">
                            <div class="entry-bar-inner">
                                                                <div class="entry-bar-info entry-bar-info2">
                                    <div class="info-item meta">
                                                                                    <a class="meta-item j-heart" href="javascript:;" data-id="169608"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-star"></use></svg></i> <span class="data">0</span></a>                                        <a class="meta-item" href="#comments"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-comment"></use></svg></i> <span class="data">0</span></a>                                                                            </div>
                                    <div class="info-item share">
                                                                                    <a class="meta-item mobile j-mobile-share" href="javascript:;" data-id="169608" data-qrcode="https://blog.ytso.com/tech/pnotes/169608.html">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-share"></use></svg></i> 生成海报                                            </a>
                                                                                    <a class="meta-item wechat" data-share="wechat" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-wechat"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item weibo" data-share="weibo" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-weibo"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item qq" data-share="qq" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-qq"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item qzone" data-share="qzone" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-qzone"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item douban" data-share="douban" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-douban"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item linkedin" data-share="linkedin" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-linkedin"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item facebook" data-share="facebook" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-facebook"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item twitter" data-share="twitter" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-twitter"></use></svg></i>                                            </a>
                                                                            </div>
                                    <div class="info-item act">
                                        <a href="javascript:;" id="j-reading"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-article"></use></svg></i></a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                                            <div class="entry-page">
                    <div class="entry-page-prev j-lazy" style="background-image: url('https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png');" data-original="http://ytso-blog-oss-img.oss-cn-beijing.aliyuncs.com/wp-content/uploads/2021/09/23/20210923_614cb16940504.png">
                <a href="https://blog.ytso.com/tech/pnotes/169607.html" title="采集 Nginx 日志的几种方式，你知道几种？" rel="prev">
                    <span>采集 Nginx 日志的几种方式，你知道几种？</span>
                </a>
                <div class="entry-page-info">
                    <span class="pull-left"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-arrow-left-double"></use></svg></i> 上一篇</span>
                    <span class="pull-right">2021年9月24日 00:55</span>
                </div>
            </div>
                <error>
    <code>wp_die</code>
    <title><![CDATA[WordPress &amp;rsaquo; Error]]></title>
    <message><![CDATA[&lt;h1&gt;Error establishing a Redis connection&lt;/h1&gt;
&lt;p&gt;To disable Redis, delete the &lt;code&gt;object-cache.php&lt;/code&gt; file in the &lt;code&gt;/wp-content/&lt;/code&gt; directory.&lt;/p&gt;
]]></message>
    <data>
        <status>500</status>
    </data>
</error>
<!--
Performance optimized by Redis Object Cache. Learn more: https://wprediscache.com

Retrieved 1728 objects (300 KB) from Redis using Predis (v2.4.0).
-->
