d.lanza38
d.lanza38

Reputation: 2617

How to change default directory in 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?

Upvotes: 35

Views: 113765

Answers (16)

zeitgeist
zeitgeist

Reputation: 1032

None of the solutions here worked for me. I am sharing what actually worked for me perfectly. There are two ways to change the starting directory of wsl Ubuntu. Here the starting directory will be UbuntuStuff folder in c drive which is located in wsl at /mnt/c/UbuntuStuff.

  1. Open Settings in Windows Terminal and change the Command line option for Ubuntu. enter image description here

  2. Open settings.json via CTRL+SHIFT+, in Windows Terminal.

         {
             "commandline": "wsl.exe -d Ubuntu -e bash -c \"cd /mnt/c/UbuntuStuff; exec bash\"",
             "guid": "{51855cb2-8cce-5362-8f54-464b92b32386}",
             "hidden": false,
             "name": "Ubuntu",
             "source": "CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc",
             "startingDirectory": null
         }
    

Now whenever you open a new Ubuntu tab you will start in the right directory. enter image description here

Upvotes: 0

airtonix
airtonix

Reputation: 5154

in ~/Documents/PowerShell/Microsoft.PowerShell_profile.ps1 put:

Function WslInHome {
    wsl.exe --cd ~ -- $args
}
Set-Alias -Name wsl -Value WslInHome

using it

C:\
> wsl

~
>

or


C:\
> wsl uname -r
5.15.153.1-microsoft-standard-WSL2

C:\
>

Why this doesn't cause infinite recursion:

  1. aliases win over cmds in $env:PATH
  2. our alias function runs wsl.exe, not wsl

About powershell profiles:

You can create a PowerShell profile to customize your environment and add session-specific elements to every PowerShell session that you start.

About powershell profile locations:

The PowerShell console supports the following basic profile files. These file paths are the default locations.

All Users, All Hosts
    Windows - $PSHOME\Profile.ps1
    Linux - /opt/microsoft/powershell/7/profile.ps1
    macOS - /usr/local/microsoft/powershell/7/profile.ps1
All Users, Current Host
    Windows - $PSHOME\Microsoft.PowerShell_profile.ps1
    Linux - /opt/microsoft/powershell/7/Microsoft.PowerShell_profile.ps1
    macOS - /usr/local/microsoft/powershell/7/Microsoft.PowerShell_profile.ps1
Current User, All Hosts
    Windows - $HOME\Documents\PowerShell\Profile.ps1
    Linux - ~/.config/powershell/profile.ps1
    macOS - ~/.config/powershell/profile.ps1
Current user, Current Host
    Windows - $HOME\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
    Linux - ~/.config/powershell/Microsoft.PowerShell_profile.ps1
    macOS - ~/.config/powershell/Microsoft.PowerShell_profile.ps1

More info can be discovered https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.4

Upvotes: 0

Santosh Padavala
Santosh Padavala

Reputation: 113

sudo apt-get install --reinstall package-name

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

Upvotes: -1

warsong
warsong

Reputation: 1038

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.

Upvotes: 3

hassan hejazi
hassan hejazi

Reputation: 149

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.

Upvotes: 0

MrCopro76
MrCopro76

Reputation: 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

Upvotes: -2

Duncanmoo
Duncanmoo

Reputation: 4171

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

Upvotes: 45

Aigbe Destiny
Aigbe Destiny

Reputation: 58

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

Upvotes: 0

NotTheDr01ds
NotTheDr01ds

Reputation: 20629

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.

Upvotes: 8

Balu
Balu

Reputation: 691

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

Upvotes: 5

Kevin Davies
Kevin Davies

Reputation: 323

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 ~

Upvotes: 1

TomBoland
TomBoland

Reputation: 349

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.

Upvotes: 24

opi
opi

Reputation: 9

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/",

Upvotes: 0

Pundreek Saurabh
Pundreek Saurabh

Reputation: 29

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.

Upvotes: 0

Shadyar
Shadyar

Reputation: 825

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

Upvotes: 12

Yashwanth Kambala
Yashwanth Kambala

Reputation: 432

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

refer this link,worked for me

github

Upvotes: 1

Related Questions