Linux(17)Centos5、6、7、8版本的防火墙常用命令

简介: Linux(17)Centos5、6、7、8版本的防火墙常用命令

有个业务需求 , 需要设置服务器防火墙 ,

服务器是在阿里云上的 , 使用的是centos 6.8 ,

开始使用的是firewall-cmd命令进行设置 , 发现并没有该命令 , 尝试安装yum install firewalld

装不上去 , 直觉告诉我版本号问题 , 去查询系统版本号 , 果然centos 6.8

查询资料发现6 和 7的设置方法不一样 , firewall-cmd是centos7的设置方法 , iptables是centos6.x设置方法。

ps:

阿里云和腾讯的防火墙要进 云服务器控制台 安全组进行添加。

一、centos5和6的防火墙常用命令

提示:【该此命令只对5和6版本兼用】。

1. 对防火墙iptables的相关操作

代码如下(示例):

  chkconfig --list | grep iptables 查看防火墙的服务

  service iptable status --- 查看防火墙状态

  servcie iptables stop -- 临时关闭防火墙

  service iptables start -- 临时启动防火墙

  service iptables restart -- 重启防火墙

  chkconfig iptables off -- 永久关闭防火墙

  chkconfig iptables on -- 永久开启防火墙

2.防火墙的端口的开放/阻止

代码如下(示例)以下用的参数说明::

  【-A】:添加一条INPUT的规则

  【-p】:指定是什么协议

  【--dport】:就是目标端口,当数据从外部进入服务器为目标端口

  【--sport】:数据从服务器出去,则为数据原端口

  【-j】:就是指定是ACCEPT接受或阻止DROP

  1:查看防火墙开放的端口在文件:/etc/sysconfig/iptables中

    cat /etc/sysconfig/iptables

开放/阻止所有服务器对某个端口的访问

    如开放8080端口

    1:vim /etc/sysconfig/iptables 进入编辑模式 添加一条:

      开放:-A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT

      阻止:-A INPUT -p tcp -m tcp --dport 8080 -j DROP

    2:重启iptables服务:service iptables restart

    3:保存配置信息:/etc/rc.d/init.d/iptables save

禁止/允许某个ip访问

    开放:-A INPUT -p tcp -s 192.xxx.xxx.xxx -j ACCEPT

    阻止:-A INPUT -p tcp -s 192.xxx.xxx.xxx -j DROP

    注:其中【-s】表示来源的意思

    然后重启服务和保存配置

对内网某机器单独开放某个端口:

    iptables -A INPUT -s 192.xxx.xxx.xxx -p tcp -m tcp --dport 3xxx -j ACCEPT

    iptables -A OUTPUT -s 192.xxx.xxx.xxx -p tcp -m tcp --sport 3xxx -j ACCEPT

    然后重启服务和保存配置

    注:INPUT表示对外,OUTPUT表示对内

xxx=代表随意填充数字。


二、centos7的防火墙常用命令

1. 对防火墙iptables的相关操作

代码如下(示例):

>>>关闭防火墙

systemctl stop firewalld.service #停止firewall

systemctl disable firewalld.service #禁止firewall开机启动

>>>开启端口

firewall-cmd --zone=public --add-port=80/tcp --permanent

命令含义

--zone #作用域

--add-port=80/tcp #添加端口,格式为:端口/通讯协议

--permanent #永久生效,没有此参数重启后失效

2、设置 iptables service

检测并安装iptables 

yum -y install iptables-services

>>>如果要修改防火墙配置,如增加防火墙端口3306

vi /etc/sysconfig/iptables

>>>增加规则

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

保存退出后

systemctl restart iptables.service #重启防火墙使配置生效

systemctl enable iptables.service #设置防火墙开机启动

>>>重启防火墙

firewall-cmd --reload

其他常用命令:

firewall-cmd --state ##查看防火墙状态,是否是running

firewall-cmd --reload ##重新载入配置,比如添加规则之后,需要执行此命令

firewall-cmd --get-zones ##列出支持的zone

firewall-cmd --get-services ##列出支持的服务,在列表中的服务是放行的

firewall-cmd --query-service ftp ##查看ftp服务是否支持,返回yes或者no

firewall-cmd --add-service=ftp ##临时开放ftp服务

firewall-cmd --add-service=ftp --permanent ##永久开放ftp服务

firewall-cmd --remove-service=ftp --permanent ##永久移除ftp服务

firewall-cmd --add-port=80/tcp --permanent ##永久添加80端口

iptables -L -n ##查看规则,这个命令是和iptables的相同的

man firewall-cmd ##查看帮助

更多命令,使用 firewall-cmd --help 查看帮助文件

三、centos8的防火墙常用命令

1. 对防火墙iptables的相关操作

yum install iptables-services #安装iptables(防火墙服务)

systemctl使用

systemctl unmask firewalld #执行命令,即可实现取消服务的锁定

systemctl mask firewalld # 下次需要锁定该服务时执行

systemctl start firewalld.service #启动防火墙

systemctl stop firewalld.service #停止防火墙

systemctl reloadt firewalld.service #重载配置

systemctl restart firewalld.service #重启服务

systemctl status firewalld.service #显示服务的状态

systemctl enable firewalld.service #在开机时启用服务

systemctl disable firewalld.service #在开机时禁用服务

systemctl is-enabled firewalld.service #查看服务是否开机启动

systemctl list-unit-files|grep enabled #查看已启动的服务列表

systemctl --failed #查看启动失败的服务列表

firewall-cmd使用

firewall-cmd --state #查看防火墙状态

firewall-cmd --reload #更新防火墙规则

firewall-cmd --state #查看防火墙状态

firewall-cmd --reload #重载防火墙规则

firewall-cmd --list-ports #查看所有打开的端口

firewall-cmd --list-services #查看所有允许的服务

firewall-cmd --get-services #获取所有支持的服务

#区域相关

firewall-cmd --list-all-zones #查看所有区域信息

firewall-cmd --get-active-zones #查看活动区域信息

firewall-cmd --set-default-zone=public #设置public为默认区域

firewall-cmd --get-default-zone #查看默认区域信息

firewall-cmd --zone=public --add-interface=eth0 #将接口eth0加入区域public

#接口相关

firewall-cmd --zone=public --remove-interface=eth0 #从区域public中删除接口eth0

firewall-cmd --zone=default --change-interface=eth0 #修改接口eth0所属区域为default

firewall-cmd --get-zone-of-interface=eth0 #查看接口eth0所属区域

例子

firewall-cmd --query-port=8080/tcp # 查询端口是否开放

firewall-cmd --add-port=80/tcp --permanent #永久添加80端口例外(全局)

firewall-cmd --remove-port=80/tcp --permanent #永久删除80端口例外(全局)

firewall-cmd --add-port=65001-65010/tcp --permanent #永久增加65001-65010例外(全局)

firewall-cmd --zone=public --add-port=80/tcp --permanent #永久添加80端口例外(区域public)

firewall-cmd --zone=public --remove-port=80/tcp --permanent #永久删除80端口例外(区域public)

firewall-cmd --zone=public --add-port=65001-65010/tcp --permanent #永久增加65001-65010例外(区域public)

修改配置后重启防火墙

firewall-cmd --reload #重启防火墙(修改配置后要重启防火墙)


[root@iZuf63nu0s9j3yizi2f8e0Z ~]# cat /proc/version //确定操作系统版本号
Linux version 2.6.32-696.6.3.el6.x86_64 (mockbuild@c1bl.rdu2.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) ) #1 SMP Wed Jul 12 14:17:22 UTC 2017
[root@iZuf63nu0s9j3yizi2f8e0Z ~]# cat /etc/issue //确定操作系统版本号
CentOS release 6.8 (Final)
[root@iZuf63nu0s9j3yizi2f8e0Z ~]# iptables -A INPUT -p tcp -m tcp --dport 7000 -j ACCEPT
[root@iZuf63nu0s9j3yizi2f8e0Z ~]# iptables -A INPUT -p tcp -m tcp --dport 7500 -j ACCEPT
[root@iZuf63nu0s9j3yizi2f8e0Z ~]# service iptables status //查看防火墙状态
表格:filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:7500 
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:7000 
 
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
 
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination 
[root@iZuf63nu0s9j3yizi2f8e0Z ~]# cat /etc/sysconfig/iptables //查看防火墙设置是否保存
----------------------------------------------------------
# Generated by iptables-save v1.4.7 on Tue Oct 11 09:48:01 2022
*filter
:INPUT ACCEPT [20942:97107726]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [21730:96992134]
-A INPUT -p tcp -m tcp --dport 7500 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 7000 -j ACCEPT
COMMIT
# Completed on Tue Oct 11 09:48:01 2022
----------------------------------------------------------


相关文章
|
4天前
|
前端开发 jenkins 持续交付
新的centos7.9安装docker版本的jenkins2.436.1最新版本-前端项目发布(五)
新的centos7.9安装docker版本的jenkins2.436.1最新版本-前端项目发布(五)
20 1
|
2天前
|
运维 Linux
CentOS系统openssh-9,你会的还只有初级Linux运维工程师的技术吗
CentOS系统openssh-9,你会的还只有初级Linux运维工程师的技术吗
|
4天前
|
Java 开发工具 git
新的centos7.9安装docker版本的jenkins2.436.1最新版本-项目发布(三)
新的centos7.9安装docker版本的jenkins2.436.1最新版本-项目发布(三)
12 4
|
4天前
|
Linux
Centos 常见的命令
Centos 常见的命令
11 0
|
4天前
|
Web App开发 缓存 运维
CentOS命令大全:从入门到精通
CentOS命令大全:从入门到精通
|
4天前
|
网络协议 Linux 网络安全
CentOS 7 防火墙指令
本文介绍了CentOS 7中管理防火墙`firewalld`的指令。
14 0
|
4天前
|
监控 网络协议 Linux
centos常见的命令
【5月更文挑战第8天】了解和掌握基本的CentOS命令对于服务器管理至关重要。本文介绍了19个常用命令,包括`ls`、`cd`、`mkdir`、`rm`、`grep`、`find`、`sudo`、`vi/vim`、`cp/mv`、`du/df`、`history`、`top/htop`、`ps`、`netstat/ss`、`yum/dnf`、`ssh`、`iptables`、`journalctl`、`crontab`和`systemctl`。每个命令都提到了常见问题、使用技巧和避免错误的方法,帮助用户更好地管理和维护CentOS系统。
27 0
|
4天前
|
存储 安全 Linux
linux centos7中使用 Postfix 和Dovecot搭建邮件系统
linux centos7中使用 Postfix 和Dovecot搭建邮件系统
|
4天前
|
Linux 网络安全 数据库
linux centos系统搭建samba文件服务器 NetBIOS解析 (超详细)
linux centos系统搭建samba文件服务器 NetBIOS解析 (超详细)
|
4天前
|
Linux 网络安全
Linux(CentOS6.5)开放端口,配置防火墙
Linux(CentOS6.5)开放端口,配置防火墙
30 0
http://www.vxiaotou.com