由于centos版本不同,开启端口号方式也不一样,centos6是iptables,centos7是firewall。

查看防火墙对外开放了哪些端口

#iptables -L -n

CentOS 通过 id 查看端口 centos如何查看端口_端口号

centos6.0防火墙操作:

配置文件:/etc/sysconfig/iptables

开启某个端口号有两种方式:一种是命令方式,一种是修改配置文件方式

查看防火墙状态:service iptables status
开启防火墙(重启后永久生效):chkconfig --level 5 iptables on
关闭防火墙(重启后永久生效):chkconfig --level 5 iptables off
开启防火墙(即时生效,重启后失效):service iptables start
关闭防火墙(即时生效,重启后失效):service iptables stop
重启防火墙:service iptables restartd

查看开启的端口号
service iptables status

开启某个端口号(如80端口号,命令方式)
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
保存开启的端口号
service iptables save
重新启动防火墙
service iptables restart

开启某个范围的端口号(如18881~65534,命令方式)
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 18881:65534 -j ACCEPT
保存开启的端口号
service iptables save
重新启动防火墙
service iptables restart

通过修改配置文件开启端口号(如80端口号)
vi /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
启动防火墙
service iptables restart

参数说明:
–A 参数就看成是添加一条规则
–p 指定是什么协议,我们常用的tcp 协议,当然也有udp,例如53端口的DNS
–dport 就是目标端口,当数据从外部进入服务器为目标端口

–j 就是指定是 ACCEPT -接收 或者 DROP 不接收

CentOS 通过 id 查看端口 centos如何查看端口_重启_02

centos7.0防火墙操作:

安装firewall插件:yum install firewalld firewalld-config

配置文件:/etc/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

查看防火墙状态:firewall-cmd --state
开启防火墙:systemctl start firewalld
关闭防火墙:systemctl stop firewalld
设置开机启动:systemctl enable firewalld
停止并禁用开机启动:sytemctl disable firewalld
重启防火墙:firewall-cmd --reload

查看指定区域所有开启的端口号
firewall-cmd --zone=public --list-ports

在指定区域开启端口(如80端口号,命令方式)
firewall-cmd --zone=public --add-port=80/tcp --permanent
重新启动防火墙
firewall-cmd --reload

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

在指定区域开启某个范围的端口号(如18881~65534,命令方式)
firewall-cmd --zone=public --add-port=18881:65534/tcp --permanent
重新启动防火墙
firewall-cmd --reload

通过配置文件方式开启端口号