How to change default directory in Windows Subsystem for Linux - windows-subsystem-for-linux

I am setting up my development environment, so I just installed Windows Subsystem for Linux and it always seems to open a fresh terminal in my Windows home directory - /mnt/c/Users/dl and I'm trying to make it default to the linux home directory - /home/dl.
I checked to see what the home directory is in the Linux subsystem in /etc/passwd and it is correctly set:
dl:x:1000:1000:,,,:/home/dl:/bin/bash
Then I came across this solution, but it doesn't seem to have any affect:
// Set starting directory
"startingDirectory": "\\\\wsl$\\Ubuntu\\home\\dl\\"
I know I can just run cd ~ in my dot files (which is what I'm currently using), but I'm looking for a way where /home/dl is just the default and cd ~ isn't needed. Is this possible?

You should only change the startingDirectory for WSL (Ubuntu in this case) terminal sessions.
Open settings.json via CTRL+SHIFT+,
Make sure you are modifying startingDirectory under profiles/list/name: "Ubuntu"
Example below (the slashes need to be escaped):
....
{
"guid": "{2c4de342-xxx-xxx-xxx-2309a097f518}",
"hidden": false,
"name": "Ubuntu",
"source": "Windows.Terminal.Wsl",
"startingDirectory": "\\\\wsl$\\Ubuntu\\home\\windows_username_in_lower_case"
},
....
Documentation about startingDirectory including default values and expected values.
Inside settings.json you will also find an explanation of the json schema which is here
If you need to know how or where to edit Windows Terminal settings/preferences: https://learn.microsoft.com/en-us/windows/terminal/get-started

In Windows 10 21H2 or later and Windows 11, it's now much simpler. According to the Microsoft Doc:
On newer versions of Windows, startingDirectory can accept Linux-style paths.
That means you can simply use:
"startingDirectory": "/home/yourusername"
No need for any prefixes for Windows directory structure, nor escaped backslashes. Just plain old Linux forward-slash notation.
This works in both WSL1 and WSL2.
Note: I tried to use "~" and it failed. There may be some way to use {$USERPROFILE}, but haven't tried it.

Changing the home directory with WSL is done the same way as in Linux:
Enter bash
Type the command sudo vim /etc/passwd
Find your account's line, which might look like:
shadyar:x:1000:1000:"",,,:/home/shadyar:/bin/bash
Change the home directory, which above is /home/shadyar, to the new directory, using WSL
note: If you want to set Windows directory as home directory, you need to prepend it with /mnt/, like /mnt/c for C:/, /mnt/d for D:/, etc
Save the file and exit vim by typing :wq and press Enter
Exit bash and re-launch it
To test, use the commands:
cd ~
pwd

The other answers here (especially the latest one from #TomBoland) are great for starting in an arbitrary directory, but the example in your question was to start in your home directory. The easiest way to do that is simply to create or change the "commandline" property to wsl ~. This is an undocumented flag to wsl.exe, and it must be the first argument (e.g. wsl ~ -u root).
Current and Recent Windows Terminal Releases
Since Windows Terminal now has a GUI for Settings, you can just edit your profile to point to wsl ~ in the ->General->Command Line setting.
Older Windows Terminal Releases, or if you want to edit manually
If you are editing your settings.json directly (currently found in %userprofile%\AppData\Local\Packages\MicrosoftWindowsTerminal...\LocalState\settings.json, but this may change) ...
Remove the "source" attribute and replace it with "commandline":
"guid": "{2d5ef231-38b7-51cf-b940-2309a097f644}",
"hidden": false,
"name": "Ubuntu",
//"source": "Windows.Terminal.Wsl",
"commandline": "wsl ~",
"startingDirectory": "//wsl$/Ubuntu/",
"tabTitle": "Ubuntu"
Also, for the fun of it, here's an alternative (hacky) way to open WSL to ~/$HOME (without hardcoding as with the other answers). This is absolutely not needed since it's much easier to use wsl ~, but:
wsl -e sh -c 'cd $HOME; exec $SHELL'
This starts up sh, changes the directory to $HOME, and then exec's your $SHELL to replace the sh.

Should you use Windows Terminal with WSL, then the simplest solution is to configure the starting directory via the Settings menu:
and then:

startingDirectory Should be a windows path, not a nix path. Try D:\Folder\SubFolder instead
refer this link,worked for me
github

I tried many things here and none worked but I finally found a workaround.
After opening your ubuntu, you can set the default path by editing your .bashrc file.
I personally wanted to change it from the default /home/${my_username} to my current user directory (like command prompt C:/users/${my_username}), so I just ran this in my Ubuntu terminal
echo 'cd "../../mnt/c/users/${my_username}"' >> $HOME/.bashrc

Step 1: Open windows command prompt and type "bash"
or open Linux app directly .
Step 2: Type a route which is something like this : /mnt/c/Users/HP/..(You can enter your desired directory here).
For example : /mnt/c/Users/HP/Documents , and by this you will get inside Documents.

For WSL2 Ubuntu the syntax should now match the following example in the json:
"guid": "{2d5ef231-38b7-51cf-b940-2309a097f644}",
"hidden": false,
"name": "Ubuntu",
"source": "Windows.Terminal.Wsl",
"startingDirectory": "//wsl$/Ubuntu/",
"tabTitle": "Ubuntu"
To start in /: "startingDirectory": "//wsl$/Ubuntu/",
To start in /root: "startingDirectory": "//wsl$/Ubuntu/root/",
To start in /home: "startingDirectory": "//wsl$/Ubuntu/home/",

No need to do any of that, just open up the profile for Ubuntu under settings, then update the Command line to add the following option
C:\Windows\system32\wsl.exe -d Ubuntu --cd ~

Related

Can't get WSL2 with Fedora to start in the home directory

I'm on Windows 11 using WSL2 with Fedora 37
I've tried the following with my Fedora username:
"startingDirectory": "//wsl$/fedora/home/<user>"
"startingDirectory": "\\wsl$\fedora\home\<user>"
"startingDirectory": "/home/<user>"
However, all of these still take me to the Windows root rather than ~
If I enter \\wsl$ into File Explorer then I can navigate to \\wsl.localhost\fedora\home\<user>
Sorry, on mobile so will make this short and fill in detail later. Just use a commandLine of:
wsl ~
Don't worry about the Windows Terminal starting directory setting. The above commandLine will override it anyway.

How to create a Linux GUI app short cut for WSL2 on Windows10?

I have properly installed and setup WSL2. It works fine.
I also setup X11 forwarding and X server (VcXsrv). I can launch GUI apps such like konsole or gvim or even google-chrome from a bash shell.
Now I want to launch konsole by simply double clicking a short cut on the desktop without launching the bash command mode terminal. How should I do it?
I tried running this in cmd:
> wsl /usr/bin/konsole
and it reports:
qt.qpa.xcb: could not connect to display
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, xcb.
I'm guessing it is because some X11 forwarding configurations were not properly setup, so I created a k.sh as follows:
#!/usr/bin/bash
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0
export LIBGL_ALWAYS_INDIRECT=1
/usr/bin/konsole &
The first two lines were the X11 settings in my .bashrc, the last line launches konsole.
It works fine under bash environment; but when I ran
wsl k.sh
from windows cmd environment, it silently quitted without launching the konsole.
I'm out of ideas. What should I do to directly launch konsole or other Linux GUI apps under windows without having to getting into bash?
Thanks in advance.
You are asking about two different command-lines, and while the failures in running them via the wsl command have the same root-cause, the underlying failures are likely slightly different.
In both cases, the wsl <command> invocation results in a non-login, non-interactive shell where the command simply "runs and exits".
Since the shell is non-login/non-interactive, your startup files (such as ~/.bashrc and ~/.bash_profile, among others) are not being processed.
When you run:
wsl /usr/bin/konsole
... the DISPLAY variable is not set, since, as you said, you normally set it in your ~/.bashrc.
Try using:
wsl -e bash -lic "/usr/bin/konsole"
That will force bash to run as a login (-l), interactive (-i) shell. The DISPLAY should be set correctly, and it should run konsole.
Note that the quotes probably aren't necessary in this case, but are useful for delineating the commands you are passing to bash. More complicated command-lines can be passed in via the quotes.
As for:
wsl k.sh
That's likely a similar problem. You are doing the right thing by setting DISPLAY in your script, but I notice that you aren't using a fully-qualified path it. This would normally work, of course, if your script is in a directory on the $PATH.
But I'm guessing that you might add that directory to the $PATH in your startup config, which means (again) that it isn't being set in this non-login, non-interactive shell.
As before, try:
wsl -e bash -lic "k.sh"`
You could also use a fully-qualified path, of course.
And, I'm fairly sure you are going to run into an issue with trying to put konsole in the background via the script. When WSL exits, and the bash shell process ends, the child konsole process will terminate as well.
You could get around this with a nohup in the script, but then you also need to redirect the stderr. It's probably easiest just to move the & from the script itself to the command-line. Change your k.sh to:
#!/usr/bin/bash
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0
export LIBGL_ALWAYS_INDIRECT=1
/usr/bin/konsole
Then run it with:
wsl -e bash -lic "k.sh &"`
Finally, a side note that when and if you can upgrade to Windows 11, it will automatically create Windows Start Menu entries for any Linux GUI app you install that creates a .desktop file. You can manually create .desktop files to have WSL create Start menu items for most applications.
For reference, in Windows 11 it's easier. To run a GUI application without a terminal window popping up, you just need to call wslg.exe instead of wsl.exe.
So, for example:
target: C:\Windows\System32\wslg.exe konsole
start in: C:\WINDOWS\system32
shortcut key: None
comment: Konsole
This tutorial shows how to install VcXsrv and and edit .bashrc to ensure that the "DISPLAY env var is updated on every restart".
DISPLAY env var needs to be dynamic setting.
I've used it successfully with WSL2 on Windows10 Version 21H2 (OS build 19044.2130) to run Chrome, Edge, and thunar. I'm using the Ubuntu 20.04 Linux distro.
To edit .bashrc follow these instructions.

What is the difference between calling a command via "wsl [command]" and opening a wsl shell and calling "[command]"?

I am using Ubuntu via WSL 2.0 on Windows 10 and would like to run Texlive from the Windows command line. To do so I prepended the Texlive folder to the path in /etc/environment (I also tried a number of other locations eg. $HOME/.bashrc):
C:\Users\scott\Documents>wsl echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Windows/system32:...
C:\Users\scott\Documents>wsl
scott#SCOTT-PC:/mnt/c/Users/scott/Documents$ echo $PATH
/usr/local/texlive/2020/bin/x86_64-linux:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Windows/system32:...
Why is there a difference between these two paths? Is it possible to change the first PATH variable?
To be honest, when I first looked at this question, I thought it would be an easy answer. Oh how wrong I was. There are a lot of nuances to how this works.
Let's start with the fairly "easy" part, though. The main difference between the first method and the second:
wsl by itself launches into a login (and interactive) shell
the shell launched with wsl echo $PATH is neither a login shell nor an interactive shell
So the first will source both login scripts (e.g. ~/.profile) and interactive startup scripts (e.g. ~/.bashrc). The second form does not get to source either of these.
You can see this a different way (and get to the solution) with the following commands:
wsl -e bash -c 'echo $PATH'
wsl -e bash -li -c 'echo $PATH'
The -li forces bash to run as a login and interactive shell, thus sourcing all of the applicable startup scripts. And, as #bovquier points out in the comments, a single quote is needed here to prevent PowerShell from interpolating the $ before it gets to Bash. That, or escape it.
You should be able to run TeX Live the same way, just replacing the "echo $PATH" with the startup command you need for TeX Live.
A second option would be to create a script that both adds the path and runs the command, and just launch that script through wsl /path/to/script.sh
That said, I honestly don't think that your current login/interactive PATH is coming from /etc/environment. In my testing, at least, /etc/environment has no use in WSL, and that's to be expected. /etc/environment is only sourced by PAM modules, and with no login check performed by WSL, there's no reason to invoke PAM in either the wsl nor the wsl echo $PATH commands.
I'd expect that you still have the PATH setting in ~/.bashrc or somewhere similar), and that's where the shell is picking it up from at the moment.
While this isn't necessarily critical to understanding the answer, you might also wonder, if /etc/environment isn't used for setting the default (non-login, non-interactive) path in WSL, what is? The answer seems to be that it is hard-coded into the init that starts up WSL. That init is also what appends the Windows path (assuming you don't have that feature disabled in /etc/wsl.conf).

How do I get Windows 10 Terminal to launch WSL?

I'm using the new Windows Terminal, and trying to get it to launch my WSL terminal. This is the setting that I'm trying to use:
{
"acrylicOpacity" : 0.75,
"closeOnExit" : true,
"colorScheme" : "Campbell",
"commandline" : "%LOCALAPPDATA%/wsltty/bin/mintty.exe --WSL= --configdir='%APPDATA%/wsltty' -~ ",
"cursorColor" : "#FFFFFF",
"cursorShape" : "bar",
"fontFace" : "Consolas",
"fontSize" : 10,
"guid" : "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"historySize" : 9001,
"icon" : "ms-appx:///ProfileIcons/{0caa0dad-35be-5f56-a8ff-afceeeaa6101}.png",
"name" : "wsl",
"padding" : "0, 0, 0, 0",
"snapOnInput" : true,
"startingDirectory" : "%USERPROFILE%",
"useAcrylic" : true
}
But all it's doing is opening some sort of CMD.
What's the correct command to run the WSL terminal
Edit:
I did notice that the GUID was the same thing as the regular CMD, so I changed that. Then it did launch an external shell.
You need to do following things first.
1. Install Linux (e.g. Ubuntu)
Search "Ubuntu" in the Microsoft store, then buy and install. This is actually WSL (Windows Subsystem for Linux).
Of course, you want to experience other versions of Linux, as well as Debian:
2. Enable WSL permissions
After installing the WSL version of Linux, you also need to enable WSL permissions:
Open another PowerShell window with "Run as Administrator".
Then enter the following command:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
After the command is completed, you can execute the Linux command in the built-out Terminal.
First enter ubuntu in cmd, take a little time to start Ubuntu, set the username and password.
Then you can play with Ubuntu happily. Below I entered a few common commands such as ps, touch, ls etc., as shown below.
3. Change settings
Click "Settings" in right top corner of above image, the file profile.json file will be opened. Then inside the word "profiles" in profile.json file, add below snippet.
{
"guid": "{78e390db-1bff-4533-9d7c-20f53d8bafa1}",
"name": "WSL",
"colorscheme": "Campbell",
"historySize": 9001,
"snapOnInput": true,
"cursorColor": "#FFFFFF",
"cursorShape": "bar",
"commandline": "wsl ~",
"fontFace": "Consolas",
"fontSize": 12,
"acrylicOpacity": 0.75,
"useAcrylic": true,
"closeOnExit": false,
"padding": "0, 0, 0, 0"
}
Near the word "schemes" in profile.json file, you need to update below:
"schemes": [
{
"name": "Campbell",
"foreground": "#A7B191",
"background": "#0C0C0C",
"colors": [
"#0C0C0C",
"#C50F1F",
"#13A10E",
"#C19C00",
"#0037DA",
"#881798",
"#3A96DD",
"#CCCCCC",
"#767676",
"#E74856",
"#16C60C",
"#F9F1A5",
"#3B78FF",
"#B4009E",
"#61D6D6",
"#F2F2F2"
]
}
The complete setting file (profile.json) which can be obtained here.
Actually, the WSL here is Ubuntu.
4. Add icons to different types of tabs
You can add icons for Tab to this location:
%LOCALAPPDATA%\packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\RoamingState
I put some 32x32 PNG in this folder, and then in profile.json I can reference the image resource with the path starting with ms-appdata:// .
The icon is available here:
Icons
Then replace the contents of the new profile-withIcons.json file below with the previous settings.
profile-withIcons.json
After finishing the contents of the folder is like this:
The final result is:
To launch any distribution, you can also use this for commandline:
wsl.exe -d <name_of_the_distribution>
Like :
wsl.exe -d Ubuntu-18.04
You can list all distributions with :
wsl.exe -l
.
PS : Tux icon for Linux :
ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png
Turns out that all I needed to do was change the commandline to ubuntu1804.exe. Like this:
{
"acrylicOpacity" : 0.75,
"closeOnExit" : true,
"colorScheme" : "Campbell",
"commandline": "ubuntu1804.exe",
"cursorColor" : "#FFFFFF",
"cursorShape" : "bar",
"fontFace" : "Consolas",
"fontSize" : 10,
"guid" : "{0caa0dad-35be-5f56-a8ff-abceeeaa6101}",
"historySize" : 9001,
"icon" : "ms-appx:///ProfileIcons/{0caa0dad-35be-5f56-a8ff-afceeeaa6101}.png",
"name" : "wsl",
"padding" : "0, 0, 0, 0",
"snapOnInput" : true,
"startingDirectory" : "%USERPROFILE%",
"useAcrylic" : false
}
While my answer is off-question (since answered by the O.P.) I found this question while searching for adding a Windows Terminal (WT) profile for my recent Ubuntu installation, since I had recently re-imaged my laptop.
Updated 19/10/03: Installation order does not matter. My profile for “ubuntu” appears after installing ubuntu, and then launching the ubuntu instance in PowerShell and establish my user account and password. THEN Windows Terminal adds the appropriate shell profile for WSL.
I have developed a tool for tweaking the terminal configurations here :
Windows Terminal Tweaker 🚀⚡
It lets you add different profiles , gives color pickers to choose color schemes and provides options to tweak every single thing while applying the settings live.
Open settings
Change the "Default Profile" in the drop down.
Click "Save" on the bottom right.
Create a new profile in the windows terminal settings and select the following command line
C:\Windows\system32\wsl.exe -d Ubuntu-20.04
where -d selects the distribution to be run.
Windows Terminal allows you to open the settings and change things there. If you already have ubuntu installed, it should be an option to set the ubuntu profile as your default config.
Copy paste the guid for ubuntu into the defaultProfile and it will automatically launch WSL ubuntu instead of powershell by default.
The premise of the question was fouled by the mistake with the GUID, as the OP says the sample code was actually correct. So, the question could be re-interpreted as--
What is the minimum required change to the default settings.json to point to an application of your choosing? (which is what got me here)
guid - has to be unique. I've had success with changing just the last number for each customization.
commandline - From within Bravo Yeung's answer is a link to a sample settings.json file: "commandline": "wsl ~"
This little bitty value is made possible by setting a default distro: "The default WSL distribution is the one that runs when you run wsl on a command line:"
wsl --setdefault <DistributionName>
However, seems commandline:wsl doesn't work with startingDirectory.
I've successfully omitted commandline in favor of source + startingDirectory:
// Make changes here to the cmd.exe profile.
"guid": "{long-guid-here}",
"hidden": false,
"name": "Debian",
"tabTitle" : "WSL (Debian)",
"source": "Windows.Terminal.Wsl",
"startingDirectory" : "C:\\Users\\myuser"
Here is a good link which got me started the first time I installed Windows Terminal and customized the settings, Easily add Anaconda Prompt to Windows Terminal to make life better
Recent versions of Windows Terminal include support for Dynamic Profiles, in which new WSL distros will automatically be added to the list of available profiles.
However, if Windows Terminal is running when you install the new distro, it will not currently get added while running. You must exit and re-start Windows Terminal, and the new profile will automatically appear.
Once it is added, you can edit/move it using the Settings dialog or by editing settings.json as usual. Each WSL distro is identified using a GUID which will have been auto-populated.
As described in the linked article, Dynamic Profiles can be turned off by disabling their source(s) such as the following in settings.json:
"disabledProfileSources": ["Windows.Terminal.Wsl", "Windows.Terminal.Azure", "Windows.Terminal.PowershellCore"]

LiteIDE no autocomplete

I'm trying to use LiteIDE (the Go IDE) on Linux 32-bit. Everything works except for autocomplete. Builds, running, everything works. The gocode binary seems to be running tho:
ithisa#miyasa ~> ps aux | grep gocode
ithisa 10003 0.0 0.0 823788 2624 pts/1 Sl+ 09:06 0:00 /home/ithisa/scratch/liteide/bin/gocode -s -sock unix -addr localhost:37373
What might I be doing wrong?
You may need to set a GOROOT=. To set it within LiteIDE, look for the environment toolbar; it should be a a dropdown, probably with "system" preselected, and a button. Click the button to bring up the Edit Environment pane, then double-click "system.env", or whichever environment was picked in the dropdown.
Change the line that starts GOROOT= to point to your 'go' directory. Plain old $HOME/go is a common setting if you installed it from golang.org, and if you don't know where it is, running go env will show the GOROOT that the Go toolchain itself is using. And of course if the line is commented out (#GOROOT=...) remove the #. Save.
If the toolbar is missing entirely, View -> Environment toolbar unhides it.
It's probably also worth setting GOROOT and related settings in your .bashrc, so tools started from the command line see it. I installed Go and LiteIDE in my homedir and my workspace is ~/gocode, so mine is like:
export PATH="$HOME/go/bin:$HOME/liteide/bin:$PATH"
export GOROOT=$HOME/go
export GOPATH=$HOME/gocode
I can't be certain this is actually your issue, but if I unset my GOROOT the symptom matches what you're seeing: completion works on my code, but not on the standard library. Good luck!
Did you install gocode?
https://github.com/nsf/gocode
Also, does nothing autocomplete or just new packages? Packages need to be installed to autocomplete. Do you have a standard install setup?
Your GOROOT and GOPATH should also be correctly setup.
I've got the exact same problem, except for 64-bit linux (ArchLinux)
I got this solved by:
set up correct GOROOT and GOPATH, for example:
$ cat ~/.bashrc | grep GO
export GOROOT=/usr/lib/go
export GOPATH=~/goroot
PATH="$PATH:$GOPATH/bin"
bash
installing/starting gocode daemon
$ go get -u github.com/nsf/gocode
$ gocode -addr=:37373
$ gocode status
set correct GOROOT on LiteIDE config file:
sudo vim /usr/share/liteide/liteenv/linux64.env
GOROOT=/usr/lib/go
For me gocode (autocomplete) broke in LiteIDE after updating Go to the latest version.
What I did was make sure GOPATH was set correct. Then install gocode:
go get -u github.com/nsf/gocode
Then remove the gocode version from the liteide/bin/ folder, because else LiteIDE will use its own version (I only renamed it just in case).
Now when you boot LiteIDE it should say
GolangCode: Found gocode at <YOUR GOPATH>/bin/gocode
instead of LiteIDE using its own version.