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
  • 1,067
  • 2
  • 11
  • 18

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
  • 1,067
  • 2
  • 11
  • 18