19

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?

35

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
8
  • This is exactly what the man page says. – Michael Hampton May 31 '13 at 16:36
  • @MichaelHampton I am not sure whether I understand correctly what you mean. – Hauke Laging May 31 '13 at 16:39
  • 1
    I think in netcat-traditional you can do -l -p – user May 31 '13 at 16:39
  • 1
    @MichaelHampton I guess I will take quite a while yet until I understand the practice rules of upvoting. If I have a look at what I get upvotes for (and much worse: What not for!) then I could cry. But it looks like this big problem's solution kicks me above the desired 3,000 points... (I noticed that on Unix & Linux my interest in rep dropped a lot after that) – Hauke Laging May 31 '13 at 16:43
  • 1
    @SAMPro The answer was not incorrect but incomplete. – Hauke Laging May 13 '20 at 20:11
8

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

5
  • 2
    The only way I can get to specify the listening interface on Ubuntu's nc. – djule5 Aug 5 '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 3 '17 at 8:30
  • I had to specify the -s flag on debian buster. – Karl Pokus Feb 7 '20 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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