wsl-sudo icon indicating copy to clipboard operation
wsl-sudo copied to clipboard

WSL2? Permissions? Can't find Powershell? I'm not sure what the problem is!

Open asos-douglaswaugh opened this issue 2 years ago • 3 comments

Hi! This was working brilliantly for me when I was using WSL1, but I'm trying it in WSL2 and I'm having problems. I know there was a revision for WSL2, but I have that revision in repo I cloned.

If I run

python3 wsl-sudo.py net.exe sessions

I get

Traceback (most recent call last):
  File "wsl-sudo.py", line 335, in <module>
    main()
  File "wsl-sudo.py", line 331, in main
    UnprivilegedClient().main(**vars(args))
  File "wsl-sudo.py", line 236, in main
    self.sock, acc = listen_socket.accept()
  File "/usr/lib/python3.8/socket.py", line 292, in accept
    fd, addr = self._accept()
socket.timeout: timed out

If I run

sudo python3 wsl-sudo.py net.exe sessions

I get

[sudo] password for douglaswaugh:
Traceback (most recent call last):
  File "wsl-sudo.py", line 335, in <module>
    main()
  File "wsl-sudo.py", line 331, in main
    UnprivilegedClient().main(**vars(args))
  File "wsl-sudo.py", line 223, in main
    subprocess.check_call(
  File "/usr/lib/python3.8/subprocess.py", line 359, in check_call
    retcode = call(*popenargs, **kwargs)
  File "/usr/lib/python3.8/subprocess.py", line 340, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'powershell.exe'

But if I invoke powershell in WSL2

powershell.exe

It seems I can access it from Ubuntu, although it's not the cross platform version

Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS Microsoft.PowerShell.Core\FileSystem::\\wsl$\Ubuntu-20.04\home\douglaswaugh\dev>

So it seems I have some sort of permissions issue and some sort of Powershell problem? Any ideas?

asos-douglaswaugh avatar May 27 '22 10:05 asos-douglaswaugh

I also tried running the same commands from a Windows Terminal run as Admin but I got the same results.

asos-douglaswaugh avatar May 27 '22 10:05 asos-douglaswaugh

One way to solve the problem is just change wsl-sudo.py to use the full path to powershell.exe. I am sure there is a better way but if you just want this to work this is fine.

Example:

diff --git a/wsl-sudo.py b/wsl-sudo.py
index 8eaae20..a5a0821 100755
--- a/wsl-sudo.py
+++ b/wsl-sudo.py
@@ -221,7 +221,7 @@ class UnprivilegedClient:

                 try:
                     subprocess.check_call(
-                        ["powershell.exe", "Start-Process", "-Verb", "runas",
+                        ["/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe", "Start-Process", "-Verb", "runas",
                          "-WindowStyle", window_style,
                          "-FilePath", "wsl", "-ArgumentList",
                          '"{}"'.format(subprocess.list2cmdline([
d

chrisg123 avatar Jun 29 '22 20:06 chrisg123

It's probably a duplicate of https://github.com/Chronial/wsl-sudo/issues/4. See https://github.com/Chronial/wsl-sudo/issues/4#issuecomment-1866483402.

fenjen avatar Dec 21 '23 15:12 fenjen