25

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?

Mike B
  • 11,570
  • 42
  • 106
  • 165

2 Answers2

40

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
Hauke Laging
  • 5,157
  • 2
  • 23
  • 40
9

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).

schlicht
  • 191
  • 1
  • 2
  • 2
    The only way I can get to specify the listening interface on Ubuntu's `nc`. – djule5 Aug 05 '16 at 15:33
  • 2
    nc says that you cannot use both -s and -l together – SunSparc Oct 11 '16 at 22:42
  • 1
    @SunSparc yet it is the only way to go for me to specify both local ip and local port in server mode - no other combination of options works here (nc v1.10-38, debian) – Antony Hatchkins Mar 22 '17 at 18:01
  • I think the issue could be that different netcat versions behave differently. I can't seem to get a version string out of mine, but there are at least two packages netcat-traditional vs netcat-openbsd. `netcat -vvlp 4445 -s 172.17.0.1` gives me `listening on [172.17.0.1] 4445 ...` vs `netcat -vvl 172.17.0.1 4445` gives me `listening on [any] 36539 ...` – relet Oct 03 '17 at 08:30
  • I had to specify the -s flag on debian buster. – Karl Pokus Feb 07 '20 at 07:33