Ubuntu has one irritating issue that has been a pain in my side for YEARS. When you set a system-wide proxy to let all your programs use a proxy on the network and set a DIFFERENT proxy, or select the “No Proxy” or “Default” option, the first system-wide proxy will persist.

One evening after a Lets Talk Geek podcast I complained about this along with Tim Haak. Stew Allen, another of the members from the podcast suggested we look at Privoxy as an option, setting one system proxy and then changing the forwarding rules as you move between proxies.

Being a lazy geek, I searched for a way to automate this…

That Ubuntu Bug

A quick browse through launchpad lists this issue several times, one bug report and discussion can be seen here. (Yes I know it approaches the issue from a perceived issue with Synaptic)

Very simply put, when you set a system-wide proxy, it will not revert to “no proxy” until you reboot, or manually set the proxy from the command-line. This… is unacceptable.

In my line of work I often move between various networks that have different proxy settings. Having to reboot every time is a pain in the rear. This also means that I have to restart when I get home.


Enter Privoxy

Allow me to quote from the Privoxy website:

Privoxy is a non-caching web proxy with advanced filtering capabilities for enhancing privacy, modifying web page data and HTTP headers, controlling access, and removing ads and other obnoxious Internet junk. Privoxy has a flexible configuration and can be customized to suit individual needs and tastes. It has application for both stand-alone systems and multi-user networks.

For the purposes of my problem I was interested in Privoxy working for “stand-alone systems.” In effect you can install the privoxy package on your Linux machine and use “localhost:8118″ for your proxy server.

From there you can tell privoxy to forward proxy requests to another server on your network.

This means that you can set ONE system proxy, and from there on change the forward address in the privoxy config file and you will never need to deal with Ubuntu not changing your global proxy settings.

Enter My Laziness

One hassle with this though is that you need to manually change the pertinent line in the privoxy config file and restart the privoxy from the command-line.

That is kinda the same as with the regular Ubuntu proxy problems.

I decided that I wanted to have the proxy auto configure from a bash script whenever I connect to a wireless network (I rarely use a wired network) and then reload privoxy.

Here is a step by step how I went about it.

Some Notes on the Bash Script

As with all my scripts it is uuug leeee. Some of the settings like the username near the end (we’ll get to that later) needs to be set to your username. I am too lazy to build something to detect the active users and run the command as the correct su $USER.

Maybe in my next update I’ll fiddle with that part.

Also, you need to create your custom privoxy config files by hand, and name them accordingly in the script. The script will fail if some of the files are not there.

In any case, I can guarantee that the script will fail if you run it as is, you will need to change some things in order for it to work – I will add comments in the script to highlight where you need to input your own information, as well as in the step by step run through below.

Right. On to the script.

Bash Script To Automatically Change and Reload Privoxy Config When Connecting To Networks *wheeze*

Right, first up I tell my script to run under bash shell.

#!/bin/bash -x

The -x is to give you output to your terminal to see where your script fails.

Next up you need to get the current SSID of your wireless network:

NETWORK=`iwconfig wlan0 | grep ESSID | awk -F “\”" ‘{printiwconfig wlan0 | grep ESSID | awk -F “\”" ‘{print $2}’ $2}’`

This should spit out your SSID in plain text. You need to make sure that the SSID names in the rest of the script match your SSID’s exactly, otherwise the script will not do what you want it to do. See below: [UPDATE]I amended my if loop to compensate for the script running twice.[/UPDATE]

if [ "$NETWORK" = " " ]; then
elif [ "$NETWORK" = "mySSID1" ]; then
cp /etc/privoxy/mySSID1 /etc/privoxy/config
elif [ "$NETWORK" = "mySSID2" ]; then
cp /etc/privoxy/mySSID2 /etc/privoxy/config
elif [ "$NETWORK" = "mySSID3" ]; then
cp /etc/privoxy/mySSID3 /etc/privoxy/config
elif [ "$NETWORK" = "mySSID4" ]; then
cp /etc/privoxy/mySSID4 /etc/privoxy/config
else cp /etc/privoxy/default /etc/privoxy/config


Note the “mySSID1″ etc above. In here you need to type your expected network SSID’s exactly like you would see it if you ran iwconfig wlan0 | grep ESSID | awk -F “\”" ‘{print $2}’

in a terminal.

The other text files in /etc/privoxy I named the same as the SSID’s for convenience sake. I also entered the full path to the files because the script will not reside in the /privoxy directory.

Next up, loading your privoxy config by restarting privoxy.

RESTARTPROXY=`/etc/init.d/privoxy restart > /etc/privoxy/proxystatus`

note that I pipe the output from that command to a textfile. The reason for that will become clear later, also why I have a RESTARTPROXY variable here.

Following that I need to make sure that my correct proxy is loaded, so another variable and another command:

GETPROXYSTATUS=`cat /etc/privoxy/proxystatus`

Now the fun part of my script – I want to output what is happening to the proxy server to a popup on my desktop. After my previous fun with notify-send I decided that it would be the perfect tool for the job.

I ran into an issue with actually displaying the notification though. The script runs as root, which is good because config files are manipulated for which I needed root privileges. Root, unfortunately, does not output to my display, which is bad. Notify-send needs a display to output to in order to pop the pretty little bubble that says the proxy is restarted.

I worked around this with su. After struggling with various solutions online I realized the easiest solution would be to run notify-send as me, and then telling it to output to my screen. See below:


Notice the plethora of backslashes. Notify-send is very finnicky with how the text is presented to it, and I have to enclose the command after ’su’ in quotes otherwise it won’t run.

Also note the YOURUSERNAME. This is you. Enter your username here. This is the part where I can probably work around this with who and grep + awk the username from there. In fact, looking at the output given by who I bet I can probably make notify-send present it’s output on multiple screens to multiple logged in users. Hmmm…

Something like

who | awk -F “tty” ‘{print $1}’ | grep -m 1 -v ‘(:’

should give you the username. Some more fiddling with that command and ‘uniq’ and you can grep the display numbers and run notify send for multiple users on their screens…

Anyhow, on to the final part of the script…

WHICH=`grep -A3 “443 (whi” /etc/privoxy/config | grep forward | awk -F forward ‘{print $2}’`

This part greps the currently used proxy from the privoxy config file and then:

su YOURUSERNAME -c “env DISPLAY=:0.0 notify-send \”PROXY NOW IN USE\” \”$WHICH\”"

That last part lets notify-send display the currently forwarded proxy so that I can take note and feel superior.

How To Let the script run when you change networks

In Ubuntu, Linux Mint and other *buntu based distros you can plonk the script in /etc/NetworkManager/dispatcher.d/

Everything in there gets run by the system whenever Networkmanager does something with your network.

Ons some other linux distros you might need to put it in /etc/network/if-up.d or so on.

Making Custom Privoxy Configs

Now all of this is going to be useless if you do not use custom privoxy configs. My needs are simple, so I will share just the forwarding part with you. The rest is up to your needs and curiosity. Before you edit the config, run “sudo cp /etc/privoxy/config /etc/privoxy/default”

That will allow your script to load the default privoxy config in case an SSID does not match one in your file.

In the privoxy config search for the line ‘#forward’ above this is a comments section in the privoxy config that explains how to set up forwarding. In my case I needed to forward everything in to our proxy server, including :443 (or SSL) traffic.

I amended these lines to read:

forward /
forward :443

Note that the lines are no longer # out.

Save this file then to /etc/privoxy/msSSID (where “mySSID” is the SSID of the network where the config applies.)

Some Issues That Need To Be Addressed

Right now the script runs twice when I change networks.[NOTE]This has been corrected with the update noted above.[/NOTE] This means that every time there are four popups, and they tellme: privoxy reloaded, proxy set to NULL, privoxy reloaded, proxy set to myproxydetails.

I think this is because there are two actions when network manager changes networks – disconnect, and then reconnect – and that the scripts inside /etc/NetworkManager/dispatcher.d/ get executed for each action. [UPDATE]Yes that is, and once the variable for $NETWORK is ” ” and this causes the proxy to be set to null. Line added to the script to fix this.[/UPDATE]

Secondly, I think I might combine the two notify-send events into one. It is a bit irritating to have four popups that all follow each other with ten second timeouts telling me the info twice.

Complete Script

Below is the complete script, ready for you to customize to your particular needs. Remember to make it executable with “chmod a+x proxy” (“proxy” is the name of my script, you can name it whatever youwant)

#!/bin/bash -x

#Script to view proxy information

#Get Wireless Information for the loop below
NETWORK=`iwconfig wlan0 | grep ESSID | awk -F “\”" ‘{print $2}’`

#’if’ loop that uses the information in the variable above to
#set the correct proxy according to the particular network setup.
#Note that the SSID’s in quotes need to be hand coded by you and
#match the SSID you are using _exactly_ otherwise the script will
#set your proxy to NULL

#this part aded to exit the script if wireless is switched off.
if [ "$NETWORK" = " " ]; then
elif [ "$NETWORK" = "mySSID1" ]; then
#You have to set and name your proxy configs accordingly otherwise
#the script will fail.
cp /etc/privoxy/mySSID1 /etc/privoxy/config
elif [ "$NETWORK" = "mySSID2" ]; then
cp /etc/privoxy/mySSID2 /etc/privoxy/config
elif [ "$NETWORK" = "mySSID3" ]; then
cp /etc/privoxy/mySSID3 /etc/privoxy/config
elif [ "$NETWORK" = "mySSID4 ]; then
cp /etc/privoxy/mySSID4 /etc/privoxy/config
else cp /etc/privoxy/default /etc/privoxy/config


#Restarting the proxy
RESTARTPROXY=`/etc/init.d/privoxy restart > /etc/privoxy/proxystatus`
#Getting Proxy Status
GETPROXYSTATUS=`cat /etc/privoxy/proxystatus`

WHICH=`grep -A3 “443 (whi” /etc/privoxy/config | grep forward | awk -F forward ‘{print $2}’`
su YOURUSERNAME -c “env DISPLAY=:0.0 notify-send \”PROXY NOW IN USE\” \”$WHICH\”"


There you have it.

Behold my ugly scripting!

On a more serious note though, building this thing I figured out how to get notify-send to send output to my screen from a root shell, get privoxy to reload and get my if loop to work without self destructing spectacularly.

Have fun and let me know if you customise it!

No related posts.