proxy all network communication in bash session

Is it possible to specify a proxy for all network communication in a bash session? I suspect it is possible to do it using ssh but I'd rather not use it if there's a better way.


Solution 1:

In Linux there is proxychains and proxychains-ng. I believe the latter is called proxychains4 in Ubuntu. The usage it like this:

proxychains4 some_program     # see the manual to learn about config files

or

proxychains4 -f configfile.conf some_program

Is it possible to specify a proxy for all network communication in a bash session?

Basically yes.

proxychains4 bash

The tool works by using LD_PRELOAD variable to preload its shared object which does the real job. First of all the bash itself is proxified (usually you don't expect your shell to initiate network connections, but Bash does this for special redirections like foo > /dev/tcp/host/port, they will be proxified). The variable is in the environment so any command invoked in this Bash session will be affected.

# from within our proxified Bash
wget -O /dev/null superuser.com     # also proxified

However

  • if our proxified Bash (or any proxified command) spawns a child whose LD_PRELOAD doesn't point to the right object, the child won't be proxified, e.g.:

    # from within our proxified Bash
    LD_PRELOAD="" wget -O /dev/null superuser.com     # not proxified
    
  • if our proxified Bash (or any proxified command) execs to a new process (possibly a new instance of itself) with LD_PRELOAD not pointing to the right object, the resulting process won't be proxified, e.g.:

    # from within our proxified Bash
    unset LD_PRELOAD
    exec bash
    # now we are in non-proxified Bash, it replaced the proxified one
    wget -O /dev/null superuser.com     # not proxified