当前位置:   article > 正文

配置Ubuntu18.04使iptables规则重启系统后仍然有效_iptables保存配置并设置开机生效

iptables保存配置并设置开机生效

第一步:保存iptables规则到文件 /etc/iptables.rules

iptables-save > /etc/iptables.rules

第二步:将如下代码写入 /etc/rc.local

  1. #!/bin/sh
  2. iptables-restore < /etc/iptables.rules

第三步:为 /etc/rc.local 文件赋可执行权限

chmod +x /etc/rc.local

第四步:编辑文件 /lib/systemd/system/rc.local.service 内容如下:

  1. # SPDX-License-Identifier: LGPL-2.1+
  2. #
  3. # This file is part of systemd.
  4. #
  5. # systemd is free software; you can redistribute it and/or modify it
  6. # under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation; either version 2.1 of the License, or
  8. # (at your option) any later version.
  9. # This unit gets pulled automatically into multi-user.target by
  10. # systemd-rc-local-generator if /etc/rc.local is executable.
  11. [Unit]
  12. Description=/etc/rc.local Compatibility
  13. Documentation=man:systemd-rc-local-generator(8)
  14. ConditionFileIsExecutable=/etc/rc.local
  15. After=network.target
  16. [Service]
  17. Type=forking
  18. ExecStart=/etc/rc.local start
  19. TimeoutSec=0
  20. RemainAfterExit=yes
  21. GuessMainPID=no

第五步:设置 rc.local 为开机启动

systemctl enable rc.local

第六步:重启系统后 查看 iptables 规则是否还在

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/902306
推荐阅读
相关标签
  

闽ICP备14008679号