Linux防火墙/开放端口

Linux防火墙

简介

在默认情况下,Linux系统的防火墙状态是打开的,已经启动,
CentOS 7默认使用的是firewall作为防火墙。
用户根据需求在/etc/sysconfig/firewalld配置文件中来配置防火墙,
控制本机的“出、入”网络访问行为,其对行为的配置策略有四个策略表
如果防火墙开启,我们pingLinux服务器的IP会ping不通,所以我们要对防火墙进行设置

centos7

查询防火墙状态
语法:
systemctl   动作   服务名.service(.service可以省略)

解释:
systemctl :是管制服务的主要工具。
动作:status/start/stop/disable/enable/restart。
服务名.service : 管理的服务,有firewalld.servcie是防火墙服务。

1、firewalld的基本使用

启动防火墙:

systemctl start firewalld

查看防火墙状态:

systemctl status firewalld

停止防火墙:

systemctl stop firewalld

防止防火墙开机自启

systemctl disable firewalld

备注:

Active: active (running)切高亮显示则表示是启动状态。
Active: inactive (dead)灰色表示停止,看单词也行。

查看firewall的状态

firewall-cmd --state

查看防火墙规则

firewall-cmd --list-all

开放端口

查看已经开放的端口:

firewall-cmd --list-ports

查询8080端口是否开放

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

开启端口

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

移除端口

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

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

firewall-cmd --reload

systemctl  restart  firewalld 

命令含义:

firwall-cmd:是Linux提供的操作firewall的一个工具;

–zone 	#作用域

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

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

centos7之前版本

iptables防火墙

1、基本操作

查看防火墙状态

service iptables status 

停止防火墙

service iptables stop 

启动防火墙

service iptables start 

重启防火墙

service iptables restart 

禁止防火墙开机自启

chkconfig iptables off 

开机重启防火墙

chkconfig iptables on 

开放端口

以80端口为例

vim /etc/sysconfig/iptables

添加配置

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

保存退出后重启防火墙

service iptables restart

如要开放80,22,8080 端口,输入以下命令即可

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

然后保存:

/etc/rc.d/init.d/iptables save

查看打开的端口:

/etc/init.d/iptables status
原创文章 76 获赞 97 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43671437/article/details/105964943