대본
powershell을 사용하여 WSL Ubuntu 18.04를 자동으로 설치하고 초기화하기 위해 첫 번째 사용자 이름과 비밀번호를 자동으로 초기화/설정하려고 합니다. 그러나 powershell 명령에서 wsl을 처음 실행하면 powershell은 사용자가 수동으로 암호를 입력할 때까지 기다리는 wsl의 셸로 이동합니다.
MWE
다음 MWE에 포함된 wsl 초기화 후 입력 프롬프트에 사용자 이름, 비밀번호(및 다시 비밀번호)를 파이프하는 5번의 다른 시도가 있습니다.
##############Required for MWE###################
# Enable wsl subsystems for linux (if powershell is ran in admin mode)
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
# Set Tls12 protocol to be able to download the wsl application
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# check to see if ubuntu1804 installation file exists and download the app otherwise
$fileToCheck = "Ubuntu1804.appx"
if (Test-Path $fileToCheck -PathType leaf)
{"File does Exist"}
else
{Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile Ubuntu1804.appx -UseBasicParsing}
# Actually install the wsl ubuntu 18.04 app
Add-AppxPackage .\Ubuntu1804.appx
Write-Output "Installed the ubuntu18.04"
# backup installation command if the first command did not function properly
invoke-expression -Command "Add-AppxPackage .\Ubuntu1804.appx"
Write-Output "Installed the ubuntu with backup attempt"
##############Actual attempts to initialize ubuntu without prompting for user input###################
Write-Host "Trying to initialize ubuntu"
# Attempt 0: makes it start installing the wsl but hangs prompting user name
#Write-Host "wsl whoami"
# Attempt 0 conclusion: Starts installing the wsl but then waits on user input
# Attempt 0.1: So would like to pipe a "password | password | username | whoami" in there but that does not work.
#Write-Host "wsl 'somepassword | somepassword | someusername | whoami'"
#Write-Host "wsl somepassword | somepassword | someusername | whoami"
# Attempt 0.1 conclusion: doesn't work, still dives into the wsl shell and waits on user input
# Attempt 1: does not make it start installing
#$output = bash -c "wsl whoami"
#$output = bash -c "wsl 'somepassword | somepassword | someusername | whoami'"
# Attempt 1 conclusion: Does not work, requires a user input to start installing (e.g. arrow down) (and then waits on user input).
# Attempt 2: try to prevent the prompt for username by setting default user to root immediatly
# Attempt 2.1: First define path to the installed ubuntu1804.exe
$str1="/Users/"
$str2="/AppData/Local/Microsoft/WindowsApps/ubuntu1804"
$hdd_name=(Get-WmiObject Win32_OperatingSystem).SystemDrive
$username=$env:UserName
[String] $ubuntu1804_path=$hdd_name+$str1+$username+$str2
# Attempt 2.2: Create command to set root as default user
$str1=" config --default-user root"
$set_user=$ubuntu1804_path+$str1
# Attempt 2.3: Create command to set root as default user and execute it
#invoke-expression -Command $set_user
# Attempt 2.3 conclusion: Doesn't work still asks for username and waits on user input
# Attempt 3: passing a username, password, and password again as one is prompted at the startup
$strA = "test | test | root"
#$output = bash "-c" $strA
# Attempt 3 conclusion: Doesn't work, requires user input to go to the next line (e.g. arrow down)
# Attempt 4: let root be default username
$str1=" install --root"
$set_user=$ubuntu1804_path+$str1
# Attempt 4 conclusion: Doesn't work, requires user input to go to the next line (e.g. arrow down)
invoke-expression -Command $set_user
# Attempt 4 conclusion: Pending.
Write-Host "Done with setup."
0,1,2,3번의 시도 중 사용자 개입 없이 WSL Ubuntu 18.04를 자동으로 초기화하는 데 성공하지 못했습니다. 시도의 문제는 댓글의 결론에 기록됩니다. 일단 활성화되면 wsl은 설치/초기화를 시작하지만 나머지 명령을 파이핑하지 않고 powershell 창에서 사용자 입력을 기다립니다.
질문:
Powershell에서 WSL Ubuntu 18.04의 자동 설치 및 초기화를 수행하려면 어떻게 해야 합니까?
가정
Powershell 변수의 사용자 이름과 비밀번호를 미리 알고 있습니다.
답변1
Add-AppxPackage
Appx 패키지를 설치하는 대신 Expand-Archive
cmdlet을 사용하여 폴더에 압축을 푸세요. 그런 다음 을 실행하여 ubuntu.exe
나머지 부분을 구성합니다. 보다WSL: Windows 서버 설치 가이드추가 아이디어를 얻으려면.
답변2
설명
기본 사용자 루트를 인수로 사용하여 wsl을 초기화하는 네 번째 시도(~ 아니다명령 ubuntu1804 config --default-user root
)이 작동했으며 비밀번호를 입력할 필요가 없습니다.
해결책
다음 코드는 powershell에서 WSL Ubuntu 18.04를 자동으로 설치합니다.
##############Downloading and installing the app###################
# Enable wsl subsystems for linux (if powershell is ran in admin mode)
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
# Set Tls12 protocol to be able to download the wsl application
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# check to see if ubuntu1804 installation file exists and download the app otherwise
$fileToCheck = "Ubuntu1804.appx"
if (Test-Path $fileToCheck -PathType leaf)
{"File does Exist"}
else
{Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile Ubuntu1804.appx -UseBasicParsing}
# Actually install the wsl ubuntu 18.04 app
Add-AppxPackage .\Ubuntu1804.appx
Write-Output "Installed the ubuntu18.04"
# backup installation command if the first command did not function properly
invoke-expression -Command "Add-AppxPackage .\Ubuntu1804.appx"
Write-Output "Installed the ubuntu with backup attempt"
##############Initializing the wsl ubuntu 18.04 app without requiring user input###################
# First define path to the installed ubuntu1804.exe
$str1="/Users/"
$str2="/AppData/Local/Microsoft/WindowsApps/ubuntu1804"
$hdd_name=(Get-WmiObject Win32_OperatingSystem).SystemDrive
$username=$env:UserName
[String] $ubuntu1804_path=$hdd_name+$str1+$username+$str2
# let root be default username
$str1=" install --root"
$set_user=$ubuntu1804_path+$str1
invoke-expression -Command $set_user
Write-Host "Done with setup."