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

问题描述 投票:0回答:3
>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.
bash ubuntu windows-10 windows-subsystem-for-linux
3个回答
12
投票

原来还有另一个命令,简称为

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


0
投票

如果您需要运行多个命令,只需以 root 身份登录即可:

wsl -u root

并以交互方式运行所有命令


0
投票

根帐户通常默认情况下不处于活动状态

执行此操作(它将提示您选择root密码):

sudo passwd root

然后您就可以以 root 身份登录:

su root

完成请求 root 权限的活动后,以用户身份重新登录:

su <username>
© www.soinside.com 2019 - 2024. All rights reserved.