Score:0

Creating a new top level Admin User for Ubuntu on WSL

cn flag

Swapping standard Ubuntu for win-10 w/ "WSL Ubuntu"

For 5 years I booted to Ubuntu Distros with a GUI desktop, however; this year, my professor has forced me to add Windows to my machine. In the past I would dual boot the two, however, as I am sure most of you are likely to already know, several Linux distros have been made available for use by developers working in "Windows 10" environments", therefore, I decided to set up WSL rather than dual booting.

The Status of my Machine's Operating System

So I was setting up WSL, and referring to the docs here Official Ubuntu page on WSL whenever I had trouble. I couldn't log into Ubuntu for whatever reason, however WSL & Ubuntu seem to be working fine, so I just used the worlds most famous Linux work around, and logged in as root (which required me to set the default user to root). The problem is, I can't get the initial prompt back to enter a username & password, which, from what I understand, is how a new user is properly created for the virtual WSL Ubuntu instance.

In-depth Explanation of the Question at Hand

I have never done this, so I don't know what a proper default sudo/admin account should look like on an "Ubuntu Operating System" that runs in a Windows-10-subsystem. Its it necessary that I get the prompt back, or can I set up a new user on my own? If I do set up a new user on my own then, what is the correct way to configure the top-level Admin account (aka Sudo-account) for a WSL instance of Ubuntu.

Things about the configuration that are not exactly clear to me include:

  1. Should the account be a system account, and should it use an arbitrary name assigned by ubuntu?
  2. Should the account have a home directory?
  3. Is the sudo-group admin:sudo the only group that the top-security-level admin account (aka Sudo-account) needs to be added to, should I make another group with the same name as the Admin account I am creating?
NotTheDr01ds avatar
vn flag
Also, if you like the full Ubuntu desktop experience, note that Windows 11 will include WSLg with the ability to run graphical Linux apps (fairly seamlessly, in my understanding). In the meantime, see [this answer](https://stackoverflow.com/a/68856351/11810933) if you'd like to be able to access a full desktop environment from within WSL through `xrdp` in Windows.
muru avatar
us flag
Quote formatting and code formatting have specific semantic meanings (e.g., screen readers and other such accessibility software can treat them differently). Do not misuse them just for "looks".
NotTheDr01ds avatar
vn flag
JAY-DEV I completely agree with @muru here, and recommend not getting into an editing war with him or Zanna ;-). While I understand your desire for formatting, the edits are absolutely clean, concise, and semantically correct.
JΛYÐΞV avatar
cn flag
The meanings are loose at best, if you don't like it then flag the question.
Score:4
vn flag

The problem is, I can't get the initial prompt back to enter a username & password, which, from what I understand, is how a new user is properly created for the virtual WSL Ubuntu instance.

Right. The initial user creation is done when the distribution executable (the .exe that is installed from the Microsoft Store) is run for the first time. In the case of Ubuntu, that's typically ubuntu.exe, although it can differ depending on if you installed a "named version" in the Store (e.g. Ubuntu 20.04 specifically). That said, it is entirely possible to create a default user manually (see below).

Let's start with wsl -l -v to see what the distribution name is. It will most likely be just "Ubuntu", but modify the below commands if it is different.


The "easy" button

It's possible to get back to the initial "configuration" state, with the big assumption and caveat that doing so will erase all data/configuration in the instance (the virtual filesystem, etc.). That command is wsl --unregister Ubuntu.

After doing that, you should simply be able to re-run "Ubuntu" from the Start Menu, and the distribution will be recreated. It will then ask you for the default username/password.


Running as root, without the need for sudo

A neat trick that you will probably find useful is that WSL can be started with any available user, including root, without the need for a password:

wsl ~ -u root
wsl ~ -u fred

While you might initially be alarmed by the ability to run root without a password, keep in mind that, even as root, your permissions in WSL can never exceed your Windows user permissions. That said, please never do this.


Sidebar: Your questions

First, to answer some of your questions:

Should the account be a system account, and should it use an arbitrary name assigned by Ubuntu?

Not sure what you mean here exactly, but the answer, I believe, is "no". The default user can be named whatever you like. I recommend just using your normally preferred user name for convenience.

Should the account have a home directory?

Yes, and that home directory should be in /home, not on a Windows mounted drive (e.g. /mnt/c).

Is the sudo-group admin:sudo the only group that the top-security-level admin account (aka Sudo-account) needs to be added to, should I make another group with the same name as the Admin account I am creating?

The default Ubuntu method (both "pure Linux" and "WSL") is to create a group with the same name as the username. While perhaps not required, I would consider that a "best-practice".


Creating a new "default" WSL user without reinstalling

As far as I can tell, the following will create the default user with the same settings that the stock installer/configuration does. From within Ubuntu:

useradd --create-home --user-group --groups  adm,dialout,cdrom,floppy,sudo,audio,dip,video,plugdev,netdev --password "encryptedPassword" username

Of course, modifying username to be whatever you want. Since you are already running as root, no need for sudo or any trickery there, of course, but other readers without the OP's use-case may need sudo or wsl -u root (as documented in this answer).

Of course, most of these groups probably aren't strictly necessary under WSL, but who knows - Some apps may expect some of them, and better safe than sorry. Again, this is the "stock" group list that is provided for the user when created by the Ubuntu/WSL configurator.

See here for how to create the encrypted password. Specifically (although personally untested for this exact command-line):

useradd --create-home --user-group --groups  adm,dialout,cdrom,floppy,sudo,audio,dip,video,plugdev,netdev --password $(read -sp Password: pw ; echo $pw | openssl passwd -1 -stdin) username

Setting the default WSL user

"Normally", the WSL distro installer (e.g. ubuntu.exe) modifies the Windows Registry with the default user name for the distro. I recommend a newer method, though. As root, create a file /etc/wsl.conf with the following contents:

[user]
default=username

Again, of course, substituting your username.

For alternative techniques, see this question and its various answers.


Recommended next steps

I personally recommend backing up this "ready-to-run" copy of your distribution at this stage. Again, from PowerShell:

wsl --export Ubuntu "2021-08-26 Default Ubuntu 20.04.tar"

If you need to spin up a throwaway WSL instance for testing something out without modifying your "daily driver", you can easily wsl --import this distribution (calling it something other than just "Ubuntu"), then throw it away with wsl --unregister.

JΛYÐΞV avatar
cn flag
__`1`__***`UP`***__``__ This is a fantastic answer — it demonstrates that you clearly care about the question that you are answering, and the community to which you, I, the question & this answer all belong. If you were to iterate through my Stack Overflow account, you would find that I too thoroughly answer questions in very much the same way. I rarley ask a question, typically I can find a solution when I get stuck using the resources I have at my disposal, but when I have asked questions in the past, not one of them received an answer that was even close to the quality of this answer.
NotTheDr01ds avatar
vn flag
@JAY-DEV Thanks for the kind words, and glad I could help. Even without reading some of your answers (which I plan to do), the time you took to format the question for clarity with headings/etc. showed a level of organization and communication skills well above par. Kind of reminds me of the mandatory "Intro to Communications" class they give before an MBA. Return kudos to you as well, and welcome to WSL! :-)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.