Changing home directory of user on Windows Subsystem for Linux

27

14

I'd like to change the home directory of the user that I initially setup on Windows Subsystem for Linux. I found this command

sudo usermod -d /mnt/c/Users/Alex alex

Which should allow this, however I am getting this error

usermod: user alex is currently used by process 2

Presumably because I am logged in as that user. I've tried going to root by doing sudo su - and then running the command again but I get the same issue. I tried a command I found on Google to log out the user alex from root but that also kicked me out of bash entirely.

Any thoughts?

Xeon06

Posted 2016-10-07T22:14:24.473

Reputation: 505

Can you not logout of that account, login as another account, and then run the commands to see what result you get? – Pimp Juice IT – 2016-10-11T20:53:06.517

Logging out of that account closes Linux bash and gets you back to Windows prompt. Logging back in just puts you by default in that user. And switching to root through sudo su - I still get the process error – Xeon06 – 2016-10-11T21:25:58.010

I see, I assume you cannot log into the Windows as another account while logged out of that one, and then run the bash command then from that account in bash for the other account? I've not setup bash within Windows yet so just thinking of potential simple solutions even though having to do that would be sort of inconvenient but if it's just a one-time deal then done, maybe it'd work. Quick thoughts only though. – Pimp Juice IT – 2016-10-11T23:25:01.503

Unfortunately the bash environment is tied to the current user Windows, stored in %appdata%. So another user will get their own bash environment – Xeon06 – 2016-10-12T14:43:43.473

You can change the default user name by executing in Windows (not bash) lxrun /setdefaultuser myusername. – harrymc – 2016-10-12T20:23:06.177

@harrymc I'm trying to change the home directory and not the username, but perhaps there's another such lxrun command for that? – Xeon06 – 2016-10-13T14:31:51.530

Answers

42

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:
    harry:x:1000:1000:"",,,:/home/harry:/bin/bash
  4. Change the home directory, which above is /home/harry, to the new directory, using WSL notation
  5. Save the file
  6. Exit bash and re-launch it
  7. To test, use the commands:
    cd ~
    pwd

Let me know if you need more information.

harrymc

Posted 2016-10-07T22:14:24.473

Reputation: 306 093

I don't know WSL, I'm just curious: in general is it safe to just vim /etc/passwd? In Linux we have vipw especially for the job. – Kamil Maciorowski – 2016-10-13T19:55:01.113

@KamilMaciorowski: I have just started with WSL, but I can say that sudo /usr/sbin/vipw does exactly the same as my point 2 above. Changing /etc/passwd is as safe as doing it on Linux. Maybe safer because destroying it does not affect Windows logon. – harrymc – 2016-10-13T20:00:57.773

Thank you! I don't know why the only way I could find to edit this was through usermod. – Xeon06 – 2016-10-13T22:02:19.910

4Be careful with this - if you move out of the main fake linux filesystem, you'll find you can't edit file permissions properly. – Hippyjim – 2017-05-12T15:04:06.173

@Hippyjim I'm facing the same issue and find out that this SO anwser could help (requires Win 10 Insider Build 17063 and above).

– Weekend – 2019-07-14T11:55:32.267

1

You can try running lxrun /setdefaultuser root in command prompt, then change the directory, then change the default user to Alex.

Joshua Merrill

Posted 2016-10-07T22:14:24.473

Reputation: 21