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

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

For example: Firefox uses random local port numbers to connect to websites. (Dozens of different local ports can be used at any given time.) Is it possible to force a program to only use specific local ports ? (Force only the local port, the target port could be any.)

share|improve this question
Is it a program that you are writing or an existing program? I don't think you can do this without changing the source code of the program. Also, why do you want to do this? Keep in mind that the reason that TCP/IP clients use random local ports is so that they don't have to deal with a port being busy. If you assign the program to only use a specific port, only one process will be able to make a request at one time. – Zhehao Mao Jun 22 '11 at 15:45
Why? <extrachars> – Shane Madden Jun 22 '11 at 15:45
@Shane: Web-browsers try to concurrently fetch multiple resources (images, CSS, JS etc) for a page. To do this they need multiple connections, each TCP connection to the same server (& to port 80 say) needs a different source port otherwise the individual connections cannot be distinguished. – RedGrittyBrick Jun 22 '11 at 16:02
@RedGrittyBrick Sorry - didn't make that clear, but my "why" was in response to the original post, not @Zhehao's comment. – Shane Madden Jun 22 '11 at 16:23
@Shane: Ahh - I misunderstood, sorry. – RedGrittyBrick Jun 22 '11 at 16:26

I don't know of any way to do this short of modifying the source code, and even then, Firefox may be calling closed netcode on Windows (ie, it might be using Visual C++ libraries).

I'm confused why you'd want to do this. Most firewalls have different rules for outgoing and incoming connections, and limiting outgoing ports is quite unusual as they're listening only for traffic from a specific TCP session. Destination ports are much more security sensitive as they are open and listening with no established sessions.

share|improve this answer

Write a library that intercepts and rewrites the bind(2) call, and use $LD_PRELOAD to load it underneath the application of choice.

share|improve this answer

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.