centos6查看防火墙状态命令(查看防火墙状态及开启关闭命令)

一、查看防火墙状态

CentOS 6采用iptables作为默认的网络防火墙软件。我们可以使用以下命令来查看防火墙状态:

service iptables status

该命令将会返回防火墙的状态信息。如果防火墙未启用,则命令的输出将会提示防火墙已停止:

[root@localhost ~]# service iptables status
iptables: Firewall is not running.

如果防火墙已经启用,则命令的输出将会显示防火墙的规则:

[root@localhost ~]# service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    RH-Firewall-1-INPUT  all  --  0.0.0.0/0            0.0.0.0/0           

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    RH-Firewall-1-INPUT  all  --  0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain RH-Firewall-1-INPUT (2 references)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0            icmp type 255 
3    ACCEPT     esp  --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     ah   --  0.0.0.0/0            0.0.0.0/0           
5    ACCEPT     udp  --  0.0.0.0/0            224.0.0.251          udp dpt:5353 
6    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0            udp dpt:631 
7    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:631 
8    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0            udp dpt:631 
9    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22 
10   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:25 
11   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 
12   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:443 
13   REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited 

可以看到,该命令返回的信息包括了三个Table:

  • filter表:该表包含了所有针对传入和传出流量的规则。
  • nat表:该表用来实现网络地址转换。
  • mangle表:该表用来修改数据报头和数据报标记。

二、启动、停止与重启防火墙

除了查看防火墙状态之外,我们还可以对防火墙进行启动、停止和重启操作。以下是相关的命令:

  • 启动防火墙:service iptables start
  • 停止防火墙:service iptables stop
  • 重启防火墙:service iptables restart
  • 启动防火墙并设置开机自动启动:chkconfig iptables on
  • 禁止防火墙开机自启动:chkconfig iptables off

三、添加、修改和删除防火墙规则

如果我们需要添加、修改或删除防火墙规则,可以使用iptables命令来实现。以下是相关的命令:

  • 添加规则:iptables -A <chain> -p <protocol> –dport <port_number> -j <target>
  • 修改规则:iptables -R <chain> <rule_number> -p <protocol> –dport <port_number> -j <target>
  • 删除规则:iptables -D <chain> <rule_number>

其中,<chain>代表要添加规则的链,<protocol>代表要允许的协议类型,<port_number>代表目标端口号,<target>代表目标规则的动作(比如ACCEPT或DROP)。<rule_number>代表要修改或删除的规则编号。

四、查看已有的防火墙规则

我们可以使用iptables命令来查看当前的防火墙规则。以下是相关的命令:

iptables -L

该命令将会返回所有的防火墙规则:

[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:22 
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

可以看到,该命令返回的信息和service iptables status命令的输出非常类似。其中,INPUT、FORWARD和OUTPUT代表三个不同的链。除了显示规则之外,该命令还会显示默认策略(policy ACCEPT或policy DROP)。

五、总结

本篇文章介绍了CentOS 6中查看防火墙状态的操作方法。我们可以通过service iptables status命令查看防火墙状态,通过service iptables start/stop/restart命令启动、停止和重启防火墙,通过iptables命令添加、修改和删除防火墙规则,以及通过iptables -L命令查看已有的防火墙规则。

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注