CentOS防火墙篇(主要讲firewalld)

2018-12-17  本文已影响0人  明月___

1. firewalld

>安装启用firewalld防火墙

# yum install firewalld  //安装
# systemctl start firewalld  //启动
# systemctl status firewalld
# systemctl enable firewalld
# firewall-cmd --state //查看防火墙状态(关闭后显示not running,开启后显示running)

>自定义添加端口

>>查看开放了哪些端口

firewall-cmd --zone=public --list-ports

>>添加端口

1. firewall-cmd --permanent --add-port=9527/tcp  //永久添加9527端口,协议为tcp
2. firewall-cmd --reload  //重新加载

>>删除端口

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

参数介绍:
1、firewall-cmd:是Linux提供的操作firewall的一个工具;
2、--permanent:表示设置为持久;
3、--add-port:标识添加的端口

另外,firewall中有Zone的概念,可以将具体的端口制定到具体的zone配置文件中。
例如:添加8010端口

firewall-cmd --zone=public --permanent --add-port=8010/tcp  
// --zone=public:指定的zone为public;

>关闭防火墙

# systemctl stop firewalld  // 关闭
# systemctl disable firewalld  // 取消开机启动
# firewall-cmd --state  // 查看状态

>firewalld防火墙常用命令

# systemctl start firewalld  // 启动,
# systemctl enable firewalld  // 开机启动
# systemctl stop firewalld  // 关闭
# systemctl disable firewalld  // 取消开机启动
# service firewalld restart // 重启
# firewall-cmd --reload //更新防火墙规则
# systemctl status firewalld //查看状态
# firewall-cmd --state  // 查看状态(这个简单点只有running和not running)
# firewall-cmd --list-all  // 查看防火墙规则列表
# firewall-cmd --list-ports //查看所有的开放端口
# firewall-cmd --zone=public --list-ports //查看zone是public的开放端口
# firewall-cmd --zone= public --remove-port=80/tcp --permanent //删除80端口

2. iptables

CentOS 7中默认是firewalld防火墙,如果使用iptables需要先关闭firewalld防火墙(关闭防火墙,取消开机启动)。

# systemctl stop firewalld   // 关闭
# systemctl disable firewalld  // 取消开机启动
# yum install iptables-services  //安装
# vi /etc/sysconfig/iptables     //修改防火墙配置文件
# systemctl restart iptables.service  //重启防火墙使配置生效
# systemctl enable iptables.service  //设置防火墙开机启动

修改配置文件时,添加端口格式如下:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

iptables的规则书写

基本语法:

iptables [-t 表] [操作命令] [链][规则匹配器][-j 目标动作]

iptables
[-t filter]
[-AI INPUT,OUTPUT,FORWARD]
[-io interface]
[-p tcp,udp.icmp,all]
[-s ip/nerwork]
[–sport ports]
[-d ip/network]
[–dport ports]
[-j ACCEPT DROP REJECT REDIRECT MASQUERADE LOG
DNAT SNAT MIRROR QUEUE RETURN MARK]

常用操作命令:
-A 在指定链尾部添加规则
-D 删除匹配的规则
-R 替换匹配的规则
-I 在指定位置插入规则
例:iptables -I INPUT 1 –dport 80 -j ACCEPT
(将规则插入到filter表INPUT链中的第一位上)

关于systemctl

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

启动一个服务:systemctl start firewalld.service
关闭一个服务:systemctl stop 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

参考链接:
https://blog.csdn.net/yyycheng/article/details/79753032
https://blog.csdn.net/weixin_40658000/article/details/78708375
https://www.cnblogs.com/hubing/p/6058932.html

上一篇 下一篇

猜你喜欢

热点阅读