1

I want to set the proxy for apt-get and the global proxy in xubuntu like here but with only one command. How would it be? What's the problem with this code? I save it in ~/.functions add the line . ~/.functions to the .bashrc file and when I reload the .bashrc file gives an error about EOF in line 7.

Correct code:

myproxy="http://proxy.server:port/"
proxyhost="proxy.server"
myport=port

# Set Proxy
function setproxy() {
    sudo tee -a /etc/environment << EOF
    http_proxy="$myproxy"
    https_proxy="$myproxy"
    ftp_proxy="$myproxy"
    no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
    HTTP_PROXY="$myproxy"
    HTTPS_PROXY="$myproxy"
    FTP_PROXY="$myproxy"
    NO_PROXY="localhost,127.0.0.1,localaddress,.localdomain.com"
EOF

    gsettings set org.gnome.system.proxy mode manual
    gsettings set org.gnome.system.proxy.http host "$proxyhost"
    gsettings set org.gnome.system.proxy.http port "$myport"
    gsettings set org.gnome.system.proxy.https host "$proxyhost"
    gsettings set org.gnome.system.proxy.https port "$myport"

    sudo tee /etc/apt/apt.conf.d/95proxies << EOF
    Acquire::http::proxy "http://$proxyhost:$myport/";
    Acquire::ftp::proxy "ftp://$proxyhost:$myport/";
    Acquire::https::proxy "https://$proxyhost:$myport/";
EOF
}

#Unset Proxy
function unsetproxy() {
    sudo rm /etc/environment
    sudo tee /etc/environment << EOF
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
EOF

    gsettings set org.gnome.system.proxy mode none

    sudo rm /etc/apt/apt.conf.d/95proxies
}
  • If you want a script: http://askubuntu.com/a/526900/158442 – muru Feb 11 '15 at 12:11
  • do firefox need gsettings? Do I have to configure firefox to see the system proxy? –  Feb 11 '15 at 12:13
  • If you configure it to use system proxy, then the gsettings command will affect it. – muru Feb 11 '15 at 12:13
  • I saved it in ~/.functions added . .functions to .bashrc and did . .bashrc to reload but it gives an error regarding the function. How would be the unsetproxy function? –  Feb 11 '15 at 12:31
  • To undo gsettings just use none instead of manual. – muru Feb 11 '15 at 12:40
  • Your gsettings command are using the wrong quotes. Single quotes (') prevent variable expansion. And the EOF must be the first thing on the line - no indentation. – muru Feb 11 '15 at 13:31

2 Answers2

0

You can not set a global environmental variable like http_proxy without at least restarting the shell. You can do one of three things:

  1. Set variable for bash session and run your internet application from bash. http_proxy=8.8.8.8 ftp_proxy=8.8.8.8 firefox
  2. Set variable in DE, if it supports, and start application in DE.
  3. Set variable for user or system-wide and restart bash or relogin. http_proxy=8.8.8.8 ftp_proxy=8.8.8.8 bash firefox exit

Any one can be accomplished by a script, that is started as one command, but clarify first, what do you need?

  • I'm trying to run the first command but it doesn't work. http_proxy="http://wifi.uma.es:3128/" ftp_proxy="http://wifi.uma.es:3128/" firefox –  Feb 11 '15 at 12:11
  • If it really is firefox, then in preferences - advanced - network the proxy must be set as "Use system settings", otherwise settings there will override environmental variables. – Barafu Albino Feb 11 '15 at 12:21
0

When you use heredocs, the heredoc limit string (EOF here) at the end should be by itself on that line - nothing else, no spaces, indentation, etc. Therefore your code should look like this:

    sudo tee /etc/apt/apt.conf.d/95proxies << EOF
    Acquire::http::proxy "http://$proxyhost:$myport/";
    Acquire::ftp::proxy "ftp://$proxyhost:$myport/";
    Acquire::https::proxy "https://$proxyhost:$myport/";
EOF
muru
  • 197,895
  • 55
  • 485
  • 740