AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 626521
Accepted
user1463822
user1463822
Asked: 2014-09-06 06:04:53 +0800 CST2014-09-06 06:04:53 +0800 CST 2014-09-06 06:04:53 +0800 CST

Centos 7 保存 iptables 设置

  • 772

问题:服务器重启后 iptables 重置为默认设置。

我正在尝试设置这样的规则:

iptables -I INPUT -p tcp --dport 3000 -j ACCEPT

之后我做:

service iptables save

它写回这样的东西

iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]

之后我就跑了(这做了一次):

chkconfig iptables on(我已经读到必须这样做才能在重新启动后恢复设置)

之后我重新启动并运行以下命令:

systemctl list-unit-files | grep iptables

我看到 iptables.service 已启用,但是,规则(打开端口 3000)不再起作用。

如何保留这些设置?

centos
  • 6 6 个回答
  • 268592 Views

6 个回答

  • Voted
  1. HosseinGBI
    2015-11-29T04:58:39+08:002015-11-29T04:58:39+08:00

    通过以下命令禁用防火墙:

    systemctl disable firewalld
    

    然后通过以下命令安装 iptables-service:

    yum install iptables-services
    

    然后启用 iptables 作为服务:

    systemctl enable iptables
    

    现在您可以通过以下命令保存您的 iptable 规则:

    service iptables save
    
    • 101
  2. Best Answer
    sgohl
    2014-09-06T06:13:17+08:002014-09-06T06:13:17+08:00

    CentOS 7 现在正在使用FirewallD!使用--permanent标志来保存设置。

    例子:

    firewall-cmd --zone=public --add-port=3000/tcp --permanent
    

    然后重新加载规则:

    firewall-cmd --reload
    
    • 74
  3. qris
    2015-01-05T09:19:06+08:002015-01-05T09:19:06+08:00

    在 CentOS 7 Minimal 上,您可能需要安装该iptables-services软件包(感谢@RichieACC的建议):

    sudo yum install -y iptables-services
    

    然后使用以下命令启用服务systemd:

    sudo systemctl enable iptables.service
    

    并运行 initscript 以保存您的防火墙规则:

    sudo /usr/libexec/iptables/iptables.init save
    
    • 31
  4. stormdrain
    2019-11-07T09:24:14+08:002019-11-07T09:24:14+08:00
    iptables-save > /etc/sysconfig/iptables
    

    将保存当前配置而无需安装任何其他库或服务。

    • 6
  5. stoned
    2014-09-06T07:29:01+08:002014-09-06T07:29:01+08:00

    可以直接修改/etc/sysconfig/iptables文件。重新加载iptables服务以从该文件重新加载规则。

    然而,正如你已经被告知的,firewalldCentos 是新的默认防火墙系统,这是学习如何使用它的好机会,你不觉得吗?

    • 2
  6. JohannesB
    2019-01-12T02:02:17+08:002019-01-12T02:02:17+08:00

    也许这样的脚本对任何人都有帮助?

    请注意,您将丢失当前配置的任何内容,因为它会删除 firewalld 并刷新 INPUT 表中的所有当前规则:

    yum remove firewalld && yum install iptables-services
    
    iptables --flush INPUT
    iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT            # Any packages related to an existing connection are OK
    iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT   # ssh is OK
    iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3000 -j ACCEPT   # Port 3000 for IPv4 is OK
    iptables -A INPUT -j REJECT # any other traffic is not welcome - this should be the last line
    service iptables save       # Save IPv4 IPTABLES rules van memory naar disk
    systemctl enable iptables   # To make sure the IPv4 rules are reloaded at system startup
    

    我猜你想要同样的,以防你的系统可能被 IPv6 流量访问(现在或以后的任何时间):

    ip6tables --flush INPUT
    ip6tables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT            # Any packages related to an existing connection are OK
    ip6tables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT   # ssh is OK
    ip6tables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3000 -j ACCEPT   # Port 3000 for IPv6 is OK
    ip6tables -A INPUT -j REJECT # any other traffic is not welcome - this should be the last line
    service ip6tables save       # Save IPv6 IPTABLES rules van memory naar disk
    systemctl enable ip6tables   # To make sure the IPv6 rules are reloaded at system startup
    
    • 2

相关问题

  • 如何在 Linux 机器上找到有关硬件的详细信息?

  • 使用 crontab 和 /etc/cron.hourly,daily,weekly 的区别

  • 持续监控许多服务器运行状况的简单方法?

  • Hudson 无法在 tomcat5 中启动

  • CentOS 的依赖挑战

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve