【问题标题】:Bash/WSL - How to run command as root?Bash/WSL - 如何以 root 身份运行命令?
【发布时间】:2020-05-01 19:53:57
【问题描述】:
>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


    【解决方案1】:

    原来还有另一个命令叫做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

    【讨论】:

      【解决方案2】:

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

      wsl -u root
      

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

      【讨论】:

      • 如果您能够以交互方式运行命令,您可以像往常一样使用sudosu。如果您需要以非交互方式从 Windows 运行多个命令,您可以执行 wsl -u root -- sh -c 'echo foo &amp;&amp; echo bar'
      • @mpen 我必须以交互方式运行命令才能将我的用户添加到 sudo 组:-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 2022-11-25
      相关资源
      最近更新 更多