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(bash
).
3 Answers
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
/etc/environment
. - Set the values in
/etc/apt/apt.conf
.
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)
-
@ma08 What's the output of
gsettings get org.gnome.system.proxy mode; gsettings get org.gnome.system.proxy.http host; gsettings get org.gnome.system.proxy.http port;
?– muruAug 26, 2014 at 20:15 -
Now I get the problem, when I run the script as normal user, the warning appears
(process:9662): dconf-WARNING **: failed to commit changes to dconf: Could not connect: Connection refused
for each command in gsettings. If I run it as root, it works fine but the the output of the above commands(get) are the changed ones only in the root but not in the normal user.– ma08Aug 26, 2014 at 20:21 -
Aaarghh.. The damn script is littered with bugs. That's three I have had to fix. :/ In the second
sudo tee
, note that the file name is/etc/environment
and not/etc/apt/apt.conf
. You might have to delete the garbage entries in apt.conf.– muruAug 26, 2014 at 20:27 -
-
2Thank you, your script works and that answer in the link helped to get over that error. I should have done the debugging myself. Thanks a lot.– ma08Aug 26, 2014 at 20:39
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.
-
1
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.
Solution 1 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 ]"
Video: https://www.youtube.com/watch?v=eBtzKa-dvJg
[ Proxy_Server ] : proxy.test.ts
[ Proxy_port ] : 8080
[ PROXY_USER ] : domainuser
[ Proxy_password ] : " the_password "
Solution 2: commands:
"Sudo apt- get install python- support"
"Sudo apt- get install ntlmaps "
Fill in the fields as appropriate:
- Proxy server
- Proxy port
- User
- Password
-
2To make this into a good answer, you should at the very least outline the steps. Sep 15, 2016 at 22:04
sudo service network manager restart
.