6

I'm using socks proxy over ssh tunnel

ssh -D 1080 -f -C -q -N <user>@<server>

when I set the proxy in System settings > Network > Network proxy, all applications like Chromium, Firefox, apt, ... are following the proxy.

enter image description here

now I want to write a shell script for automation, how can I set Network proxy by terminal?

all I found is "use some third-party software like Tsocks and ...". but as I said, the ubuntu it self can do this without any third-party software so don't want to use them.

all I want is set Network proxy using a command in terminal.

M.A. Heshmat Khah
  • 867
  • 2
  • 9
  • 17
  • 1
    Potentially a dupe of http://askubuntu.com/questions/664777/systemwide-proxy-settings-in-ubuntu - check the accepted answer's 3rd point about `/etc/environment`, but using localhost instead eg `http_proxy=http://localhost:1080` – taifwa Apr 11 '17 at 13:04

1 Answers1

5

as @taifwa said, Systemwide proxy settings in ubuntu was the solution.

and here is my script:

    ssh -D ${LOCAL_PORT} -f -C -q -N ${REMOTE_USER}@${REMOTE_HOST} -p ${REMOTE_PORT}

    #set socks setting in System settings > Network > network proxy 
    gsettings set org.gnome.system.proxy mode 'manual'
    gsettings set org.gnome.system.proxy.socks port ${LOCAL_PORT}
    gsettings set org.gnome.system.proxy.socks host 'localhost'
    gsettings set org.gnome.system.proxy ignore-hosts "['localhost', '127.0.0.0/8', '${LOCAL_RANGE}', '::1']"

    sudo su <<-EOF  
    #environment settings
    echo "socks_proxy='socks://localhost:${LOCAL_PORT}/'" >> /etc/environment 
    #apt settings
    echo "Acquire::socks::proxy 'socks://localhost:$LOCAL_PORT/';" >> /etc/apt/apt.conf
    EOF

run as root is required for editing environment and apt.conf,

edit:


but it's important to know editing org.gnome.system.proxy as root has no effect on current user, so them should run as normal user. so don't run the script with sudo
M.A. Heshmat Khah
  • 867
  • 2
  • 9
  • 17
  • 1
    I know that this space isn't for saying thanks but you saved my whole day. I wish that I found this sooner. – Raza Mar 04 '19 at 09:24
  • Does this script only work for Gnome, not for Xfce? – sequence Oct 14 '21 at 12:32
  • I don't this test on Xface. – M.A. Heshmat Khah Oct 14 '21 at 15:52
  • Using gsettings to set socks results in generating environmental variables 'ALL_PROXY' and 'all_proxy' as 'socks://xxx:yyy', which neglects the VERSION of socks. Do you know how to make gsettings generate these environmental variables as 'socks5://xxx:yyy' instead? This is important to me because many apps can't detect the version of socks automatically, including pip. – cyfex May 09 '23 at 13:26