云服务器免费试用

iptables配置(iptables配置文件位置)

服务器知识 0 1456

本文目录:

  • 1、iptables规则持久化,启动配置
  • 2、iptables简单配置DMZ
  • 3、CentOS-7.5 使用 iptables

iptables规则持久化,启动配置

sudo sh -c "iptables-save /etc/iptables.rules"

使用该命令可以保存当前系统的iptables规则到特定文件。可以直接使用 iptables-save /etc/iptables.rules 命令字串,但为了避免可能触发的重定向的权限问题,这里使用了 -c 选项,将这条字串变成一个整体的命令来执行。

(注意:如果对该文件直接进行了错误的配置,可能会关闭或禁用所有的网络接口,导致你无法远程连接)

对该文件修改前,我们需要知道是对哪个网络接口进行配置。确定后,将相应的规则在相应的接口配置记录下面:

pre-up iptables-restore /etc/iptables.rules

pre-up表示网卡启动前的动作

post-down iptables-restore /etc/iptables.downrules

此外也可以配置相应的网卡关闭后的动作post-down,这里我们用不同的规则文件区分开来

完整的接口配置如下所示:

(第一行‘auto’表示自动启动,‘inet dhcp’表示dhcp方式管理ip等参数设置)

sudo sh -c "iptables-save -c /etc/iptables.rules"

此外,上述命令iptables-save 使用 -c 表示counter,即同时记录规则和网络收发的数据包的数量。

在这两个文件夹中新建相应的脚本文件(文件名不能包含“·”)

新建/etc/network/if-pre-up.d/iptablesload

新建/etc/network/if-post-down.d/iptablessave

之后我们还要确保脚本可执行

sudo chmod +x /etc/network/if-post-down.d/iptablessave

sudo chmod +x /etc/network/if-pre-up.d/iptablesload

sudo apt install iptables-persistent

安装完成后,使用 netfilter-persistent 命令管理

将所想要保存的规则写入相应的rules.ipv4/rules.ipv6文件

iptables-save /etc/iptables/rules.v4

ip6tables-save /etc/iptables/rules.v6

参考

iptables简单配置DMZ

内网网段为:192.168.1.0/24

DMZ区网段为:172.16.1.0/24

新建iptables.sh,内容如下:

CentOS-7.5 使用 iptables

1、关闭 firewalld

由于 firewalld 是 CentOS-7系列的默认防火墙,因此如果想使用iptables的话,需要将CentOS-7系列的默认防火墙 firewalld 关闭,并安装 iptables-services 

[root@shell ~]#

[root@shell ~]# systemctl  stop  firewalld.service            #关闭

[root@shell ~]# systemctl  disable  firewalld.service        #禁止开机自启动

[root@shell ~]# firewall-cmd  --state          #查看firewalld是否在运行

not  running

[root@shell ~]#

2、安装iptables防火墙

[root@shell ~]#

[root@shell ~]# yum install iptables-services                  #安装iptables服务

[root@shell ~]# systemctl  start  iptables

[root@shell ~]# systemctl  enable  iptables                      #设置iptables为开机自启动

[root@shell ~]#

[root@shell ~]# cat  /etc/sysconfig/iptables            #查看iptables默认配置

[root@shell ~]# 

[root@shell ~]#

[root@shell ~]# systemctl  start  iptables             #启动 iptables

[root@shell ~]# systemctl  status  iptables            #查看状态

[root@shell ~]#

[root@shell ~]# iptables  -L                  #查看当前防火墙规则

3、配置防火墙规则

【iptables配置】的内容来源于互联网,如引用不当,请联系我们修改。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942@qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: iptables配置(iptables配置文件位置)
本文地址: https://solustack.com/26690.html

相关推荐:

网友留言:

我要评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。