Bash/WSL - 如何以 root 身份运行命令?

mpe*_*pen 3 bash ubuntu windows-10 windows-subsystem-for-linux

>ubuntu1804.exe -c "echo $USER"
mpen  
Run Code Online (Sandbox Code Playgroud)

以我的身份运行命令,我如何以 root 身份运行它?

帮助页面甚至没有提到 -c

>ubuntu1804.exe help
Launches or configures a Linux distribution.

Usage:
    <no args>
        Launches the user's default shell in the user's home directory.

    install [--root]
        Install the distribuiton and do not launch the shell when complete.
          --root
              Do not create a user account and leave the default user set to root.

    run <command line>
        Run the provided command line in the current working directory. If no
        command line is provided, the default shell is launched.

    config [setting [value]]
        Configure settings for this distribution.
        Settings:
          --default-user <username>
              Sets the default user to <username>. This must be an existing user.

    help
        Print usage information.
Run Code Online (Sandbox Code Playgroud)

mpe*_*pen 5

原来还有一个简单的命令wsl可以让你以任意用户的身份运行任意命令:

>wsl -u root -d Ubuntu-18.04 -- echo "I am g$USER"
I am groot
Run Code Online (Sandbox Code Playgroud)

注意,您需要为此使用单独的参数(而不是字符串)。

-d是可选的。您可以更改默认发行版,例如

wslconfig.exe /l
wslconfig.exe /s Ubuntu-18.04
wslconfig.exe /l
Run Code Online (Sandbox Code Playgroud)

wslconfig /l 似乎相当于 wsl --list