Bash / WSL - Как запустить команду как root? - PullRequest
1 голос
/ 15 января 2020
>ubuntu1804.exe -c "echo $USER"
mpen  

Это запускает команду как я, как мне запустить ее как 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.

1 Ответ

1 голос
/ 16 января 2020

Оказывается, есть еще одна команда, просто называемая wsl, которая позволяет запускать произвольные команды от имени произвольных пользователей:

>wsl -u root -d Ubuntu-18.04 -- echo "I am g$USER"
I am groot

Обратите внимание, что для этой команды вам нужно использовать отдельные аргументы (вместо строки).

-d необязательно. Вы можете изменить дистрибутив по умолчанию, например,

wslconfig.exe /l
wslconfig.exe /s Ubuntu-18.04
wslconfig.exe /l

wslconfig /l эквивалентно wsl --list

...