CentOS 防火墙设置

223 阅读4分钟

一、iptables防火墙

1、 查看防火墙状态

service iptables status
出现Active: active (running)高亮显示则表示是启动状态。
出现 Active: inactive (dead)灰色表示停止状态。

2、 CentOS6关闭防火墙使用以下命令:

# 临时关闭 service iptables stop
# 禁止开机启动 chkconfig iptables off
# 重启防火墙 service iptables restart

3、 CentOS7关闭防火墙使用以下命令:

// 临时关闭 systemctl stop firewalld 
// 禁止开机启动 systemctl disable firewalld

4、 开启80等端口

vim /etc/sysconfig/iptables
加入如下代码
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
或者使用此命令:
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT 

保存退出后重启防火墙
service iptables restart

查看打开的端口
/etc/init.d/iptables status

打开49152~65534之间的端口
iptables -A INPUT -p tcp --dport 49152:65534 -j ACCEPT

【注】参数说明:

–A 参数就看成是添加一条规则
–p 指定是什么协议,我们常用的tcp 协议,当然也有udp,例如53端口的DNS
–dport 就是目标端口,当数据从外部进入服务器为目标端口
–sport 数据从服务器出去,则为数据源端口使用
–j 就是指定是 ACCEPT -接收 或者 DROP 不接收

进入防火墙配置文件:
vi /etc/sysconfig/iptables

添加开放端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
或者
-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT

修改配置文件后需要重启
service  iptables restart

关闭某个端口:在配置文件中去掉配置,重启防火墙就好了

firewall防火墙

查看firewall服务状态
systemctl status firewalld
或者 firewall-cmd --state
出现Active: active (running)高亮显示则表示是启动状态。
出现 Active: inactive (dead)灰色表示停止状态。

开启、重启、关闭 firewalld.service服务

开启
service firewalld start

重启
service firewalld restart

关闭
service firewalld stop
systemctl stop firewalld.service

查看防火墙端口

开放的端口

firewall-cmd --list-all

查询端口是否开放

firewall-cmd --query-port=8080/tcp

开放80端口

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

移除端口

firewall-cmd --permanent --remove-port=8080/tcp

删除

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

重启防火墙(修改配置后要重启防火墙)

firewall-cmd --reload

开机启动

systemctl enable firewalld

停止并禁用开机启动

sytemctl disable firewalld

查看版本

firewall-cmd --version

查看帮助

firewall-cmd --help

查看区域信息

firewall-cmd --get-active-zones

查看指定接口所属区域信息

firewall-cmd --get-zone-of-interface=eth0

拒绝所有包

firewall-cmd --panic-on

取消拒绝状态

firewall-cmd --panic-off

查看是否拒绝

firewall-cmd --query-panic

将接口添加到区域(默认接口都在public)

(永久生效再加上 --permanent 然后重启防火墙) firewall-cmd --zone=public --add-interface=eth0

设置默认接口区域

(立即生效,无需重启) firewall-cmd --set-default-zone=public

更新防火墙规则

(两者的区别就是第一个无需断开连接,就是firewalld特性之一动态添加规则,第二个需要断开连接,类似重启服务) firewall-cmd --reload或firewall-cmd --complete-reload

查看指定区域所有打开的端口

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

在指定区域打开端口(记得重启防火墙)

(永久生效再加上 --permanent) firewall-cmd --zone=public --add-port=80/tcp

【注】:「 参数解释」

–zone 作用域 –add-port=8080/tcp 添加端口,格式为:端口/通讯协议 –permanent #永久生效,没有此参数重启后失效

1、firwall-cmd:是Linux提供的操作firewall的一个工具; 2、--permanent:表示设置为持久; 3、--add-port:标识添加的端口; 【注】CentOS7 默认使用firewalld防火墙

如果想换回iptables防火墙,可关闭firewalld并安装iptables。

1.关闭firewall:
停止firewall:
systemctl stop firewalld.service

禁止firewall开机启动:systemctl disable firewalld.service
查看默认防火墙状态:firewall-cmd --state(关闭后显示notrunning,开启后显示running)

2.安装iptables-services
yum install iptables-services

3.修改防火墙配置文件
vi /etc/sysconfig/iptables

添加端口80、8080、3306、3690端口:

esc :wq! 退出保存修改。
【注】:添加在端口22上面或者下面,不要放在最后,不然不起作用。

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

刚刚yum install iptables.service之后系统如果没有重启,iptables.service是找不到的,会报unit not fount。
设置防火墙开机启动:
systemctl enable iptables.service

————————————————

版权声明:本文为CSDN博主「满眼清香」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:blog.csdn.net/weixin_4403…