Set Ubuntu System Proxy Settings without Restart from commandline
I am using
Ubuntu 14.04 . I want to change the
http proxy settings from the command line. This should be equivalent to changing in the GUI(All Settings->Network->Network Proxy) and clicking the button
Apply System Wide. I don’t want to restart/logout the system as I am planning to change the settings dynamically from a script(
From what I understand, setting proxies system-wide via that GUI does three things:
- Set the corresponding values in the dconf database.
- Set the values in
- Set the values in
1 and 3 take effect immediately.
/etc/environment is parsed on login, so you will need to logout and login for that to take effect. (Note that this is login proper, not merely running a login shell.)
The following script should be equivalent (assuming http/https proxies):
#! /bin/bash HTTP_PROXY_HOST=proxy.example.com HTTP_PROXY_PORT=3128 HTTPS_PROXY_HOST=proxy.example.com HTTPS_PROXY_PORT=3128 gsettings set org.gnome.system.proxy mode manual gsettings set org.gnome.system.proxy.http host "$HTTP_PROXY_HOST" gsettings set org.gnome.system.proxy.http port "$HTTP_PROXY_PORT" gsettings set org.gnome.system.proxy.https host "$HTTPS_PROXY_HOST" gsettings set org.gnome.system.proxy.https port "$HTTPS_PROXY_PORT" sudo sed -i.bak '/http[s]::proxy/Id' /etc/apt/apt.conf sudo tee -a /etc/apt/apt.conf <<EOF Acquire::http::proxy "http://$HTTP_PROXY_HOST:$HTTP_PROXY_PORT/"; Acquire::https::proxy "http://$HTTPS_PROXY_HOST:$HTTPS_PROXY_PORT/"; EOF sudo sed -i.bak '/http[s]_proxy/Id' /etc/environment sudo tee -a /etc/environment <<EOF http_proxy="http://$HTTP_PROXY_HOST:$HTTP_PROXY_PORT/" https_proxy="http://$HTTPS_PROXY_HOST:$HTTPS_PROXY_PORT/" EOF
Even though it requires a re-login for PAM to apply
/etc/environment everywhere, in a current shell you can still extract the values in that file:
export http_proxy=$(pam_getenv http_proxy)
I made a tool, ProxyMan, to simplify the entire task. You can download it from this link.
Also, you can have a look at the code if you are more interested to know the backend functioning. Download the zip file,extract them, go to the location of extracted files in terminal and following commands would help you:
- bash main.sh: to set and unset proxy.
- bash proxy_check.sh: to check your current proxy settings.
I think this should avoid headaches:
An excelent tutorial, running in python.
Setting Simple and Authenticated Proxy Settings in Ubuntu 12.04/12.10/13.04/13.10/14.04/14.10 and above 100% Working.
The system must be installed “python ” .
With the interpreter running the command :
Command: “sudo python setproxy.py [ Proxy_Server ] [ proxy_port ] [ PROXY_USER ] [ proxy_password ]”
[ Proxy_Server ] : proxy.test.ts [ Proxy_port ] : 8080 [ PROXY_USER ] : domainuser [ Proxy_password ] : " the_password "
"Sudo apt- get install python- support" "Sudo apt- get install ntlmaps "
Fill in the fields as appropriate:
- Proxy server - Proxy port - User - Password