配置Ubuntu18.04使iptables规则重启系统后仍然有效

2023-12-13 18:49:43

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

iptables-save > /etc/iptables.rules

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

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

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

chmod +x /etc/rc.local

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

#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

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

systemctl enable rc.local

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

文章来源:https://blog.csdn.net/one312/article/details/134855358
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。