Why node nc (netcat) give error "inverse host lookup failed: Unknown host"?
Asked Answered
S

3

27

nc works fine when i execute it on a VM.

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

But when I execute the same command inside my docker container, it gives the below UNKNOWN error/exception.

10.0.0.10: inverse host lookup failed: Unknown host 
(UNKNOWN) [10.0.0.10] 22 (ssh) open

Below is the nc command that I am using:

nc -vz 10.0.0.10 22 -w 4
Slipper answered 14/2, 2018 at 4:16 Comment(0)
S
31

"Inverse host lookup failed" simply means that nc wanted to print which host name 10.0.0.10 corresponds to, but couldn't.

UNKNOWN is simply what it then prints as the host name.

This is distinct from "I looked it up, but it doesn't seem to correspond to anything" which is what happens outside the container.

To be perfectly explicit, connecting to the host succeeded, but looking up its name from the IP address failed. This is just an informational warning message, not a hard error; the lookup is entirely optional, anyway, and can be disabled with -n.

If you really want to avoid this warning and not switch to -n, you need to set up working DNS inside the container.

Sungkiang answered 14/2, 2018 at 5:25 Comment(4)
so, with this exception "inverse host lookup failed: Unknown host", can we assume that connection is succeeded..!? Because my intention is to do SSH test.Slipper
The reverse lookup is purely for human friendly output, and has no bearing on whether or not the requested operation is successful (unless you happen to be testing for working DNS).Sungkiang
@Slipper so , in the end, does this "open" part mean that the connection is succeeded? Because this is confusing me too...Icono
@Icono See updated answer now. The connection succeeded.Sungkiang
P
11

Simply provide the -n option on both sides of listener and client to remove this error as it will neglect the DNS look up by using it.

Pleuron answered 11/5, 2018 at 7:19 Comment(0)
R
3

Yes, this is expected if you have not SSH'ed into the docker container.

Connection to 10.0.0.10 22 port [tcp/ssh] succeeded! is seen in the VM because you have SSH'ed into the VM as ssh [email protected] and port 22 is used in the VM for SSH.

But, when you are inside a docker container (using docker run or docker exec or docker attach), port 22 will not be used, and hence the following error from nc is expected inside the docker container:

10.0.0.10: inverse host lookup failed: Unknown host 
(UNKNOWN) [10.0.0.10] 22 (ssh) open

Here are the steps to successfully test if port 80 is used using nc inside an nginx docker container:

$ 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

Hence, for nc -vz a.b.c.d P -w 4 to work inside a container, port P must be used on IP address a.b.c.d inside that container.

Rosel answered 14/2, 2018 at 5:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.