45

How do I change Firefox Proxy settings via command line on windows xp/2k?

Thanks

Guss
  • 30,470
  • 17
  • 104
  • 128
bluegene
  • 5,767
  • 8
  • 26
  • 18

18 Answers18

18

The proxy setting is stored in the user's prefs.js file in their Firefox profile.

The path to the Firefox profile directory and the file is:

%APPDATA%\Mozilla\Firefox\Profiles\7b9ja6xv.default\prefs.js

where "7b9ja6xv" is a random string. However, the directory of the default profile always ends in ".default". Most of the time there will be only one profile anyway.

Setting you are after are named "network.proxy.http" and "network.proxy.http_port".

Now it depends on what technology you are able/prepared to use to change the file.

P.S.: If this is about changing the proxy settings of a group of users via the logon script or similar, I recommend looking into the possibility of using the automatic proxy discovery (WPAD) mechanism. You would never have to change proxy configuration on a user machine again.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Even with WPAD you need to change `network.proxy.type`. See my answer [below](http://stackoverflow.com/a/24807333/594137). – Samuel Harmer Jul 17 '14 at 15:59
10

Just wanted to post the code in a cleaner format... originally posted by sam3344920

cd /D "%APPDATA%\Mozilla\Firefox\Profiles"
cd *.default
set ffile=%cd%
echo user_pref("network.proxy.http", "148.233.229.235 ");>>"%ffile%\prefs.js"
echo user_pref("network.proxy.http_port", 3128);>>"%ffile%\prefs.js"
echo user_pref("network.proxy.type", 1);>>"%ffile%\prefs.js"
set ffile=
cd %windir%

If someone wants to remove the proxy settings, here is some code that will do that for you.

cd /D "%APPDATA%\Mozilla\Firefox\Profiles"
cd *.default
set ffile=%cd%
type "%ffile%\prefs.js" | findstr /v "user_pref("network.proxy.type", 1);" >"%ffile%\prefs_.js"
rename "%ffile%\prefs.js" "prefs__.js"
rename "%ffile%\prefs_.js" "prefs.js"
del "%ffile%\prefs__.js"
set ffile=
cd %windir%

Explanation: The code goes and finds the perfs.js file. Then looks within it to find the line "user_pref("network.proxy.type", 1);". If it finds it, it deletes the file with the /v parameter. The reason I added the rename and delete lines is because I couldn't find a way to overwrite the file once I had removed the proxy line. I'm sure there is a more efficient/safer way of doing this...

Matthias
  • 13,607
  • 9
  • 44
  • 60
xBoarder
  • 106
  • 2
  • 5
  • 3
    I never knew that something like `cd *.default*` is possible -- Even better: `pushd %APPDATA%\Mozilla\Firefox\Profiles\*.default*` works too. – Martin Mar 18 '15 at 13:37
  • @xBoarder What you do when you need the string available to sed again, is to just comment it. Add more, don't remove. ie: `user_pref("network.proxy.type", 1);`. Also, instead of remove you can just set `network.proxy.type` to direct connection. – m3nda May 24 '17 at 00:28
10

I don't think you can. What you can do, however, is create different profiles for each proxy setting, and use the following command to switch between profiles when running Firefox:

firefox -no-remote -P <profilename>
Ayman Hourieh
  • 132,184
  • 23
  • 144
  • 116
4

The easiest way to do this is to configure your Firefox to use a PAC with a file URL, and then change the file URL from the line command before you start Firefox.

This is the easiest way. You don't have to write a script that remembers what path to prefs.js is (which might change over time).

You configure your profile once, and then you edit the external file whenever you want.

benc
  • 1,381
  • 5
  • 31
  • 39
  • 5
    change the file URL from the line command before you start Firefox -> Please explain yourself or put a code example. – m3nda Mar 03 '15 at 09:55
  • Can you further detail what is a PAC file and what should be done. I am also interested into how to start a new web session. I dont want that firefox ignores the new process because it is already running and open a new tab instead. –  Apr 16 '20 at 12:46
  • This question is so old, I'll put my response in comments: Save the PAC file you want to disk. Then view it in the browser, this will force the creation of a correct file URL in the location bar. Copy this value, go to your browser proxy preferences, select PAC file, and paste in that URL (rather than using a more standard `http:` or `https:` URL. – benc Apr 18 '20 at 00:07
  • re: what is a PAC file -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_(PAC)_file – benc Apr 18 '20 at 00:08
  • re: web session -> should that be a different question? – benc Apr 18 '20 at 00:09
3

You can easily launch Firefox from the command line with a proxy server using the -proxy-server option.

This works on Mac, Windows and Linux.

path_to_firefox/firefox.exe -proxy-server %proxy_URL%

Mac Example:

/Applications/Firefox.app/Contents/MacOS/firefox -proxy-server proxy.example.com

  • 3
    This is not a documented option and doesn't work in Linux at least. – EricLaw Jul 19 '13 at 16:18
  • @EricLaw There's an extension to tell Firefox to read `http_proxy` env variable, so you can simply chain it by `http_proxy="http://midomain:port" firefox google.com` as example. Finally, you can make use of gsettings package, which let you set anything "globally" using `gsettings set org.gnome.system.proxy`. See here (http://dpaste.com/3BE8YK1) a full example extracted from Fiddler's attach.script for Linux. – m3nda Feb 25 '18 at 08:42
3

cd /D "%APPDATA%\Mozilla\Firefox\Profiles" cd *.default set ffile=%cd% echo user_pref("network.proxy.http", "%1");>>"%ffile%\prefs.js" echo user_pref("network.proxy.http_port", 3128);>>"%ffile%\prefs.js" echo user_pref("network.proxy.type", 1);>>"%ffile%\prefs.js" set ffile= cd %windir%

This is nice ! Thanks for writing this. I needed this exact piece of code for Windows. My goal was to do this by learning to do it with Linux first and then learn the Windows shell which I was not happy about having to do so you saved me some time!

My Linux version is at the bottom of this post. I've been experimenting with which file to insert the prefs into. It seems picky. First I tried in ~/.mozilla/firefox/*.default/prefs.js but it didn't load very well. The about:config screen never showed my changes. Currently I've been trying to edit the actual Firefox defaults file. If someone has the knowledge off the top of their head could they rewrite the Windows code to only add the lines if they're not already in there? I have no idead how to do sed/awk stuff in Windows without installing Cygwin first.

The only change I was able to make to the Windows scripts is above in the quoted part. I change the IP to %1 so when you call the script from the command line you can give it an option instead of having to change the file.

#!/bin/bash
version="`firefox -v | awk '{print substr($3,1,3)}'`"
echo $version " is the version."
# Insert an ip into firefox for the proxy if there isn't one
if
! grep network.proxy.http /etc/firefox-$version/pref/firefox.js 
  then echo 'pref("network.proxy.http", "'"$1"'")";' >> /etc/firefox-$version/pref/firefox.js 
fi

# Even if there is change it to what we want
sed -i s/^.*network.proxy.http\".*$/'pref("network.proxy.http", "'"$1"')";'/  /etc/firefox-$version/pref/firefox.js 

# Set the port
if ! grep network.proxy.http_port /etc/firefox-$version/pref/firefox.js 
  then echo 'pref("network.proxy.http_port", 9980);' >> /etc/firefox-$version/pref/firefox.js 
  else sed -i s/^.*network.proxy.http_port.*$/'pref("network.proxy.http_port", 9980);'/ /etc/firefox-$version/pref/firefox.js 
fi

# Turn on the proxy
if ! grep network.proxy.type  /etc/firefox-$version/pref/firefox.js 
  then echo 'pref("network.proxy.type", 1);' >> /etc/firefox-$version/pref/firefox.js 
  else sed -i s/^.*network.proxy.type.*$/'pref("network.proxy.type", 1)";'/ /etc/firefox-$version/pref/firefox.js 
fi
johnny2k
  • 53
  • 1
  • 5
  • sed rocks. To avoid that, there's an Firefox extension that allows firefox to get global variable `http_proxy=http://localhost:8080` in example. You don't need to `export`, just to prepend it to comand. In example Chrome works out of the box using `http_proxy=http://some-proxy-ip:port chrome httpbin.org/ip`. I don't know why is that not handled by firefox. – m3nda May 24 '17 at 00:31
  • Adding few $@ arguments to that script will perform ok, also to support other than just default proxy file :-) – m3nda May 24 '17 at 00:33
  • @erm3nda could you specify the extension you are referring too? – Rick van der Zwet Apr 18 '18 at 08:20
  • @RickvanderZwet The extension is using environment variables. Here it is: https://addons.mozilla.org/es/firefox/addon/environment-proxy/. I didn't check if it's supported with the new and standard extensions. – m3nda Apr 18 '18 at 18:17
3
it working perfect.

cd /D "%APPDATA%\Mozilla\Firefox\Profiles"
cd *.default
set ffile=%cd%
echo user_pref("network.proxy.ftp", "YOUR_PROXY_SERVER"); >>prefs.js
echo user_pref("network.proxy.ftp_port", YOUR_PROXY_PORT); >>prefs.js
echo user_pref("network.proxy.http", "YOUR_PROXY_SERVER"); >>prefs.js
echo user_pref("network.proxy.http_port", YOUR_PROXY_PORT); >>prefs.js
echo user_pref("network.proxy.share_proxy_settings", true); >>prefs.js
echo user_pref("network.proxy.socks", "YOUR_PROXY_SERVER"); >>prefs.js
echo user_pref("network.proxy.socks_port", YOUR_PROXY_PORT); >>prefs.js
echo user_pref("network.proxy.ssl", "YOUR_PROXY_SERVER"); >>prefs.js
echo user_pref("network.proxy.ssl_port", YOUR_PROXY_PORT); >>prefs.js
echo user_pref("network.proxy.type", 1); >>prefs.js
set ffile=
cd %windir%
user220579
  • 31
  • 2
2

I needed to set an additional option to allow SSO passthrough to our intranet site. I added some code to an example above.

pushd "%APPDATA%\Mozilla\Firefox\Profiles\*.default"
echo user_pref("network.proxy.type", 4);>>prefs.js
echo user_pref("network.automatic-ntlm-auth.trusted-uris","site.domain.com, sites.domain.com");>>prefs.js
popd
Matt Hill
  • 46
  • 6
2

This is the final compiled solution which worked for me... Tried and tested...

Steps to Change Proxy settings in Mozilla Firefox via cmd in Windows

  1. cd /D "%APPDATA%\Mozilla\Firefox\Profiles"
  2. cd *.default

To Replace Already Present Proxy Settings with User-defined ones

  1. powershell -Command "(gc prefs.js) -replace 'user_pref(\"network.proxy.http\", \"already present IP\")\;', 'user_pref(\"network.proxy.http\", \"your http proxy ip\");' | Set-Content prefs.js"
  2. powershell -Command "(gc prefs.js) -replace 'user_pref(\"network.proxy.http_port\", already present port)\;', 'user_pref(\"network.proxy.http_port\", your http proxy port);' | Set-Content prefs.js"
  3. powershell -Command "(gc prefs.js) -replace 'user_pref(\"network.proxy.share_proxy_settings\", already present value)\;', 'user_pref(\"network.proxy.share_proxy_settings\", true);' | Set-Content prefs.js"
  4. powershell -Command "(gc prefs.js) -replace 'user_pref(\"network.proxy.type\", already present value)\;', 'user_pref(\"network.proxy.type\", 1);' | Set-Content prefs.js"

    0 - No Proxy

    1 - Manual Proxy Configuration

    4 - Auto detect Proxy Settings

    5 - Use System Settings(default)

  5. cd %windir%

To Check Already Present Proxy Settings

  1. cd C:\Users\{username}\AppData\Roaming\Mozilla\Firefox\Profiles\sat2m7dr.default\
  2. find /i "network.proxy" prefs.js

To Start Firefox from CMD using your defined proxy settings

  1. cd C:\Program Files\
  2. cd "Mozilla Firefox"
  3. firefox.exe -ProfileManager
  4. Select default from the list (default-release is selected by default) and click ok.

NOTE: You may not have to run step 14 again. Instead you can directly run firefox.exe

1

All the other answers here explain how to program your proxy settings into Firefox which is what WPAD was invented to do. If you have WPAD configured then just tell Firefox to use it to auto-detect its settings, as you would in the GUI.

Connection Settings

To do this from a cmd file or command line:

pushd "%APPDATA%\Mozilla\Firefox\Profiles\*.default"
echo user_pref("network.proxy.type", 4);>>prefs.js
popd

This of course requires you to have WPAD configured and working correctly. Also I believe prefs.js won't exist until you've run Firefox once.

Martin
  • 10,738
  • 14
  • 59
  • 67
Samuel Harmer
  • 4,264
  • 5
  • 33
  • 67
1

I found a better way to do this with powershell under windows (but really only because I was looking for a way to script changing the user agent string, not muck about with proxies).

function set-uas
{
    Param
    (
            [string]$UAS = "Default"
    )

    $FirefoxPrefs = "C:\Users\Admin\AppData\Roaming\Mozilla\Firefox\Profiles\*.default\prefs.js"

    if ($UAS -eq "Default")
    {
        $fileinfo = type $FirefoxPrefs
        $fileinfo = $fileinfo | findstr /v "general.appname.override"    
        $fileinfo = $fileinfo | findstr /v "general.appversion.override"
        $fileinfo = $fileinfo | findstr /v "general.platform.override"  
        $fileinfo = $fileinfo | findstr /v "general.useragent.appName"  
        $fileinfo = $fileinfo | findstr /v "general.useragent.override" 
        $fileinfo = $fileinfo | findstr /v "general.useragent.vendor"   
        $fileinfo = $fileinfo | findstr /v "general.useragent.vendorSub"
        $fileinfo += "user_pref(`"useragentswitcher.import.overwrite`", false);`n"
        $fileinfo += "user_pref(`"useragentswitcher.menu.hide`", false);`n"
        $fileinfo += "user_pref(`"useragentswitcher.reset.onclose`", false);`n"
        $fileinfo | Out-File -FilePath $FirefoxPrefs -Encoding ASCII
    }
    else
    {
        set-uas Default
    }

    if ($UAS -eq "iphone")
    {
        $fileinfo = ""
        $fileinfo += "user_pref(`"general.appname.override`", `"Netscape`");`n"
        $fileinfo += "user_pref(`"general.appversion.override`", `"5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16`");`n"
        $fileinfo += "user_pref(`"general.platform.override`", `"iPhone`");`n"                                                                                                                                      
        $fileinfo += "user_pref(`"general.useragent.appName`", `"Mozilla`");`n"                                                                                                                                     
        $fileinfo += "user_pref(`"general.useragent.override`", `"Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16`");`n"
        $fileinfo += "user_pref(`"general.useragent.vendor`", `"Apple Computer, Inc.`");`n"                                                                                                                         
        $fileinfo += "user_pref(`"general.useragent.vendorSub`", `"`");`n"                                                                                                                                          
        $fileinfo += "user_pref(`"useragentswitcher.reset.onclose`", false);`n"
        $fileinfo | Out-File -FilePath $FirefoxPrefs -Encoding ASCII -Append
    }
    elseif ($UAS -eq "lumia")
    {
        $fileinfo = ""
        $fileinfo += "user_pref(`"general.appname.override`", `"Netscape`");`n"
        $fileinfo += "user_pref(`"general.appversion.override`", `"9.80 (Windows Phone; Opera Mini/9.0.0/37.6652; U; en) Presto/2.12.423 Version/12.16`");`n"
        $fileinfo += "user_pref(`"general.platform.override`", `"Nokia`");`n"                                                                                                                                       
        $fileinfo += "user_pref(`"general.useragent.appName`", `"Mozilla`");`n"                                                                                                                                     
        $fileinfo += "user_pref(`"general.useragent.override`", `"Opera/9.80 (Windows Phone; Opera Mini/9.0.0/37.6652; U; en) Presto/2.12.423 Version/12.16`");`n"
        $fileinfo += "user_pref(`"general.useragent.vendor`", `"Microsoft`");`n"                                                                                                                            
        $fileinfo += "user_pref(`"general.useragent.vendorSub`", `"`");`n"                                                                                                                                          
        $fileinfo += "user_pref(`"useragentswitcher.reset.onclose`", false);`n"
        $fileinfo | Out-File -FilePath $FirefoxPrefs -Encoding ASCII -Append
    }
}

I have the firefox plugin "useragentswitcher" also installed, and have not tested this without it.
I also have set "user_pref("useragentswitcher.reset.onclose", false);"

[EDIT] I've revised my code, it was occasionally outputting some bad character or something. For some reason this is detected by firefox as a corrupt profile, and the entire profile was discarded, and refreshed with a default profile.

Also, credit where credit is due: this code is loosely based off of what xBoarder posted in his response to sam3344920 (https://stackoverflow.com/a/2509088/5403057). Also, I was able to fix the encoding bug with help from a post from Phoenix14830 (https://stackoverflow.com/a/32080395/5403057)

[Edit2] Added support for setting the UAS to lumia. This is actually using an Opera mobile UAS, because I still wanted bing to work, and if you use the regular lumia UAS www.bing.com redirects to bing://?%^&* which firefox doesn't know how to process

Community
  • 1
  • 1
GarThor
  • 51
  • 4
  • hmm... now it seems to be adding a bunch of null's between characters for some strange reason... o_0 – GarThor Oct 02 '15 at 22:48
1

user.js is better for customizations as you can include only the lines you want to manipulate, i.e. instead of find-replace you can just overwrite the entire file. Also, prefs.js (at least on Firefox 65.0.1 for Mac) starts with a warning:

// DO NOT EDIT THIS FILE.
//
// If you make changes to this file while the application is running,
// the changes will be overwritten when the application exits.
//
// To change a preference value, you can either:
// - modify it via the UI (e.g. via about:config in the browser); or
// - set it within a user.js file in your profile.

In my case, user.js didn't exist, so I created it and included the line to switch between "No proxy" and "Manual proxy configuration" (I'm using only one SOCKS proxy all the time, so no need to change port number or any other details, just flip 0 to 1 in the following line):

user_pref("network.proxy.type", 1);

I ended up with a bash script that I placed at /usr/local/bin/firefox:

#!/bin/bash
if [ $# -eq 0 ]; then
  echo 'user_pref("network.proxy.type", 0);' > ~/Library/Application\ Support/Firefox/Profiles/t5rvw47o.default/user.js
  open -a Firefox
else
  case $1 in
    vpn)
      echo 'user_pref("network.proxy.type", 1);' > ~/Library/Application\ Support/Firefox/Profiles/t5rvw47o.default/user.js
      open -a Firefox
  esac 
fi

To use it, I make sure no Firefox is running and then run firefox to have a straight connection and firefox vpn to use proxy.

0

You could also use this Powershell script I wrote to do just this, and all other Firefox settings as well.

https://bitbucket.org/remyservices/powershell-firefoxpref/wiki/Home

Using this you could easily manage Firefox using computer startup and user logon scripts. See the wiki page for directions on how to use it.

Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
David Remy
  • 101
  • 1
  • 2
0

Thank very much, I find the answers in this website.

Here I refer to the production of a cmd file

by minimo

cd /D "%APPDATA%\Mozilla\Firefox\Profiles"
cd *.default
set ffile=%cd%
echo user_pref("network.proxy.http", "192.168.1.235 ");>>"%ffile%\prefs.js"
echo user_pref("network.proxy.http_port", 80);>>"%ffile%\prefs.js"
echo user_pref("network.proxy.type", 1);>>"%ffile%\prefs.js"
set ffile=
cd %windir%
Community
  • 1
  • 1
  • Thank very much, I find the answers in this website. Here I refer to the production of a cmd file by minimo cd /D "%APPDATA%\Mozilla\Firefox\Profiles" cd *.default set ffile=%cd% echo user_pref("network.proxy.http", "148.233.229.235 ");>>"%ffile%\prefs.js" echo user_pref("network.proxy.http_port", 3128);>>"%ffile%\prefs.js" echo user_pref("network.proxy.type", 1);>>"%ffile%\prefs.js" set ffile= cd %windir% –  Aug 06 '09 at 06:30
0
@echo off
color 1F


cd /D "%APPDATA%\Mozilla\Firefox\Profiles"
cd *.default 
set ffile=%cd%
cd %ffile%
echo user_pref("network.proxy.http", "192.168.1.235 ");>>"prefs.js" 
echo user_pref("network.proxy.http_port", 80);>>"prefs.js" 
echo user_pref("network.proxy.type", 1);>>"prefs.js" 
set ffile=
cd %windir%
Logan78
  • 31
  • 2
  • Some explanation what you do here might be quite helpful. – Styxxy Oct 13 '12 at 11:07
  • I like to grok CD *.default, but I suggest to ECHO the three lines on the file user.js : Firefox on start read and merge user.js into prefs.js ( so we don't edit the master configuration file ). – Massimo Dec 28 '12 at 22:26
0

for the latest firefox you must change

cd *.default

to

cd *.default*
Goon
  • 9
  • 1
0

I don't think there is a direct way to set the proxy (on Windows).

You could however install an add-on like FoxyProxy, create several configurations for different proxies and prior to starting FireFox move the appropriate configuration to the correct folder in your FireFox profile (using a batch file).

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
-1

Hello I got the Perfect cod use this code

cd /D "%APPDATA%\Mozilla\Firefox\Profiles"

cd *.default

set ffile=%cd%

echo user_pref("network.proxy.http", "127.0.0.1"); >>prefs.js

echo user_pref("network.proxy.http_port", 8080); >>prefs.js

set ffile=

cd %windir

Banna
  • 1