How to set up proxy auto config on Ubuntu Desktop

Suppose you are using your Ubuntu Desktop laptop at home and workplace. When you are at your workplace, the corporate network your laptop is connected to is behind proxy. You would then have to turn on/off proxy depending on where you are. Of course you could manually update proxy settings of Ubuntu Desktop every time your network changes, but that'd be quite cumbersome. That's when proxy auto-config can help.

A proxy auto-config (PAC) is a powerful mechanism that allows one to conditionally define proxy settings for web browsers. Using PAC, you can automatically switch proxy settings based on destination URL, IP address of local host, time of day, etc. As you can imagine, PAC is extremely useful for setting up proxy exceptions, proxy load balancing, network-aware conditional proxy, etc.

In the following, I will show you how to set up PAC on Ubuntu Desktop so that you can automatically switch between "no-proxy" and "proxy" based on the IP address of local host.

Creating a PAC file for automatic proxy switching is not complicated. In a PAC file, you essentially define FindProxyForURL(url, host) JavaScript function which is supposed to return the proxy to use when fetching a given URL. Create a PAC file as follows.

$ sudo vi /etc/proxy.pac
function FindProxyForURL(url, host)
  if (isInNet(myIpAddress(), "", "")) {
    if (isInNet(host, "", ""))
      return "DIRECT";
    if (shExpMatch(url, "http:*"))
      return "PROXY" ;
    if (shExpMatch(url, "https:*"))
      return "PROXY" ;
    if (shExpMatch(url, "ftp:*"))
      return "PROXY" ;
    return "DIRECT";
  } else {
    return "DIRECT";

In this PAC file, if you are connected to network (assuming that is the corporate network), you use proxy ( for all destinations other than Otherwise, you do not use proxy at all.

Once you have created this PAC file, go to "System Settings" -> "Network" -> "Proxy Settings", and choose "Automatic" method in network proxy. Then type "file:///etc/proxy.pac" in configuration url field.

Proxy autoconfig on Ubuntu Desktop

Configure the IP address of localhost

There is one last important thing to do before finalizing PAC configuration. In the PAC file you created, myIpAddress() is supposed to return the IP address of localhost correctly. As a final step, you should verify that is the case by using hostname command.

$ hostname -i

If the hostname command returns "", not an actual IP address assigned to your laptop, then myIpAddress() will also return "", and the above proxy auto configuration will fail. To get around this problem, you need to set up the real IP address of local host somewhere.

In Linux, you can hard code the IP address of local host in /etc/hosts. However, since the IP address of localhost may keep changing depending on where you are, you can write a start-up script which automatically generates /etc/hosts upon boot.

To do that, first rename the original /etc/hosts to something else, which will then be used to generate an actual /etc/hosts to use.

$ sudo mv /etc/hosts /etc/hosts.custom

Now, create the following script which generates /etc/hosts from /etc/hosts.custom.

$ vi

WIRED_IP=`ifconfig eth0 | sed -ne 's/.*inet addr:\([^ ]*\).*/\1/p'`
WIRELESS_IP=`ifconfig wlan0 | sed -ne 's/.*inet addr:\([^ ]*\).*/\1/p'`

cat /etc/hosts.custom > /etc/hosts

cat >> /etc/hosts << EOF
# This file is automatically generated by /sbin/

exit 0

The script takes the IP address of either eth0 (for wired) or wlan0 (for wireless), and puts it in /etc/hosts.

The next step is to make script get called when a network interface gets activated. Assuming that you are using NetworkManager on Ubuntu Desktop, this can be achieved by running the following.

$ sudo chmod 755
$ sudo cp /etc/NetworkManager/dispatcher.d

Now the script will get executed to update /etc/hosts automatically every time any wired or wireless network interface is up.

An alternative way to run script is to configure your Ubuntu Desktop, so that the script automatically gets executed when you log in to your desktop.

After using either of the above two methods, verify that your hostname can successfully be resolved to IP address after you log in.

$ hostname -i

Proxy auto configuration is now set up on Ubuntu Desktop. Your Ubuntu Desktop will automatically turn on or off proxy depending on where you are at home or workplace.

Use proxy auto configuration on Firefox

If you want to use /etc/proxy.pac on Firefox, follow the instruction here.

First open up "Connection Settings" menu in Firefox preferences. Then enter "resources:///proxy.pac" as the automatic proxy configuration URL.

Then create a symbolic link to /etc/proxy.pac inside the directory where Firefox executable exists (e.g., /usr/bin).

$ sudo ln -s /etc/proxy.pac /usr/bin/proxy.pac

Once you restart Firefox, it will start using the proxy auto configuration.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

10 thoughts on “How to set up proxy auto config on Ubuntu Desktop

  1. Hi there, many thanks for sharing this -- it sounds like it will be really useful for my purposes! I haven't tried it out yet, though I have a question: if I'm not mistaken, this approach only works assuming the correct IP address is known during boot/start-up, since it is only then that the correct /etc/hosts file is created, and that in turn is used by the myIPAddress method. In my case, this wouldn't work, because I can switch between WiFi and wired ethernet several times during the course of the day, and I don't want to have to reboot every time. Is it possible for the proxy.pac configuration to actually check for the real IP address given the condition where it might switch "on-the-fly"? I notice that hostname -I or --all-ip-addresses (i.e. not hostname -i) yields the correct and actual IP addresses without any reference to /etc/hosts. Would it be possible for the proxy.pac configuration to somehow "execute" this command to determine which proxy rule to trigger?

    • Good point. Actually you can achieve it quite easily by placing script in /etc/NetworkManager/dispatcher.d Whenever any new (wired/wireless) network interface is up, NetworkManager will execute all the scripts located in /etc/NetworkManager/dispatcher.d. You don't have to log out/in or reboot any more. Your /etc/hosts will automatically be updated while network interfaces are changing.

      If you are not using NetworkManager, but using /etc/network/interfaces to manage your networks, you can add "post-up /sbin/" in /etc/network/interfaces instead.

      Post has been updated to add this suggestion.

  2. I think that the correct syntax for cat is
    cat > /etc/hosts << EOF
    bla bla

    However, the how-to is great. Thanks for the help!

      • There is still wrong code in the script above. It should be:
        cat > /etc/hosts << EOF

        Also, for Firefox, your suggestion doesn't work for me, but instead I have to use "Use system proxy settings" option and then it works.

        Thanks for the tutorial!

        • If you use this command (with only one pipe symbol '>'):

          ~$ cat > /etc/hosts << EOF
          Bla bla bla

          the final file will have only the string "Bla bla bla" then your backup file (/etc/hosts.custom) will be in vain. On the other hand, if you use the suggested command you may have more than one IP associated to your HOST_NAME.

  3. Hello,

    This is the first post I find on this matter and it does interest me a lot since I'm stuck in the exact same qituation : a laptop I use at home and at work, but... sorry to say I am unable to male it work :( Maybe I'm missing something.
    The proxy at my school is : So I changed the proxy lines in your script (replacing 8000 by 3128). The server is, so I changed the line wiht that and the mask to I don't really know what to do with the line in your script. I've tried all possible solutions coming to my mind, but nothing works... I used the script, that is OK, I receive a IP address when at work....

    If anybody has a little time to help, that would be fantastic!

  4. Hi. Great post. I was wondering what is the simplest way to do that in the script, so that the proxy is used according to the network connection name. Which command to use instead 'myIpAddress()'
    thank you

  5. Hi,

    Seems like one should replace 'inet addr' by 'inet adr' in this two lines :
    WIRED_IP=`ifconfig eth0 | sed -ne 's/.*inet addr:\([^ ]*\).*/\1/p'`
    WIRELESS_IP=`ifconfig wlan0 | sed -ne 's/.*inet addr:\([^ ]*\).*/\1/p'

    Best regards,

    • Hi,
      Thanks for the notification.
      I'm still facing an issue though: the script works perfectly on its own, but when I put it in dispatcher.d it returns nothing for the IP. Seems like ifconfig returns nothing. I've tried setting the full path to /sbin/ (and /bin for sed) with no luck. I've tried moving the script in network/if-up-d/ but it failed too ;(

      If anyone could help, I'd greatly appreciate.

Leave a comment

Your email address will not be published. Required fields are marked *