Reputation: 282995
>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.
Upvotes: 6
Views: 30484
Reputation: 606
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"
Upvotes: 1
Reputation: 47
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>
Upvotes: 2
Reputation: 39323
If you need to run multiple commands, just login as root:
wsl -u root
and run all commands interactively
Upvotes: 0
Reputation: 282995
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
Upvotes: 15