Linux – Force program to use only specific local port

linuxnetworkingport

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

Best Answer

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.