How can I configure netcat (or some other stock linux utility) to listen on a specific port on a secondary IP address?

CentOS 5.9

For testing purposes, I want my CentOS server to listen on a secondary virtual IP (eth0:0). I'm familiar with nc -l -p <port> but it only listens on the primary. Is there a way I can specify a specific IP for the listener to use?

If not, is there another "stock" utility in CentOS 5.9 that can do this?


The syntax depends on the netcat package.

netcat-openbsd

nc -l 192.168.2.1 3000

netcat-traditional

nc -l -p 3000 -s 192.168.2.1

A simple way (at least in bash) for telling them apart in scripts is:

if ldd $(type -P nc) | grep -q libbsd; then
    nc -l 192.168.2.1 3000
else
    nc -l -p 3000 -s 192.168.2.1
fi

For completion:

nc -l -p port -s ip

should work too. Works with nc6 version 1.0 and netcat (The GNU Netcat) 0.7.1.

The command from Laging doesn't work with nc6 (used in debian).