Super User is a question and answer site for computer enthusiasts and power users. 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

I have a Primary IP set for all the system related tasks. Sometimes I have to use my system to carry out different tests and most of the time I have to deal with bash/shell scripts. I want the tests/bandwidth used to be completely apart from the Primary IP so I want to carry out the operations using the Secondary IP. How can I achieve this?

share|improve this question
That depends on the commands your script is invoking. – Oliver Salzburg Oct 14 '13 at 17:43
It doesn't actually use any commands as one of the script is network related and keeps track of data, scans a list of servers off a master IP and updates data. Pure programming I can say. I have tried using "bind-address" commands which you might be referring to but they are command related, true. – Asad Moeen Oct 14 '13 at 17:45
up vote 2 down vote accepted

This is not much about bash and not even so much about the commands you use in the script. Typically, the source IP is chosen according to the target IP. Some tools may let you specify the source IP or you can possibly rewrite the source IP using iptables if you can match the script e.g. by uid/gid.

AFAIK there's no way to attach a source IP addresses to a processes (and you would need also the subprocesses to inherit it). If you are ok with the source IP rewriting, you can switch the script process to a special group id:

As root:

ip4_address=... # fill in your ipv4 address
group=secondaryip # or any other name

groupadd $group
usermod your-user -aG $group # not needed for the root user

iptables -t nat -A POSTROUTING \
    -m owner --gid-owner $group \
    -j SNAT --to-source $ip4_address

Run the script using:

sg secondaryip "/path/to/your/script arg1 arg2"

Alternatively you can invoke individual commands inside your scripts using sg. Read its manual page for more details. Additionally there are tricks to make your script run itself with sg command but you shouldn't need that much. You can use the classic newgrp command and pass it a shell script via stdin but that won't work for perl (mentioned by your comment).

Note: The commands used in the script won't know about the modified source IP but that shouldn't generally be a problem.

For the sake of completeness, there's yet another way. You could use the LD_PRELOAD environment variable to replace some libc symbols, e.g. connect() with your own version that would perform additional actions such as binding to a specific source address. But it's a bit advanced C programming and you'd better stick with conventional waepons :).

share|improve this answer
And how may we change the source IP based on that just for a specific script? – Asad Moeen Oct 15 '13 at 7:49
@AsadMoeen Let me know if the new information in the answer works for you. I'll update it if there's any problem. – Pavel Šimerda Oct 15 '13 at 8:05
By the way, the script does not respond when the first two lines are this: "newgrp secondaryip #!/usr/bin/perl" – Asad Moeen Oct 16 '13 at 15:08
True enough, see updated answer. – Pavel Šimerda Oct 16 '13 at 17:50
Thank you. But the "sg" command does not allow parameters to be passed to the script. – Asad Moeen Oct 17 '13 at 10:55

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.