Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a vhost on my webserver whose sole and unique goal is to return the client IP adress:

petrus@bzn:~$ cat /home/vhosts/
<?php echo $_SERVER['REMOTE_ADDR']; echo "\n" ?>

This helps me troubleshoot networking issues, especially when NAT is involved. As such, I don't always have domain name resolution and this service needs to work even if queried by its IP address.

I'm using it this way:

petrus@hive:~$ echo "GET /" | nc 80

petrus@hive:~$ echo "GET /" | nc 80


However I found that it wasn't working from at least a computer:

petrus@seth:~$ echo "GET /" | nc 80

petrus@seth:~$ echo "GET /" | nc 80

What I checked:

This is not related to ipv6:

petrus@seth:~$ echo "GET /" | nc -4 80

petrus@hive:~$ echo "GET /" | nc 80

netcat version is the same (except platform, i386 vs x64):

petrus@seth:~$ type nc
nc est haché (/bin/nc)
petrus@seth:~$ file /bin/nc
/bin/nc: symbolic link to `/etc/alternatives/nc'
petrus@seth:~$ ls -l /etc/alternatives/nc
lrwxrwxrwx 1 root root 15 2010-06-26 14:01 /etc/alternatives/nc -> /bin/nc.openbsd

petrus@hive:~$ type nc
nc est haché (/bin/nc)
petrus@hive:~$ file /bin/nc
/bin/nc: symbolic link to `/etc/alternatives/nc'
petrus@hive:~$ ls -l /etc/alternatives/nc
lrwxrwxrwx 1 root root 15 2011-05-26 01:23 /etc/alternatives/nc -> /bin/nc.openbsd

It works when used without the pipe:

petrus@seth:~$ nc 80

And the piping works at least with a test service (netcat listening on 1234/tcp and output to stdout)

petrus@bzn:~$ nc -l -p 1234

petrus@seth:~$ echo "GET /" | nc 1234

I don't know if this issue is more related to netcat or Apache, but I'd appreciate any pointers to troubleshoot this issue !

bzn is the server, hive is a working client and seth is the client on which I have the issue.

Edit: It works also with telnet but telnet doesn't allow piping.

2nd edit in answer to MickeyB:

Indeed host headers are not transmitted, but as seen with hive host above, I've configured the default vhost of Apache to behave like vhost:

petrus@bzn:/etc/apache2/sites-available$ cat default
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerAlias 2a01:e1b:1:132:1a0a:a9ff:fec8:f0a9
        DocumentRoot /home/vhosts/

However, it works with curl!

petrus@seth:~$ curl

Output of apache2 -S posted to

But I'd like to know why it doesn't work with netcat...

share|improve this question
What happens if you use telnet instead of netcat? – quanta Nov 25 '11 at 3:05
@quanta: telnet client doesn't seem to allow piping... – petrus Nov 25 '11 at 11:26
telnet 80 and GET / followed by Enter. – quanta Nov 25 '11 at 12:47
@quanta: oh, this works. As netcat btw (see "It works without the pipe"). – petrus Nov 25 '11 at 16:22
You're not actually hitting the vhost - see my answer. – MikeyB Nov 25 '11 at 16:37

3 Answers 3

up vote 2 down vote accepted

In short, its the version of netcat you are running.

I tested the command string on my machines as follows:

Mac OS X Lion:

yvaine:sqlite user$ echo -e "GET /" | nc 80


[root@freebsd82 /usr/ports]# echo -e "GET /" | nc 80


[root@kvm0001 ~]# echo -e "GET /" | nc 80

Debian (version 6)

root@debian:~# echo -e "GET /" | nc 80

Its not until I got to Ubuntu "lucid" (which is what you are apparently are running) is when I got this:

root@ubuntu:~# echo -e "GET /" | nc 80

It appears that Ubuntu defaults to nc.openbsd rather that nc.traditional (which is the default on Debian). Once I use the traditional version, I received the desired output:

root@ubuntu:~# echo -e "GET /" | nc.traditional 80

What is curious that both of your client machines have nc.openbsd, yet they different in behavior. Of course, it could be that one of them either has a different version of nc.openbsd or a different version of Ubuntu altogether. In either that, you can sync your versions of nc.openbsd or switch over to nc.traditional.

share|improve this answer
Yes! You found it: it works with nc.traditional on Ubuntu 10.10, on seth. However, it's working on 11.04 with nc.openbsd on hive... – petrus Nov 25 '11 at 23:26

Works for me :)

michael@challenger:~$ echo -e "GET /\n" | nc 80
<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>

If you are trying to hit the vhost you are doing so incorrectly. You need to send a Host: header to identify the virtual host which you want to use.

The best way of using your 'what is my IP' service would be curl. Hey look, I see the same problem on my end when I do what you're doing:

michael@challenger:~$ echo -e "GET /" | nc 80
michael@challenger:~$ curl


michael@challenger:~$ curl -v
* About to connect() to port 80 (#0)
*   Trying connected
* Connected to ( port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.21.3 (x86_64-pc-linux-gnu) libcurl/7.21.3 OpenSSL/0.9.8o zlib/ libidn/1.18
> Host:
> Accept: */*
< HTTP/1.1 200 OK
< Date: Fri, 25 Nov 2011 22:11:31 GMT
< Server: Apache/2.2.14 (Ubuntu)
< Last-Modified: Wed, 25 Aug 2010 08:11:38 GMT
< ETag: "840d09-b1-48ea16e57f5a6"
< Accept-Ranges: bytes
< Content-Length: 177
< Vary: Accept-Encoding
< Content-Type: text/html
< X-Pad: avoid browser bug
<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
* Connection #0 to host left intact
* Closing connection #0
share|improve this answer
Mmh, it works with curl. But it should work without host headers. See my edit. – petrus Nov 25 '11 at 21:14
You don't have vhosts set up correctly. That's part of the problem. – MikeyB Nov 25 '11 at 21:33
Examining the output of 'httpd -S' should help identify why things aren't going to work as you expect in all cases. You need to fix your vhosts. – MikeyB Nov 25 '11 at 21:39
MikeyB is right. Apache must be able to know from the client where to redirect to the right virtualhost - hence, the host header request. Otherwise, the server will go to the default DocumentRoot. – Rilindo Nov 25 '11 at 21:42
@Rilindo: default DocumentRoot is /home/vhosts/ default and vhost use the same file. Moreover, it works without the host header on hive... – petrus Nov 25 '11 at 21:49

Set a wait time and try again:

$ echo "GET /" | nc -w 3 80
share|improve this answer
good idea, but no change. Still doesn't work – petrus Nov 25 '11 at 21:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.