Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 972
d.lanza38 Avatar asked Nov 26 '19 18:11

d.lanza38


2 Answers

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

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

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

like image 50
Duncanmoo Avatar answered Oct 11 '22 17:10

Duncanmoo


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.

like image 21
TomBoland Avatar answered Oct 11 '22 17:10

TomBoland