cat /etc/redhat-release # 查看centos 版本
Centos7端口常见命令
虚拟机新开了5005端口,系统内部是显示开了的,但是外部不能访问端口。
一些需要用到的命令:
1、firewalld的基本使用
systemctl enable firewalld.service # 设置开机启用防火墙 systemctl disable firewalld.service # 设置开机禁用防火墙 systemctl start firewalld # 启动防火墙 systemctl stop firewalld # 关闭防火墙 systemctl status firewalld # 检查防火墙状态 netstat -lntp | 查看端口 telnet 121.40.177.39 1081 # 查看端口是否通
添加端口
firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,没有此参数重启后失效)
添加端口外部访问权限(这样外部才能访问)
firewall-cmd --add-port=6379/tcp
重新载入,添加端口后重新载入才能起作用
firewall-cmd --reload
这些之后,端口是开启成功的,如果没有成功,重启系统试试。
添加端口
firewall-cmd --permanent --zone=external --remove-port=5555/tcp 1>/dev/null
firewall-cmd --permanent --zone=external --add-port=5555/tcp 1>/dev/null
firewall-cmd --permanent --zone=external --remove-port=6666/udp 1>/dev/null
firewall-cmd --permanent --zone=external --add-port=6666/udp 1>/dev/null
firewall-cmd --reload
firewall-cmd --zone=external --list-ports
查看端口
firewall-cmd --zone=public --query-port=80/tcp
firewall-cmd --zone=public --list-ports 查看所有开发的端口
删除端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent
查看firewall是否运行,下面两个命令都可以
systemctl status firewalld.service
firewall-cmd --state
2、iptables的基本使用
系统命令 systemctl start iptables #启动 systemctl status iptables #查看运行状态 systemctl restart iptables.service #重启 systemctl stop iptables.service #停止 systemctl enable iptables.service #设置开机启动 systemctl disable iptables.service #禁止开机启动 常用命令 iptables -h #查询帮助 iptables -L -n #列出(filter表)所有规则 iptables -L -n --line-number #列出(filter表)所有规则,带编号 iptables -L -n -t nat #列出(nat表)所有规则 iptables -F #清除(filter表)中所有规则 iptables -F -t nat #清除(nat表)中所有规则 service iptables save #保存配置(保存配置后必须重启iptables) systemctl restart iptables.service #重启 filter表解析 INPUT:过滤进入主机的数据包 OUTPUT:处理从本机出去的数据包 FORWARD:负责转发流经本机但不进入本机的数据包,起到转发作用 iptables常用语法 -A:追加到规则的最后一条 -D:删除记录 -I:添加到规则的第一条 -p:(proto)规定通信协议,常见的协议有:tcp、udp、icmp、all -j:(jump)指定要跳转的目标,常见的目标有:ACCEPT(接收数据包)、DROP(丢弃数据包)、REJECT(重定向)三种,但是一般不适用重定向,会带来安全隐患 常见案例 iptables -A INPUT ! -s 192.168.1.3 -j DROP # 禁止192.168.1.3 IP地址的所有类型数据接入 iptables -A INPUT -p tcp --dport 80 -j ACCEPT #开放80端口 iptables -I INPUT -p tcp --dport 22:80 -j ACCEPT #开发22-80范围的端口 iptables -I OUTPUT -p tcp --dport 80 -j DROP vim /etc/sysconfig/iptables # 也可以在这个配置文件添加规则 systemctl reload iptables # 修改完重启一下
查看当前开了哪些端口
其实一个服务对应一个端口,每个服务对应/usr/lib/firewalld/services下面一个xml文件。
查看开启了哪些服务
firewall-cmd --list-services
查看开启了哪些端口
firewall-cmd --list-ports
查看还有哪些服务可以打开
firewall-cmd --get-services
查看所有打开的端口:
firewall-cmd --zone=public --list-ports
更新防火墙规则:
firewall-cmd --reload
查看端口访问权限情况
firewall-cmd --query-port=6379/tcp
添加端口外部访问权限
firewall-cmd --add-port=6379/tcp
另附firewall的其他命令操作
systemctl是CentOS7的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。
启动: systemctl start firewalld
查看状态: systemctl status firewalld
停止: systemctl disable firewalld
禁用: systemctl stop firewalld
启动服务: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
查看版本: firewall-cmd --version
查看帮助: firewall-cmd --help
显示状态: firewall-cmd --state
查看所有打开的端口: firewall-cmd --zone=public --list-ports
更新防火墙规则: firewall-cmd --reload
查看区域信息: firewall-cmd --get-active-zones
查看指定接口所属区域: firewall-cmd --get-zone-of-interface=eth0
拒绝所有包:firewall-cmd --panic-on
取消拒绝状态: firewall-cmd --panic-off
查看是否拒绝: firewall-cmd --query-panic