2

I am using tsocks to socksify my connections. When I run the following commands, apt-get isn't socksified:

$ . tsocks -on
$ sudo apt-get update

However, when I run the whole thing in one line like the following, it works.

sudo tsocks apt-get update

I have verified that when I type . tsocks -on, my LD_PRELOAD environment variable is set to /usr/lib/libtsocks.so.

Why doesn't the former work?

3 Answers 3

3

Although it's already given an answer I want to share what worked for me, using Ubuntu 14.04:

sudo apt-get install tsocks

Then open /etc/tsocks.conf and add your server to server and port to server_port, also add your socks server's range as local, like

Example if your ip address is 90.70.60.5 then write:

local = 90.70.60.0/255.255.255.0 (Note the .0 as the last part in ip address)

Now try:

sudo tsocks apt-get update (or whatever apt-get command)

If it doesn't work using sudo, log in directly as root like:

sudo su

Then try again:

tsocks apt-get update

I was behind a socks 5 server to reach a download link I didn't have access from my own computer, and it worked flawlessly.

Note: With this I didn't have to use "Acquire" stuff in /etc/apt/apt.conf

1

Per the answer given by @creveti-mihai, I looked at the sudoers man page. Unfortunately, as the man page says, it is not possible to preserve LD_* variables. Here is what the man page says:

 Note that the dynamic linker on most operating systems will remove
 variables that can control dynamic linking from the environment of setuid
 executables, including sudo.  Depending on the operating system this may
 include _RLD*, DYLD_*, LD_*, LDR_*, LIBPATH, SHLIB_PATH, and others.
 These type of variables are removed from the environment before sudo even
 begins execution and, as such, it is not possible for sudo to preserve
 them.

So, I'll have to look at specifying the proxy in apt's own configuration files using Acquire::http::proxy and Acquire::https::proxy.

0

Is the root's LD_PRELOAD also set to /usr/lib/libtsocks.so? Take a look at:

  • The -E (preserve environment) option for sudo. Ex: sudo -E bash -c 'echo $LD_PRELOAD' vs sudo echo $LD_PRELOAD;
  • The sudoers file: Defaults env_keep;
3
  • When I run the first command in your first option, I get nothing. However, running the second command in the first option gives me the output. I've also done sudo -i and then echo $LD_PRELOAD, and I've gotten nothing. So I assume the variable isn't being set when I run . tsocks -on. How can I make it be set though? Commented Nov 8, 2014 at 15:54
  • That is correct. Take a look at the manpage for sudo (look at the -E flag to preserve the environment) or the manpage for sudoers at env_keep). Commented Nov 8, 2014 at 15:58
  • Sorry, I'm not quite sure how I should do this. I've added Defaults env_keep += "LD_PRELOAD" to both /etc/sudoers.d/vagrant (the user with which I am sudo-ing) and /etc/sudoers, just beneath Defaults env_reset, but the first command in your first option, and also sudo printenv LD_PRELOAD still return nothing. Commented Nov 8, 2014 at 16:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .