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.

  • 1
    Potentially a dupe of askubuntu.com/questions/664777/… - 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

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', '', '${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

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


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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.