- Linux: command to open URL in default browser
- 11 Answers 11
- How to Open a URL in the Web Browser from Linux Terminal
- 1. The xdg-open Command
- 2. The sensible-browser Command
- 3. The x-www-browser Command
- 4. Alternative Commands to Open URL
- How to open default browser from command line Linux
- Checking default web browser:
- Opening default web browser from the command line:
- Changing default web browser:
- Conclusion:
- About the author
- Talha Saif Malik
- Command to open a URL
Linux: command to open URL in default browser
What command we have to execute (from Java, but that should not matter) on Linux (different common distributions) to open a given URL in the default browser?
11 Answers 11
The most cross-distribution one is xdg-open http://stackoverflow.com
I believe the simplest method would be to use Python:
python -m webbrowser "http://www.example.com/"
I like this solution better for a cross-platform use case, but for Linux only, it does take ~7-8x as much time to run on my system compared to xdg-open for the same url.
Just as a note for people running this on windows: I’ve found it often opens up Internet Explorer. (instead of the user’s configured default) But it works, I guess. 😅
on ubuntu you can try gnome-open.
In Java (version 6+), you can also do:
Desktop d = Desktop.getDesktop(); d.browse(uri);
Though this won’t work on all Linuxes. At the time of writing, Gnome is supported, KDE isn’t.
At least on Debian and all its derivatives, there is a ‘sensible-browser’ shell script which choose the browser best suited for the given url.
On distributions that come with the open command,
###1 Desktop's -or- Console use: sensible-browser $URL; # Opinion: best. Target preferred APP. # My-Server translates to: w3m [options] [URL or filename] ## [ -z "$BROWSER" ] && echo "Empty" # Then, Set the BROWSER environment variable to your desired browser. ###2 Alternative # Desktop (if [command-not-found] out-Dated) x-www-browser http://tv.jimmylandstudios.xyz # firefox ###3 !- A Must Know -! # Desktop (/usr/share/applications/*.desktop) xdg-open $URI # opens about anything on Linux (w/ .desktop file)
When using any of these commands in a Shell Script you’ll need to test if they exist first (e.g. command -v $CMD ). $? = 0
I think using xdg-open http://example.com is probably the best choice.
In case they don’t have it installed I suppose they might have just kde-open or gnome-open (both of which take a single file/url) or some other workaround such as looping over common browser executable names until you find one which can be executed(using which). If you want a full list of workarounds/fallbacks I suggest reading xdg-open(it’s a shell script which calls out to kde-open/gnome-open/etc. or some other fallback).
But since xdg-open and xdg-mime(used for one of the fallbacks,) are shell scripts I’d recommend including them in your application and if calling which xdg-open fails add them to temporary PATH variable in your subprograms environment and call out to them. If xdg-open fails, I’d recommend throwing an Exception with an error message from what it output on stderr and catching the exception and printing/displaying the error message.
I would ignore the java awt Desktop solution as the bug seems to indicate they don’t plan on supporting non-gnome desktops anytime soon.
How to Open a URL in the Web Browser from Linux Terminal
Flexibility is an underrated attribute overlooked by many users of the Linux operating system. Being a flexible Linux user implies that you can launch most; if not all, of your user and system applications from the command line interface without having to navigate and search for them from the system GUI applications’ menu.
Also, the flexibility of your Linux OS user experience not only applies to system-based applications like installed apps but also to non-system-based applications like web apps that require a URL for access.
When working with Linux, you don’t necessarily need to open a browser application before you can access targeted URLs associated with specific websites.
This article will walk us through various ways (commands) of opening a URL on a default browser application from the terminal.
1. The xdg-open Command
As per the Linux man page, the xdg-open command opens a file or URL via the user’s preferred/default file application or browser application.
Its usage can be demonstrated via the following command syntax:
For instance, to open the LinuxShellTips home page from the Linux command-line interface, we will execute the following command.
$ xdg-open 'http://ubuntumint.com'
The execution of the xdg-open command will open the specified URL on your default web browser application.
2. The sensible-browser Command
As per the Linux operating system man pages, the sensible-browser command can be used to execute sensible decisions regarding a default web browser application that can be used to open a targeted web URL.
Its implementation can be referenced from the following command syntax:
$ sensible-browser URL_OF_TARGETED_WEB_PAGE
For instance, we can access a random LinuxShellTips website URL in the following manner:
$ sensible-browser 'http://ubuntumint.com/ssh-passwordless-login-almalinux'
The execution of the sensible-browser command should redirect us to the specified URL on the default web browser.
The use of the sensible-browser command is fully supported in Debian-based Linux operating system distributions like Linux Mint and Ubuntu.
3. The x-www-browser Command
Even though the x-www-browser command might not exist in the Linux man-pages ($ man x-www-browser) of the Linux operating system distribution you are using, it is equally effective in redirecting a user to a specified web URL on their default web browser.
Its command syntax can be represented in the following manner:
$ x-www-browser URL_OF_TARGETED_WEB_PAGE
For demonstration, let us try to access another random web page on the LinuxShellTips website.
$ x-www-browser 'http://ubuntumint.com/download-files-remote-linux-servers'
The execution of the x-www-browser command should swiftly redirect you to a web browser with a display of the specified web page URL.
4. Alternative Commands to Open URL
You could alternatively use your browser name as part of the Linux command for opening the web page URL you are targeting:
For Firefox web browser:
For Google Chrome web browser:
$ google-chrome http://google.com
Despite the error messages on the terminal on executing these alternative commands, your specified web URL should open without any issues.
You now know some cool tweaks on opening web URLs on the Linux command-line without directly going to the web browser’s GUI app.
How to open default browser from command line Linux
On Ubuntu, you might have several web browsers installed, which are utilized according to your browsing preferences. Although, we all have a most favorite web browser to use 90% of the internet every day. In Linux, it is preferred to state the best browser as the default browser. This default browser will also allow you to open links from the terminal.
Checking default web browser:
To set, change or view the default browser through a terminal or user interface is simple yet straightforward.
Write out the below-given command to know the default browser of your Linux system.
You can look into the list of other default applications, including your default browser. For performing this, there are two methods, In the first method, you can use the below-given command, and it will redirect you to the Default Application windows.
Searching “Default Applications” in the activities is considered as the second method.
The highlighted web browser in the “Web” option is already picked as the default web browser.
You can also check out the list of other available alternatives for browsing the web on your system.
The output declares that “Google Chrome” also exists as a web browser on our Ubuntu.
Opening default web browser from the command line:
Now, we will try to open the google instance in the default web browser, which is “firefox” in our case. Utilize the below-given command in your terminal if you want to do so.
Here, you can see terminal command execution successfully open a google instance in our default web browser.
Changing default web browser:
For setting any other browser as the default one, you have to execute the “xdg” settings command with the browser name at the end. We will set “Google Chrome” as the default browser on our Linux system in the following example.
Again, open any web instance by specifying its URL in the “xdg” open command. Furthermore, we have chosen “Google” for the testing.
The output states that we are successful in setting “Google Chrome” as your default browser.
Conclusion:
As Linux users, we all have various installed web browsers, which we use according to our web preferences. Still, there exists a possibility of 90% using a single web browser for browsing activities which turns to be our default web browser. In this article, we have seen opening the default browser for any web instance from the command line. Furthermore, the procedure of viewing and setting any other alternatives to your default browser is also provided.
About the author
Talha Saif Malik
Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.
Command to open a URL
There’s a command that knows about your default browser:
This will also work for every other type of URI (Uniform Resource Identifier), like images — which will automatically open with eog, openoffice documents, and so on, and also on filesystem paths ( xdg-open /tmp/foobar.png ).
xdg-email team@stackexchange.com
xdg-mime query default text/html
To find out what application is going to be used.
You can set your preffered application by going to System → Preferences → Preferred Applications:
These utilities are part of the freedesktop.org specification, so you can use them in your applications — and it’s going to work on all of the desktops that adhere to the spec.
You can use xdg-open like so:
xdg-open opens a file or URL in the user’s preferred application. If a URL is provided the URL will be opened in the user’s preferred web browser.
If you don’t necessarily want to use the default browser, you can call any browser program directly and give the URL as argument, for example:
chromium-browser https://youtube.com
gnome-open works as well as xdg-open but neither knows what to do with naked domain.
So gnome-open http://askubuntu.com works but not gnome-open askubuntu.com .
Here is a small function to make it easier to type and accept the stripped down domain name.
Paste the above in your command line, hit enter and try it out. If you like it you can save it to your .bash_aliases or .bashrc file, then source ~/.bash_aliases (assuming it is in your home directory) and it will be persistent for that session and all new sessions.
So now we just type go askubuntu.com and wallah!