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(bash).

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

From what I understand, setting proxies system-wide via that GUI does three things:

  1. Set the corresponding values in the dconf database.
  2. Set the values in /etc/environment.
  3. 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)

Method 2

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.

Method 3

I think this should avoid headaches:

Proxy Settings Video

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments