转载自:https://www.cnblogs.com/yj411511/p/11547460.html
日常工作问题解决:centos/linux系统如何检测端口是否打开
1、telnet命令
格式: telnet ip 端口号
[root@centos7-127 ~]# telnet 192.168.87.128 22
Trying 192.168.87.128…
Connected to 192.168.87.128. # 表示端口已经开放
Escape character is ‘^]’.
SSH-2.0-OpenSSH_7.4
[root@centos7-127 ~]# telnet 192.168.87.128 8008
Trying 192.168.87.128…
telnet: connect to address 192.168.87.128: Connection refused # 表示端口未开放
2、nmap命令
格式: nmap ip -p 端口号
Starting Nmap 6.40 ( http://nmap.org ) at 2019-09-19 10:34 CST
Nmap scan report for centos7-128.cn (192.168.87.128)
Host is up (0.00028s latency). # 主机可通
PORT STATE SERVICE
80/tcp open http # 端口开放
MAC Address: 00:0C:29:0C:AE:01 (VMware)
Nmap done: 1 IP address (1 host up) scanned in 0.08 seconds
[root@centos7-127 ~]# nmap 192.168.87.128 -p 8080
Starting Nmap 6.40 ( http://nmap.org ) at 2019-09-19 10:34 CST
Nmap scan report for centos7-128.cn (192.168.87.128)
Host is up (0.00035s latency). # 主机可通
PORT STATE SERVICE
8080/tcp closed http-proxy # 端口关闭
MAC Address: 00:0C:29:0C:AE:01 (VMware)
Nmap done: 1 IP address (1 host up) scanned in 0.03 seconds
3、通过nc命令检查
命令格式:nc -v ip 端口
[root@centos7-127 ~]# nc -v 192.168.87.128 80
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.87.128:80. # 端口开放
^C
[root@centos7-127 ~]# nc -v 192.168.87.128 8080
Ncat: Version 7.50 ( https://nmap.org/ncat ) # 端口关闭
Ncat: Connection refused.
4、使用nc测试端口是否可用
需要测试端口的服务器启动nc服务监听端口
格式: nc -lp 端口
[root@centos7-128 ~]# nc -lp 8081
adfadfjla
测试的服务器使用nc命令连接,然后发送信息
格式: nc ip 端口
[root@centos7-128 ~]# nc 192.168.87.128 8081
adfadfjla
能收到信息,表示端口可用