shayne / wsl2-hacks Goto Github PK
View Code? Open in Web Editor NEWUseful snippets / tools for using WSL2 as a development environment
Useful snippets / tools for using WSL2 as a development environment
This seems to work for most applications but it seems to fail for ping
when I run ping google.com it goes straight back to the command prompt. Is there a workaround for this?
xfe gives message "FXApp::openDisplay: unable to open display :0" I have tried several approaches without success. Does any one have a fix?
I am running:
Operating System: Ubuntu 20.04.5 LTS
Kernel: Linux 5.15.57.1-microsoft-standard-WSL2
Architecture: x86-64
Access localhost ports from Windows seems to be fixed in current WSL2 release (build 18970+)
Curious, any chance you can fork and update your procedure for Windows 11 + Ubuntu 22.04?
Really appreciate your effort sharing this info in the first place!
hi
thank you so much for publishing this
although i am struggling to make it work
I've tried different Linux detro's but cant get it to work
is the problem related to windows 11 ?
please help
Windows - 2004 build 19041.487
Distro - Ubuntu 18.04
After going through all the steps when testing out systemctl is-active dbus
it reports the same error, as if I never applied the hack.
Edit: tried rebooting the machine and reinstalling the distro. Even when applying to a fresh distro it does not work. It worked before, but after a reinstallation it just stopped.
wsl2 does not (yet?) support IPv6. This means when a service like apache opens a listening port on the IPv6 ANY interface, these connections are not forwarded through the WSL2 system. Only IPv4 listening addresses are forwarded. I add this to my .bashrc currently which works around the issue by disabling ipv6. The default WSL2 kernel has IPv6 enabled, but the default WSL2 networking does not route IPv6. I don't know why this is. This is a hack, but it's a working hack.
grep -q 1 /proc/sys/net/ipv6/conf/all/disable_ipv6 || sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
grep -q 1 /proc/sys/net/ipv6/conf/default/disable_ipv6 || sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1
Feel free to adapt to your root startup. This could get added to /etc/sysctl.d/something.conf and then startup sysctl on "boot" for a more complete solution that would handle other sysctl settings.
put the shell to wsl2hack and setting default user causes a bug with the "bash from here" in explorer.
In Windows 11, is an alternative:
Don't change the default shell of root and don't change default user to root.
create a file
/etc/wsl.conf
and put
[boot]
command=/usr/local/bin/wsl2hack
in wsl2hack, add
chmod u+w /etc/hosts && echo ip r | grep default | cut -d' ' -f3
windows >> /etc/hosts
to put windows IP in the /etc/hosts with IP of Windows
After this step my editor just shows a load of mashed up @ symbols etc ...
$ sudo touch /usr/bin/bash
$ sudo chmod +x /usr/bin/bash
$ sudo editor /usr/bin/bash
I'm receiving the following error when attempting to install the service.
C:\Users\andre\Documents>.\wsl2host.exe install
Windows Username: ****@outlook.com
Windows Password: 2020/10/27 07:32:41 failed to install wsl2host: The specified domain either does not exist or could not be contacted.
The latest WSL install method includes Ubuntu 20.10. Nothing bad happens, per se, but this guide simply does not work as intended:
ubuntu2004.exe
is now simply ubuntu.exe
root
results in... The default user being root
(presumably the wsl2hack
script is not running)Not entirely sure what the issue might be, at the moment.
On step 4 I substituted 2004 for 18004 and got this error:
wsl --shutdown
ubuntu2004.exe config --default-user root
Error: 0x80070057 The parameter is incorrect.
I have tried restarting but can't get back in to undo the changes I made.
Unable to do it working Ubuntu 20.4 / WSL2... Any idea ?
Three changes are required:
ubuntu1804.exe
, and replace that with ubuntu2004.exe
./usr/bin/bash
, so go with something like /usr/bin/bash-bootstrap-services
instead.daemonize
is now in /usr/bin
instead of /usr/sbin
. The script, as written, will hang indefinitely.Given the third problem, I would recommend that the steps in the instructions be run in a different order. After step 1, jump to step 4. Then do step 3 (sudo
will no longer be required at this point) and then test the script by running it. If it logs you in as your user ID and the test in step 5 works, then and only then do step 3.
Trying to follow the guide by running:
sudo apt install dbus policykit-1 daemonize
Output:
E: Unable to locate package daemonize
I want to enable systemd on Debian WSL under my work machine, and don't want to mess with a lot of stuff.
So, I'd like to know if there is someone who successfully used this hack on Debian WSL ?
Thnaks, cheers!
While there is some utility in exposing the whole script as a README, is easier to deploy this and/or write an installer script if any scripts are kept as separate files. Then users can run / download something on their wsl instances that can automatically pull the latest bash shim and set it as their root user's shell.
Also mentioning this as I have a few other utilities that I've written for wsl2 (https://github.com/madumlao/wsl2-tools) there is probably some utility in combining the work for these.
Hi,
I've did everything from the instructions but I still get the below error.
As You can see, I sing as root but it does not change anything.
root@9BG9HG3:/home/fdrzewiecki# uname -a
Linux 9BG9HG3 5.10.102.1-microsoft-standard-WSL2 #1 SMP Wed Mar 2 00:30:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
root@9BG9HG3:/home/fdrzewiecki# systemctl is-active dbus
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
root@9BG9HG3:/home/fdrzewiecki#
VIPW:
root:x:0:0:root:/root:/bin/bash
rootwsl:x:0:0:root:/root:/usr/local/bin/wsl2hack
VIPW -S
root:*:18375:0:99999:7:::
rootwsl:x:0:0:root:/root:/usr/local/bin/wsl2hack
#!/bin/bash
# your WSL2 username
UNAME="fdrzewiecki"
UUID=$(id -u "${UNAME}")
UGID=$(id -g "${UNAME}")
UHOME=$(getent passwd "${UNAME}" | cut -d: -f6)
USHELL=$(getent passwd "${UNAME}" | cut -d: -f7)
if [[ -p /dev/stdin || "${BASH_ARGC}" > 0 && "${BASH_ARGV[1]}" != "-c" ]]; then
USHELL=/bin/bash
fi
if [[ "${PWD}" = "/root" ]]; then
cd "${UHOME}"
fi
# get pid of systemd
SYSTEMD_PID=$(pgrep -xo systemd)
# if we're already in the systemd environment
if [[ "${SYSTEMD_PID}" -eq "1" ]]; then
exec "${USHELL}" "$@"
fi
if [[ -z ${SYSTEMD_PID} ]]; then
# start systemd
/usr/bin/daemonize -l "${HOME}/.systemd.lock" /usr/bin/unshare -fp --mount-proc /lib/systemd/systemd --system-unit=basic.target
# wait for systemd to start
retries=50
while [[ -z ${SYSTEMD_PID} && $retries -ge 0 ]]; do
(( retries-- ))
sleep .1
SYSTEMD_PID=$(pgrep -xo systemd)
done
if [[ $retries -lt 0 ]]; then
>&2 echo "Systemd timed out; aborting."
exit 1
fi
fi
# export WSL variables
export WINPATH="$(echo "$PATH"|grep -o ':/mnt/c.*$'|sed 's!^:!!')"
RUNOPTS=""
RUNOPTS="$RUNOPTS -l"
RUNOPTS="$RUNOPTS -w WINPATH"
RUNOPTS="$RUNOPTS -w WSL_INTEROP"
RUNOPTS="$RUNOPTS -w WSL_DISTRO_NAME"
# enter systemd namespace
exec /usr/bin/nsenter -t "${SYSTEMD_PID}" -m -p --wd="${PWD}" /sbin/runuser $RUNOPTS -s "${USHELL}" "${UNAME}" -- "${@}"
Not sure What I'm doing wrong here...
I'm now seeing the same problem that was reported on the (now archived) ubuntu-wsl2-systemd-script:
DamionGans/ubuntu-wsl2-systemd-script#15
The solution is to add the following to /etc/rc.local
in step 6
ls /proc/sys/fs/binfmt_misc > /dev/null 2>&1 || \
mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc
More background:
Without this fix, I am now seeing the following when attempting to debug an application created using create react app:
[client] Error: spawn WSL Interopability is disabled. Please enable it before using WSL.
[client] C:/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe ENOENT
[client] at Process.ChildProcess._handle.onexit (internal/child_process.js:269:19)
The error message itself is produced by wslu-header when it is not able to verify that binfmt_misc/WSLInterop
is enabled
. Googling on that lead me to ubuntu-wsl2-systemd-script issue and ultimate fix.
Apparently, invoking the webpack devserver causes the system default browser to be launched, which in the case of WSL2 involves spawning out to powershell.
cynyc@CybrOps:$ which daemonize$
/usr/bin/daemonize
cynyc@CybrOps:
Fixing this in the /usr/bin/bash script causes things to work properly again.
Otherwise, bash hangs.
wsl -e /bin/bash gets you back in to fix if you updated before modifying the script.
Any hacks to get WSL2 working in Windows Server 2022? It used to be working in insider builds which means it's currently blocked and not unimplemented so probably there's a way around it.
Followed the instructions for Auto-start/services, and found that wsl2 entered an infinite loop on starting after setting it up.
Tracked it down to /usr/sbin/daemonize
not executing, because it was installed to /usr/bin/
instead, after updating the reference to /usr/bin
all worked fine. :)
The following message shows up every time I start Pengwin after doing this workaround:
/mnt/c/WINDOWS/system32/ipconfig.exe: Invalid argument
wslpath: Invalid argument
I tracked down the problem and it is in the last line it appears.
Thank you for this - it's great, but I have a few suggestions to improve this.
root
user. Instead create a second root user with same uid but a different username, such as:root:x:0:0:root:/root:/bin/bash
rootwsl:x:0:0:root:/root:/usr/local/bin/wsl2hack
Don't edit /etc/passwd
(or /etc/shadow
) directly, instead use vipw
and vipw -s
.
Put the script in /usr/local/bin/
and don't call it bash
(you can see what I did above in my passwd fragment).
Don't swallow the output from daemonize
and don't retry forever. Here's what I do (I also loosened up the sleep a bit, 100ms is fast enough for me):
/usr/bin/daemonize -l "${HOME}/.systemd.lock" /usr/bin/unshare -fp --mount-proc /lib/systemd/systemd --system-unit=basic.target
# wait for systemd to start
retries=50
while [[ -z ${SYSTEMD_PID} && $retries -ge 0 ]]; do
(( retries-- ))
sleep .1
SYSTEMD_PID=$(pgrep -xo systemd)
done
if [[ $retries -lt 0 ]]; then
echo "Systemd failed to start. Giving up"
exit 1
fi
Just my suggestions, for what they are worth. Thanks again!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.