Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unattended Install #90

Closed
kousu opened this issue Mar 20, 2021 · 3 comments
Closed

Unattended Install #90

kousu opened this issue Mar 20, 2021 · 3 comments

Comments

@kousu
Copy link

kousu commented Mar 20, 2021

I want to be able to do an unattended install as if from the Microsoft Store. There is install --root, which you document prominently:

ubuntu1804.exe install --root

and this is what is used by

https://github.com/microsoft/windows-dev-box-setup-scripts/blob/a0d80679847fc668bdc1706ae29b45da7a65772b/scripts/WSL.ps1#L10

but that team knows they should be installing with a regular user, but declined to explain fully how to actually to implement https://github.com/microsoft/windows-dev-box-setup-scripts/blob/d488050/README.md#how-to-run-the-scripts

If you are using WSL there's a followup step we recommend after running the setup script. When the script finishes you will only have a root user with a blank password. You should manually create a non-root user via $ sudo adduser [USERNAME] sudo with a non-blank password. Use this user going forward. For more info on WSL please refer to the documentation.

From microsoft/WSL#3369 I learned that I make an almost fully-automatic user account by:

Ubuntu1804 install --root
Ubuntu1804 run apt update
Ubuntu1804 run apt upgrade -y

Ubuntu1804 run adduser -m user
Ubuntu1804 run 'echo user:password | chpasswd'
Ubuntu1804 run 'chsh -s /bin/bash user'

# change default user to 'user'
reg.exe query HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\ # to get the GUID of the container
reg.exe add 'HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\{1422a095-2e43-4bf8-8108-d209cabe2a07}' /v DefaultUid /t REG_DWORD /d 1000 /f

But this is really fragile:

  1. It requires parsing out a GUID from the registry; and even if I did script that, it would probably break down as soon as a second distro was installed
  2. It just guesses that 'user' == UID 1000

I wish this section of code was just slightly different:

// Create a user account.
if (createUser) {
Helpers::PrintMessage(MSG_CREATE_USER_PROMPT);
std::wstring userName;
do {
userName = Helpers::GetUserInput(MSG_ENTER_USERNAME, 32);
} while (!DistributionInfo::CreateUser(userName));
// Set this user account as the default.
hr = SetDefaultUser(userName);
if (FAILED(hr)) {
return hr;
}
}

or maybe this one:

std::wstring commandLine = L"/usr/sbin/adduser --quiet --gecos '' ";
commandLine += userName;
HRESULT hr = g_wslApi.WslLaunchInteractive(commandLine.c_str(), true, &exitCode);

Could there be a --unattended or --default-user flag that does a clean unattended install? Or, a wsl set-user that would let me change the default user after initially installing with root?

@kousu
Copy link
Author

kousu commented Mar 21, 2021

Ah, I read the docs: there's

wsl -u root

which partially helps. If, say, you generalized --root to --user which worked so that, given --user someone, DistroLauncher runs useradd -m someone instead of using adduser then it won't prompt, and I can later use wsl -u root passwd someone to set the password, or echo 'someone:pass' | wsl -u root chpasswd to set it noninteractively.

@kousu
Copy link
Author

kousu commented Mar 21, 2021

..and now I just noticed ubuntu1804 config --default-user username. Which pretty much covers everything I want.

DistroLauncher could make this smoother maybe with more obvious flags, but this works fine:

<distro>.exe install --root

wsl -u root -d <distro> useradd -m "$username"
wsl -u root -d <distro> sh -c "echo "${username}:${password}" | chpasswd"
wsl -u root -d <distro> chsh -s /bin/bash "$username"
wsl -u root -d <distro> usermod -aG adm,cdrom,sudo,dip,plugdev "$username"

<distro>.exe config --default-user "$username"

@kousu kousu closed this as completed Mar 21, 2021
@kousu
Copy link
Author

kousu commented Mar 21, 2021

Or, this is even better:

Given that you've grabbted the .appx link for a distro:

$distro = "ubuntu1804"
$distro_link = "https://aka.ms/wsl-ubuntu-1804"

$username = "ubuntu"
$password = "ubuntu"

Invoke-WebRequest -Uri $distro_link -OutFile ~/${distro}.appx -UseBasicParsing
Add-AppxPackage -Path ~/${distro}.appx


& $distro install --root

& $distro run useradd -m "$username"
& $distro run sh -c "echo "${username}:${password}" | chpasswd"
& $distro run chsh -s /bin/bash "$username"
& $distro run usermod -aG adm,cdrom,sudo,dip,plugdev "$username"

& $distro config --default-user "$username"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant