linux系列之-—09 安全相关 防火墙

由于linux 系统版本不同,默认集成的防火墙工具不同。遇到防火墙相关问题,第一步先要确认自己系统是什么版本的:cat /etc/redhat-release

一. CentOS 6 防火墙常用命令

Iptables(软件)是unix/linux自带的一款优秀且开放源代码的完全自由的基于包顾虑的防火墙工具,它是linux2.4及2.6内核中集成的服务,功能十分强大,使用灵活,可以对流入和流出服务器的数据包进行很精细的控制。

  1. 安装防火墙
    yum install -y iptables
    yum install iptables-services

  2. 防火墙基本操作(永久性生效,重启后不会复原)
    开启:chkconfig iptables on
    关闭:chkconfig iptables off

  3. 防火墙基本操作(即时生效,重启后复原)

3.1 重启防火墙
方式一:/etc/init.d/iptables restart
方式二:service iptables restart

3.2 关闭防火墙:
方式一:/etc/init.d/iptables stop
方式二:service iptables stop

3.3 启动防火墙
方式一:/etc/init.d/iptables start
方式二:service iptables start

3.4 查看防火墙状态
service iptables status

3.5 查看防火墙规则
iptables -L -n
  1. 防火墙基本操作---开启接口相关
步骤1. 找到防火墙配置文件并修改
目录在/etc/sysconfig/iptables

在里面增加行代码,代码如下:
-A RH-Firewall-1-INPUT -p tcp -m state –state NEW -m tcp –dport 8088 -j ACCEPT

此代码必须放在以下代码之上
-A RH-Firewall-1-INPUT -j REJECT –reject-with icmp-host-prohibited之上,否则不会起作用具体的配置文件如下图:

步骤2. 重启防火墙以便改动生效
service iptables restart

二. CentOS 7 防火墙常用命令

CentOS7 默认的防火墙不是 iptables,而是 firewalle。

  1. 查看firewall服务状态
    systemctl status firewalld
    出现Active: active (running)切高亮显示则表示是启动状态。
    出现 Active: inactive (dead)灰色表示停止,看单词也行。

  2. 防火墙基本操作

2.1 显示状态: 
systemctl status firewalld
firewall-cmd --state

2.2 开启防火墙 
systemctl start firewalld
service firewalld start

2.3 关闭防火墙 
systemctl stop firewalld
service firewalld stop

2.4 禁用防火墙 
systemctl disable firewalld

2.5 重启防火墙 
service firewalld restart

2.6 若遇到无法开启
即启动报错:Failed to start firewalld.service: Unit is masked.
先用:systemctl unmask firewalld.service
然后:systemctl start firewalld.service

2.7 查看所有打开的端口: 
netstat -anp

systemctl是 CentOS7 的服务管理工具中主要的工具,它融合之前 service 和 chkconfig 的功能于一体

  1. 防火墙基本操作---开启接口相关

firwall-cmd:是 Linux 提供的操作 firewall 的一个工具,其常用命令如下:

关键字 意义
firewall-cmd --version ##查看版本
firewall-cmd --help ##查看帮助
firewall-cmd --state ##查看防火墙状态,是否是running
firewall-cmd --reload ##重新载入配置,比如添加规则之后,需要执行此命令
firewall-cmd --get-zones ##列出支持的zone
firewall-cmd --get-services ##列出支持的服务,在列表中的服务是放行的
firewall-cmd --list-all ##查看防火墙规则
firewall-cmd --list-ports ##查看所有打开的端口
firewall-cmd --query-service ftp ##查看ftp服务是否支持,返回yes或者no
firewall-cmd --add-service=ftp ##临时开放ftp服务
firewall-cmd --add-service=ftp --permanent ##永久开放ftp服务,--permanent永久生效
firewall-cmd --remove-service=ftp --permanent ##永久移除ftp服务,--permanent永久生效
firewall-cmd --zone= public --query-port=80/tcp ##查看80端口是否支持
firewall-cmd --zone= public --add-port=80/tcp --permanent ##永久添加80端口,--permanent永久生效
firewall-cmd --zone= public --remove-port=80/tcp --permanent ##永久移除80端口
posted @ 2021-03-10 11:31  雨 燕  阅读(93)  评论(0编辑  收藏  举报