2

测试 Linux 某个端口是否打开

 1 year ago
source link: https://xujinzh.github.io/2022/11/23/linux-test-port-is-opened/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在使用 docker 映射端口时,宿主机已经使用 ufw 命令关闭了映射的端口,但是,外部还是能够访问宿主机的端口来访问 docker 容器,想通过命令行测试端口是否真的能够联通,本篇介绍命令行测试某台 linux 主机的某个端口是否打开。

telnet

telnet 命令最初是用于远程登陆服务器的,但因为数据是明文传送的,因此现在都采用 SSH,但用 telnet 测试远程端口还是很方便的。

服务器端 telnet 默认端口是 23,SSH 默认端口是 22. 这些都可以根据需要更改,建议改端口号范围是 [1024, 65535] 之间。Linux 端口号的范围从 0 到 65535。但是,位于 0 到 1023 这个较低范围内的端口号通常由操作系统使用。

查看 IP:192.168.1.11 的 8888 端口是否打开,命令:

telnet 192.168.1.11 8888
# 如果出现下面的内容,证明端口打开了
Trying 192.168.1.11...
Connected to 192.168.1.11.
Escape character is '^]'.
# 使用快捷键:ctrl + ],可以进入 telnet 提示符界面
# 然后使用 quit 退出 telnet

# 如果没有出现上面的 Connected 内容,则证明端口没有打开

docker 映射端口管理

如果使用如下命令映射,则无法使用 ufw 管理映射的端口:

docker run -itd --name ubuntu-test -p 8888:80 ubuntu:latest bash

则宿主机的端口 8888 无法使用 ufw 管理。想要 8888 端口不让外部服务器连接,可以尝试如下命令:

docker run -itd --name ubuntu-test -p 127.0.0.1:8888:80 ubuntu:latest bash

ufw 无法管理 docker 映射端口的原因是 docker 绕过防火墙的原理是修改了 iptables,那不让它修改即可,方法可参见 linux防火墙firewall无法禁用docker端口映射问题

  1. linux 检测远程端口是否打开

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK