CentOS防火墙篇(主要讲firewalld)

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • CentOS6.5查看防火墙的状态: [zh@localhost ~]$service iptable statu...
    b17b782f6d63阅读 2,069评论 0 0
  • 1.安全技术 (1)入侵检测与管理系统(Intrusion Detection Systems): 特点是不阻断任...
    尛尛大尹阅读 2,488评论 0 2
  • CentOS各个版本中的命令有所不同,使用起来比较麻烦,感觉有点乱糟糟的,在此总结一下防火墙在不同CentOS中的...
    GHope阅读 577评论 0 9
  • CentOS6.5查看防火墙的状态: [zh@localhost ~]$service iptable statu...
    克鲁德李阅读 894评论 0 0
  • Centos7以上的防火墙用了新命令,下面我们来说一下之两种防火墙的使用1.centos7以下一般服务器自带ipt...
    YangEvol阅读 1,608评论 0 0