方法一:netstat
安装netstat
apt-get update apt-get install -y net-tools
netstat -tuln
-t
:显示 tcp 连接。-u
:显示 udp 连接。-l
:只显示监听状态的端口。-n
:以数字形式显示地址和端口号。
方法二:telnet
安装telnet
apt-get update apt-get install -y telnet
telnet localhost 8080
使用 telnet
发送 http 请求是一个很好的方法来测试和调试 web 服务器。以下是如何通过 telnet
发送 http 请求的步骤。
连接到服务器:使用 telnet
连接到目标服务器和端口(通常是 80 端口用于 http,443 端口用于 https,但 https 需要使用 ssl,所以通常不通过 telnet
来测试)。
telnet example.com 80
发送 http 请求:连接成功后,你可以手动输入 http 请求。http 请求的基本格式如下:
get / http/1.1 host: example.com
请注意,每个请求行后都需要一个空行,以表示请求的结束。
示例如下:
root@9c2b177de1f5:/demo# telnet localhost 8080 trying ::1... connected to localhost. escape character is '^]'. get /hello http/1.1 host:localhost http/1.1 200 ok content-type: text/plain; charset=utf-8 date: wed, 16 oct 2024 07:19:36 gmt content-length: 11 ok service2
方法三:curl
安装curl
apt-get update apt-get install -y curl
curl -i http://localhost:8080
如果服务在该端口上运行,您应该会收到 http 响应头。
方法四:lsof
安装lsof
apt-get update apt-get install -y lsof
lsof
命令可以用来查看当前系统中打开的文件和使用的网络端口。首先,你需要确保容器中安装了 lsof
。
root@9c2b177de1f5:/demo# lsof -i :8080 command pid user fd type device size/off node name main 444 root 4u ipv6 182989 0t0 tcp *:http-alt (listen) main 444 root 8u ipv6 182998 0t0 tcp 9c2b177de1f5:http-alt->172.17.0.1:37130 (established) main 444 root 9u ipv6 195023 0t0 tcp 9c2b177de1f5:http-alt->172.17.0.1:40346 (established)
到此这篇关于详解在linux中如何确定指定端口是否开启的文章就介绍到这了,更多相关linux确定指定端口是否开启内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论