Linux系统下怎么测试端口的连通性

Linux系统下怎么测试端口的连通性,第1张

可以使用nc命令测试。

例如测试一下 某个个IP 的80 端口有没有开启可以使用命令:nc -z -w 1 “IP地址”  80

可以看到默认是使用TCP进行测试的,如果要测试UDP端口有没有开放的可以添加-u 一起使用。例如我测试一下202.96.128.86 这个IP的UDP 53端口:nc -u -z -w 1 “ip地址”

6、上面可以看到成功的会显示相关的信息,但是如果测试到端口是不开放的或者被防火墙拦截的就不会返回相关的信息。

一、安装telnet

1、检测telnet-server的rpm包是否安装

[root@localhost ~]# rpm -qa telnet-server

若无输入内容,则表示没有安装。出于安全考虑telnet-server.rpm是默认没有安装的,而telnet的客户端是标配。即下面的软件是默认安装的。

2、若未安装,则安装telnet-server,否则忽略此步骤

[root@localhost ~]#yum install telnet-server  

3、检测telnet-server的rpm包是否安装

[root@localhost ~]# rpm -qa telnet

telnet-0.17-47.el6_3.1.x86_64

4、若未安装,则安装telnet,否则忽略此步骤

[root@localhost ~]# yum install telnet

二、重新启动xinetd守护进程

由于telnet服务也是由xinetd守护的,所以安装完telnet-server,要启动telnet服务就必须重新启动xinetd

[root@locahost ~]#service xinetd restart

三、测试

我们先来查看TCP的23端口是否开启正常

[root@localhost ~]#netstat -tnl |grep 23

tcp 0 0 0.0.0.0:23 0.0.0.0:* LISTEN

如果上面的一行存在就说明服务已经运行了。如果netstat命令没有返回内容,我们就只好继续进行更深入的配置了。

四、连接到 memcached

telnet ip 端口,如:

[root@localhost proc]# telnet localhost 11211

Trying ::1...

Connected to localhost.

Escape character is '^]'.

表明连接成功。

(stats查看状态,flush_all:清楚缓存)

五、遇到的问题

1、telnet: connect to address 127.0.0.1: Connection refused的错误信息

[root@localhost software]# telnet localhost 11211

Trying ::1...

telnet: connect to address ::1: Connection refused

Trying 127.0.0.1...

telnet: connect to address 127.0.0.1: Connection refused

[root@localhost software]# rpm -qa telnet-server

检查原因是没有安装telenet-server的服务。

解决方法:[root@localhost software]# yum install telnet-server

2、[root@localhost ~]#netstat -tnl |grep 23 没有返回内容

解决方法:

[root@localhost ~]vi /etc/xinetd.d/telnet

service telnet

{

flags = REUSE

socket_type = stream

wait= no

user= root

server = /usr/sbin/in.telnetd

log_on_failure += USERID

disable = yes

}

将disable项由yes改成no。

[root@localhost ~]/etc/init.d/xinetd restart

翻译自:

How to Test Port[TCP/UDP] Connectivity from a Linux Server (文档 ID 2212626.1)

适用于:

Linux OS - Version Oracle Linux 5.0 to Oracle Linux 6.8 [Release OL5 to OL6U8]

Information in this document applies to any platform.

目标:

在Linux服务器上检查TCP/UDP端口的连通性。

解决方案:

telnet和nc 是用来测试端口连通性的一般工具。

telnet可以测试tcp端口的连通性。

nc可以测试tcp和udp端口的连通性。

请确保telnet和nc工具已经安装

在CODE上查看代码派生到我的代码片

# yum install nc

# yum install telnet

测试tcp端口的连通性:

语法如下:

在CODE上查看代码片派生到我的代码片

telnet <hostname/IP address><port number>

如下是连通成功的例子:

在CODE上查看代码片派生到我的代码片

# telnet 192.118.20.95 22

Trying 192.118.20.95...

Connected to 192.118.20.95.

Escape character is '^]'.

SSH-2.0-OpenSSH_6.6.1

Protocol mismatch.

Connection closed by foreign host.

如下是连通不成功的例子:

在CODE上查看代码片派生到我的代码片

# telnet 192.118.20.95 22

Trying 192.118.20.95...

telnet: connect to address 192.118.20.95: No route to host

使用nc命令来测试tcp端口的连通性:

语法:

在CODE上查看代码片派生到我的代码片

nc -z -v <hostname/IP address><port number>

如下是连通成功的例子:

在CODE上查看代码片派生到我的代码片

# nc -z -v 192.118.20.95 22

Connection to 192.118.20.95 22 port [tcp/ssh] succeeded!

如下是连通不成功的例子:

在CODE上查看代码片派生到我的代码片

# nc -z -v 192.118.20.95 22

nc: connect to 192.118.20.95 port 22 (tcp) failed: No route to host

使用nc命令来测试udp端口的连通性:

语法:

在CODE上查看代码片派生到我的代码片

nc -z -v -u <hostname/IP address><port number>

在CODE上查看代码片派生到我的代码片

# nc -z -v -u 192.118.20.95 123

Connection to 192.118.20.95 123 port [udp/ntp] succeeded!

nc检测端口的用法

nc -z -w 10 %IP%%PORT%

-z表示检测或者扫描端口

-w表示超时时间

-u表示使用UDP协议


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7215740.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-03
下一篇 2023-04-03

发表评论

登录后才能评论

评论列表(0条)

保存