Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to make iptables redirect internally from one port to another only if a service is listening to that port. Is it possible?

share|improve this question

AFAIK IPtables doesn't have enough information to do that effectively. You would probably need to write a script that uses netcat or data from netstat to decide if the port is listening, and if so do the redirect. Run that script from cron.

share|improve this answer
You think a pid or a lock file would help? Maybe using conntrack or state matching modules? – omgirkal May 28 '11 at 21:29
Don't force yourself to use iptables. You can check with a if command from netstat, lsof, ... result and do the redirect. – quanta May 29 '11 at 2:04

Sure, you can do it:

iptables -t nat -A PREROUTING -i ethx -p tcp --dport $srcPort -j REDIRECT --to-port $dstPort

share|improve this answer
That's what I already have, but that doesn't make sure a service is listening or that the port is on a certain state. – omgirkal May 28 '11 at 19:24

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.