WSL (Ubuntu): how to open localhost in browser from bash terminal

不问归期 提交于 2020-07-06 10:39:14

问题


I am trying to open http://localhost in (any) browser from WSL bash terminal.

So far I have tried:

  • How can I open Google Chrome from the terminal with the URL "localhost:3000"?
  • "Couldn't find a file descriptor referring to the console" on Ubuntu bash on Windows
  • How to mention C:\Program Files in batchfile

No luck in setting up BROWSER variable for xdg-open, it responds to xdg-open http://localhost with /usr/bin/xdg-open: 851: /usr/bin/xdg-open: /c/"Program: not found.

I have tried escaping with \ and ^. Using %ProgramFiles(x86)% and ofcorse "Program Files (x86)". More or less it is the same answer every time... Any ideas how to set a work flow for opening browser in WSL?

So far I've ended up with:

/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe localhost

But I am looking for more elegant solution.


回答1:


You are almost there. Just add an alias for the windows chrome executable http://www.linfo.org/alias.html

alias chrome="/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe"

Now you can simply run chrome localhost and open chrome in any web location you desire.




回答2:


You can invoke the Windows command line from Bash and use Windows file association to open URL with the default Windows browser.

To do so just type in Bash:

cmd.exe /C start http://localhost

In my case this loads localhost in Chrome, note that the full URL is necessary for Windows to decide what to do.

This is similar to what open does in MacOS, hence you may find useful to directly alias the command and use it also for other type of files:

# add this to .bash_aliases
open='cmd.exe /C start'

Now you can open URL or open file.pdf directly from WSL.




回答3:


To open localhost in browser from bash terminal, you need to configure wsl so that it
defaults to whatever browser has been set as default in your windows 10 system.

You can do this by using some tools from wslu ("A collection of utilities for WSL").
For this purpose you need.

  • wslview (-u, --unregister "remove wslview as the default WSL web browser.
    -r, --register "register wslview as the default WSL web browser.)

  • wslpath (-a "force result to absolute path format",
    -u "translate from a Windows path to a WSL path (default)")

You need to register your preferred browsers like this...
For Google Chrome:
wslview -r $(wslpath -au 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')

For Microsoft Edge:
wslview -r $(wslpath -au 'C:\Windows\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\MicrosoftEdge.exe')

Now you can open localhost as x-www-browser localhost:8080 or www-browser localhost:8080 and x-www-browser or www-browser will default to whatever is your current windows 10 default browser provided it has been registered as described above.
Do not forget to indicate the port; localhost alone did not work for me.

To unregister any browser just change the -r flag to -u.

Have a look at wslview help: info wslview <enter> in the wsl terminal and wslpath <enter> for help with wslpath.




回答4:


Ok so first of all, I don't use windows anymore so I can't post a full solution that I've personally tested, but back when I did use windows, I use to do this and it worked. (This should probably be a comment, but a while back I deleted some unaccepted answers and lot the associated reputation :/)

Solution:

Don't try to launch your windows programs from inside WSL, instead install the linux version of the program and an X server, such as Xming. Here is an example tutorial for forwarding X apps back to Xming on windows.

Summarized, install Xming (on Windows). Then export the DISPLAY variable:

export DISPLAY=:0

Install google-chrome inside WSL and launch it via the CLI. It should show up on your desktop.

Note: There's also a way to use PuTTY alongside XMing for remote viewing, but you'll need to disable Windows firewalls and install the full openssh-server inside WSL first.




回答5:


I created a script that basically forwards xdg-open to powershell -c start

Not tested much though.

sudo tee /usr/local/bin/xdg-open <<EOF
#!/bin/sh

powershell.exe -c start "'\$@'"
EOF
sudo chmod +x /usr/local/bin/xdg-open

Cheers Oliver




回答6:


Came across this article that worked for me: https://towardsdatascience.com/running-jupyter-notebook-on-wsl-while-using-firefox-on-windows-5a47ebfae4c1

In short:

Step 1 - Generate config for Jupyter Notebook:

jupyter notebook --generate-config

Step 2 - Edit the config file using "nano" or other editor

The config fileshould be under your home directory under ".jupyter" folder:

~/.jupyter/jupyter_notebook_config.py

Step 3 - Disable launching browser by redirecting file

First comment out the line, then change True to False:

c.NotebookApp.use_redirect_file = False

Step 4 - add a line to your .bashrc file to set the BROWSER path

export BROWSER='/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'

For me it was Chrome under my Windows Program File. Otherwise any linux installation under WSL doesn't have a native browser to launch, so need to set it to the Windows executable.

Step 5 - restart .bashrc

source .bashrc

That should work!



来源:https://stackoverflow.com/questions/52691835/wsl-ubuntu-how-to-open-localhost-in-browser-from-bash-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!