How to Re-login to WSL Ubuntu on Windows 10

Ubuntuwindows 10windows-subsystem-for-linuxwsl2

I have Ubuntu-22.04 installed in WSL2.

I changed /etc/environment and, as this answer suggests, I need to log out and log in Ubuntu
for the changes to take place. But I'm unable to do so as WSL just seems to save its state whatever I do.

I tried:

  1. wsl --shutdown.
  2. Restart-Service LxssManager.
  3. Restart Windows.

None of the above worked.

I noticed that in older versions WSL Ubuntu sometimes listed some additional information on login (greeting, current time, etc.) — I figure, it does that on login, but I never see this info now, it just starts with an empty shell line, so, I guess, it never logs me in just keeping the same session somehow.

So, how can I force it to re-login?

Best Answer

I was thinking last night, as I was deep into researching where the path is set when using Systemd (of course, /etc/environment by default), that it would be ironic to get a related question on the topic today. And look - Here it is!

The problem you are seeing isn't that WSL doesn't "shut down" (it does), or even that it "saves state" (it doesn't). The problem is simply that /etc/environment doesn't (normally) get processed on WSL.

This is because /etc/environment is a PAM construct -- It's typically read into the environment by pam_env.so during login.

However, as you've probably noticed, there's no real concept of a "login" in WSL. It never asks for your username or password, as the real security comes from your Windows account and permissions.

You can force a login that invokes PAM (and thus reads /etc/environment) with something like:

sudo su - $USER

You might want to just set the variables in either:

  • ~/.bashrc -- If you only need them for something in an interactive session
  • ~/.bash_profile -- If you need them for all sessions under the "login" shell.
  • Or another config file if you are using a different shell.

/etc/environment is really meant for variables that should be set for all users in a multi-user system. WSL is really designed with a single default, developer user in mind.

Ubuntu sometimes listed some additional information on login (greeting, current time, etc.) ..., but I never see this info now

That's the message-of-the-day functionality, and it should typically only display once a day. Just wait until tomorrow, and you'll probably see it again.

Related Question