Bash/WSL - How to run command as root?
Asked Answered
O

4

6
>ubuntu1804.exe -c "echo $USER"
mpen  

That runs the command as me, how do I run it as root?

The help page doesn't even mention -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.
Opine answered 15/1, 2020 at 3:32 Comment(0)
O
14

Turns out there's another command simply called wsl that lets you run arbitrary commands as arbitrary users:

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

N.B. you need to use separate args (instead of a string) for this one.

-d is optional. You can change the default distro like

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

wslconfig /l appears to be equivalent to wsl --list

Opine answered 16/1, 2020 at 6:10 Comment(1)
You win the internet for your echo example, as far as I'm concerned. Maybe it's an old joke by now, but I voted up your solution for that alone.Mateya
U
1

Often the root account is not active by default

Do this (it will prompt you to select the password for root):

sudo passwd root

and then you can login as root:

su root

After you have done with activities that request root privileges, re-login back as user:

su <username>
Unfasten answered 15/1, 2024 at 9:58 Comment(1)
You should use su - root, or, in general, su - <username> to log in with full user environment. To logout, you should use logout or simply Ctrl + D (in almost every terminal). Using su again will start a new session over the pre-existing root and the original user sessions.Ruella
S
1

First list all your wsl distributions. In my case, Ubuntu is default.

wsl -l

Since ubuntu is default, all you need is "-u" flag to run a command as root.

wsl -u root "whoami"

If ubuntu is not default, you can either set it as default if you want

wsl -s ubuntu

Or, specify that you want to run this in ubuntu environment

 wsl -d ubuntu -u root "whoami"
Sematic answered 24/7, 2024 at 22:52 Comment(0)
G
0

If you need to run multiple commands, just login as root:

wsl -u root

and run all commands interactively

Guanidine answered 7/2, 2022 at 0:2 Comment(2)
If you're able to run commands interactively, you can just use sudo or su as normal. If you need to run multiple commands from Windows non-interactively, you can do wsl -u root -- sh -c 'echo foo && echo bar'Opine
@Opine I had to run commands interactively to add my user to the sudo group :-)Guanidine

© 2022 - 2025 — McMap. All rights reserved.