centoscentos7xdgutilsdefault-browser

How do I set the default browser for xdg-open on Centos 7 if xdg-settings has no desktop environment


There are many questions similar to mine (e.g. xdg-open not open default browser or xdgutils - xdg-settings not setting default-web-browser in gentoo, but none of the answers helped in my case. Therefor I ask for my particular situation:


On Centos 7 I have no free desktop manager running, I just run some X11 applications (like VS Code) from the command line where the DISPLAY variable is set to the X server on the (Windows) machine I connect from.

On the Centos machine I have two browsers installed, firefox and google-chrome. I can start both browsers just by typing firefox resp. google-chrome in the bash terminal.

xdg-open is available and it opens links in google-chrome - as does VS Code. However I want to change this to firefox.

I tried:

  1. Ticking "Default browser" in Firefox's GUI preferences.

  2. Using xdg-settings, but

    xdg-settings get default-web-browser 
    

    returns "xdg-settings: unknown desktop environment"

  3. Setting $BROWSER. In bash I issued

    export BROWSER=firefox
    

    but still google-chrome is started by xdg-open

How can I set in this environment the default browser to firefox?


Note: Strangely on another machine with Centos 6 (and "no desktop environment" either) the export BROWSER method works!


Solution

  • The desired behavior can be set in the mimeapps.list configuration files described in the XDG MIME Applications specification.

    TLDR: In order to configure firefox as the default browser for your user create ~/.config/mimeapps.list containing the following lines:

    [Default Applications]
    x-scheme-handler/http=firefox.desktop
    x-scheme-handler/https=firefox.desktop
    x-scheme-handler/ftp=firefox.desktop
    x-scheme-handler/chrome=firefox.desktop
    text/html=firefox.desktop
    application/x-extension-htm=firefox.desktop
    application/x-extension-html=firefox.desktop
    application/x-extension-shtml=firefox.desktop
    application/xhtml+xml=firefox.desktop
    application/x-extension-xhtml=firefox.desktop
    application/x-extension-xht=firefox.desktop
    

    Details: xdg-utils like xdg-open(1) and xdg-mime(1) look for this file in the locations listed under the File name and location section of this specification:

    The locations for the $XDG variables are governed by the XDG Base Directory specification. If you want to figure out where xdg-utils are looking for configuration in your particular case, run them with the XDG_UTILS_DEBUG_LEVEL environment variable like so:

    $ XDG_UTILS_DEBUG_LEVEL=10 xdg-open 'https://www.example.com'
    ...
    Checking /home/USERNAME/.config/mimeapps.list
    ...