How to change default directory in Windows Subsystem for Linux
Asked Answered
D

15

33

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?

Dabble answered 26/11, 2019 at 18:57 Comment(1)
This question is being discussed in meta: meta.#413702Generatrix
U
44

You should only change the startingDirectory for WSL (Ubuntu in this case) terminal sessions.

  1. Open settings.json via CTRL+SHIFT+, in Windows Terminal1
  2. 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

1 If you need to know how or where to edit Windows Terminal settings/preferences: https://learn.microsoft.com/en-us/windows/terminal/get-started

Utmost answered 26/2, 2021 at 6:28 Comment(6)
While this answer is still valid, Windows Terminal now has a nice dialog in which you can edit these options directly. For the windows home folder, you may use: %USERPROFILE%. For advanced settings, such as definition of commands, I still prefer this method. E.g., as described in command palette documentation.Edenedens
I think this answer is missing something since I installed WSL2 and there is no Windows Terminal installed in the system. And I really don't want to install it just to fix this problem. So this fix does not work for me =/. Being more precise: Using CTRL + Shift + , does nothing on my WSL terminal, and searching on windows for "Windows Terminal" yields no results.Stinkhorn
@Stinkhorn the OP was specific to the application Windows Terminal.Utmost
@Edenedens I am glad to hear there is an interface to configure this, I have moved back to the *nix world, so would not be able to answer my question. Perhaps you should post an answer?Utmost
@Utmost good suggestion. Should I add a new answer or edit/extend yours to include a description of how to edit settings if Windows Terminal is installed as well?Edenedens
For Debian and WSL2, setting \\\\wsl$ stuff didn't work and I had to set it as /home/usernameGoda
T
24

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.

Tribasic answered 27/11, 2021 at 14:52 Comment(2)
Your snippet appears to be json, where is this json file?Arneson
@justin, see the currently highest voted answer. If you need to know how or where to edit Windows Terminal settings/preferences: learn.microsoft.com/en-us/windows/terminal/get-startedFigwort
B
11

Changing the home directory with WSL is done the same way as in Linux:

  1. Enter bash
  2. Type the command sudo vim /etc/passwd
  3. Find your account's line, which might look like:
    shadyar:x:1000:1000:"",,,:/home/shadyar:/bin/bash
  4. 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
  5. Save the file and exit vim by typing :wq and press Enter
  6. Exit bash and re-launch it
  7. To test, use the commands:
    cd ~
    pwd
Belostok answered 23/3, 2021 at 11:33 Comment(1)
Although this doesn't answer the OP question, it does answer the title question which I found very helpful. Thx! This file can be edited from Win explorer in your Linux directory under etc/passwd, removing steps. If you have no user (nologin) you can edit root instead: root:x:0:0:root:/root:/bin/bash => root:x:0:0:root:/srv:/bin/bash . . . for instance to change start directory to /srv/.Mavilia
G
8

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.

Gareth answered 27/11, 2021 at 15:12 Comment(6)
It's a JSON file, don't comment with # but // or /* ... */ instead. ;)Snaffle
@Snaffle You're absolutely right. Not sure why I did that -- Probably just muscle memory from too many shell scripts. Fixed! Thanks!Gareth
JSON does not support comments. If you leave that in the file, it will probably break. Unless the system is using a very forgiving parser.Phytophagous
@MaxWilder While that's correct for most JSON files, I wouldn't be too worried about that in this case -- At the time I wrote that, Microsoft themselves commented the Windows Terminal settings.json, so they are/were clearly parsing it. Nowaways, they strip the commented lines when you save from the GUI. While they could switch to a parser in the future that doesn't handle it, they'll still have to check for comments and strip them to support upgrading from their own, commented settings.json from older version.Gareth
What settings.json file? Where is this file?Arneson
@Arneson Note the second paragraph -- You don't need to edit settings.json directly any longer -- That was for older versions of Windows Terminal. Nowadays, just use the Windows Terminal "Settings" GUI and change the "Command Line" argument for the corresponding Profile. I'll edit to try to make that more clear.Gareth
O
5

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

and then: enter image description here

Overbearing answered 9/5, 2022 at 7:44 Comment(0)
A
3

https://learn.microsoft.com/en-us/windows/wsl/basic-commands#change-directory-to-home

Simply run

wsl ~

To open your distro. I know there are various answers to this which have already been posted but I find this to be the simplest.

Araby answered 24/12, 2023 at 15:25 Comment(0)
P
1

startingDirectory Should be a windows path, not a nix path. Try D:\Folder\SubFolder instead

refer this link,worked for me

github

Pantile answered 14/8, 2020 at 7:12 Comment(2)
Yeah, I'm pretty sure I tried that before I posted, but I went ahead and tried again, and it doesn't seem to work. For reference, this is what I tried it with in my settings.json "startingDirectory": "C:\\Users\\<user>\\AppData\\Local\\Packages\\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\\LocalState\\rootfs\\home\\<user>" What was the exact path you used? Maybe I'm using the wrong one?Dabble
For example, \\\\wsl$\Ubuntu\home\\rich\\ opens my Ubuntu home folder! 😁Yearbook
T
1

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 ~
Thermopylae answered 10/4, 2022 at 9:43 Comment(1)
This is the only one that did what I wanted. ChatGPT told me something similar but had a space after -- which stopped it working.Partnership
K
0

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.

Katrinka answered 1/7, 2021 at 7:14 Comment(0)
P
0

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/",
Poundfoolish answered 17/11, 2021 at 1:24 Comment(0)
C
0

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
Cathay answered 22/9, 2022 at 12:37 Comment(2)
Unfortunately this thread is old and Microsoft has changed/added different/new ways of setting your default path since the original post. However, I'd venture to say none of the purposed solutions worked because it's suggested against referencing or using resources outside your WSL installations from within WSL. It would most likely work in most cases, but mixing and matching WSL paths/apps and Windows paths/apps opens the door for unpredictable behavior.Dabble
Also, /home/${my_username} is your current user directory when in Ubuntu WSL. I'd propose asking yourself, why are you using WSL, then ask, why must these files be in the Windows file structure. Most likely, your answers will lead you to either move those files into your WSL install, or to realize WSL isn't the proper tool for what you're trying to do.Dabble
L
0

If you ever changed default setting after typing this command sudo vim /etc/passwd and then the Ubuntu shell fail to start and throw this error: ' [process exited with code 1 (0x00000001)]' like this. Then you have to reset the default setting.

To re-edit this file again (since Ubuntu shell won't open) use C:\Windows\System32\bash this command in the classic command line of windows to access it.

Lilianaliliane answered 5/9, 2023 at 18:1 Comment(0)
G
0

sudo apt-get install --reinstall package-name

i encountered same issue , force install the package worked for me

Gastrolith answered 23/6, 2024 at 11:50 Comment(0)
O
0

You can add in ~/.bashrc (or whatever bash that you use) the command cd ~

Use the command:

echo "cd ~" >> ~/.bashrc.

Now you can refresh the bash, use the command:

source ~/.bashrc

In any terminal that you open the wsl the linux always will go to home (~) directory (You can specify what directory you like to init).

The code above will redirect you always that you use "source ~/.bashrc". If you want to avoid this behavior you can add this piece of code in your ~/.bashrc:

if [ ! "$BASH_INITIATED" = "true" ]; then
    echo "Hello $(whoami), the bash was initiated!"
    cd ~
    export BASH_INITIATED="true"
else
    echo "Reloading the bash ..."
fi

When the linux initiate the bash will define the "BASH_INITIATED" variable with "true" and if you reload the .bashrc the directory not will change.

Note: remove the "echo" statements if you no want see any message at start or reload!

Outofdate answered 27/7, 2024 at 22:21 Comment(0)
A
-1

you can also use:

echo cd ~ >>~/.bashrc

and each time you start wsl you will be placed and your home directory if you use bash

Autry answered 27/4, 2023 at 8:3 Comment(2)
This forces ~ upon every new bash shell, thus is not only applicable for WSL on start.Hance
Yes, it's the idea. I agree, it's just a shortcut.Autry

© 2022 - 2025 — McMap. All rights reserved.