nc
在 VM 上执行时工作正常。
Connection to 10.0.0.10 22 port [tcp/ssh] succeeded!
但是当我在我的 docker 容器中执行相同的命令时,它会给出以下未知错误/异常。
10.0.0.10: inverse host lookup failed: Unknown host
(UNKNOWN) [10.0.0.10] 22 (ssh) open
下面是我正在使用的
nc
命令:
nc -vz 10.0.0.10 22 -w 4
“Inverse host lookup failed”简单来说就是
nc
想打印10.0.0.10对应的主机名,但是打印不出来
UNKNOWN 就是它随后打印的主机名。
这与容器外发生的“我查了一下,但它似乎没有任何对应”是不同的。
明确地说,连接到主机成功,但从 IP 地址查找其名称失败。这只是一条信息性警告消息,不是硬错误;无论如何,查找完全是可选的,并且可以使用
-n
禁用。
如果你真的想避免这个警告而不是切换到
-n
,你需要在容器内设置工作DNS。
只需在侦听器和客户端的两侧提供
-n
选项即可消除此错误,因为它会忽略使用它进行的DNS查找。
是的,如果您not SSH进入docker容器,这是预期的。
Connection to 10.0.0.10 22 port [tcp/ssh] succeeded!
在 VM 中可见,因为您已通过 SSH 将 VM 作为 ssh [email protected]
并且端口 22
在 VM 中用于 SSH。
但是,当您在 docker 容器内时(使用
docker run
或 docker exec
或 docker attach
),端口 22
将 not 被使用,因此预计在 docker 内部会出现来自 nc
的以下错误容器:
10.0.0.10: inverse host lookup failed: Unknown host
(UNKNOWN) [10.0.0.10] 22 (ssh) open
以下是成功测试端口
80
是否在 nc
docker 容器内使用 nginx
的步骤:
$ sudo docker run --name docker-nginx -d -p 80:80 nginx
$ sudo docker exec -it docker-nginx /bin/bash
root@60ec582e90f4:/# apt-get -y update
root@60ec582e90f4:/# apt-get -y upgrade
root@60ec582e90f4:/# apt-get install -y net-tools
root@60ec582e90f4:/# apt-get install -y netcat
# make sure that port 80 is used
root@60ec582e90f4:/# netstat -pan | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1/nginx: master pro
# nc will work now inside the nginx container as port 80 is used inside the container
root@60ec582e90f4:/# nc -vz 127.0.0.1 80 -w 4
localhost [127.0.0.1] 80 (?) open
因此,要使
nc -vz a.b.c.d P -w 4
在容器内工作,必须在该容器内的 IP 地址 P
上使用端口 a.b.c.d
。