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

This is somehow related to Change interface for doing a whois

The differece is that I'd like to know if it's possible to specify the default interface only for a specific program - not system-wide. For example: let's say my server has several IPs. I want to run program X (say wget) and I want it to use one of my IPs as the source address. It would look like this:

theprogram --use-interface= wget

and wget would use my IP to connect to and fetch the html. Is it possible?

share|improve this question
I'm aware that some programs (like wget) allow specifing source addresses - but I'm looking for a general solution. There is a program called trickle which works in a way descibed in my question and lets you limit bandwith used by another program: I thought something like that could exist for bind/source addresses. – wanson Jun 17 '11 at 23:26
up vote 2 down vote accepted

Folks here have some suggested some workable alternatives for what you want to do. Routing and source NAT can help out, but may or may not solve the problem you're trying to solve.

You can do exactly what you've asked if you're willing to much about a little. You can use the magic of function interposition via LD_PRELOAD to wrap the bind() system call with code of your choice.

The follow pages have information that provides more details about this solution:

This will only work as long as the code you're running is not SUID (for security reasons SUID/SGID programs cannot be modified using LD_PRELOAD).

share|improve this answer
On Daniel Lange's blog (…) I found the bind.c program which does what I want. Thanks for pointing me there! – wanson Jun 18 '11 at 14:43

With wget you can use --bind-address

Otherwise I think you either need to specify a route for the destination IP address, or look in the program you're trying to use if it has a way of deciding the bind/source address.

share|improve this answer

Many, but by no means all, programs have the option to bind to a specific address. Some, like wget (as pointed out by fbh) have a command-line option to specify which address to bind to. Many more server-like programs, such as apache and squid, control this in their configuration file. It's always going to be program-specific.

For programs that don't support binding to a specific address, your options may be more limited, depending on what sort of program you're dealing with.

For a server program (that accepts incoming connections), you can usually accomplish the same thing by listening on all addresses (which is usually the default), then using iptables, or your external router, to forewall the other addresses.

The trickier thing is for outbound connections. Here you'll need something more like described in the answer you linked to in your question. The problem with that is its dependent on the destination address, not the source program.

You may be able to accomplish what you want with iptables and a SNAT rule.

iptables -A POSTROUTING -p tcp --dport 80 -j SNAT --to-source

This iptables rule (which I have not tested), ought to change the source address on any traffic going to port 80. If you need the same behavior for both tcp and udp traffic, you'd need to create two rules, replacing '-p tcp' with '-p udp' in the second one.

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.