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


>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.

事实证明,还有另一个命令简单地称为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

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

wsl -u root

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

最新更新