wsl2-hacks
wsl2-hacks copied to clipboard
A few suggestions to improve this
Thank you for this - it's great, but I have a few suggestions to improve this.
- Never mess with the
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 usevipw
andvipw -s
. -
Put the script in
/usr/local/bin/
and don't call itbash
(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!
I've just tried to apply the workaround for the first time, already with your suggestions applied. I got WSL2 + Ubuntu 20.04. Far from being a Linux admin or anything.
The trick for me is that whenever I'd launch wsl after changing the default user, I'd only get root user with regular bash in my terminal. systemctl would not work either.
After checking the code a few times I figured that wsl could be somehow finding the user correctly - but when effectively launching with that user it would then grab the first in the list with that uid. In that case, the actual root
user.
Out of that thought I went vipw
and vipw -s
again and got the rootwsl
user before of root
in the list, thus making it the first entry in the list.
It kinda worked. After shutting wsl down and launching ubuntu again, I get the message: Can't lock the lock file "/root/.systemd.lock". Is another instance running?
, but I'm then logged in with my user.
systemctl is-active dbus
now brings me active
, also.
Notice that I haven't tried up anything further than that, so I don't know for a fact if anything weird would come out of this.
In 20.04, /usr
is just a link to /usr/bin
, so putting fake bash in there replaces the real bash. Tripped me up for a minute!
@esatapedico I was getting the same error. It happens when daemonize
tries to grab the lock and systemd
is already running. The script already checks if it is running by looking for its process ID, so we can just use that to skip trying to run it again:
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
Based on the comments here - I modified the instructions - hopefully helpful for everyone
@atiensivu Looks like the closing fi
is missing for if [[ -z ${SYSTEMD_PID} ]]; then
(it should be right before the line # enter systemd namespace
).
Are you going to submit a PR back to this repo?
did you try running the command with elevated powershell prompt (right clicking on the powershell icon ?
One question I'm hoping someone can answer; on step 3 it mentions to use both vipw
AND vipw -s
. It shows exactly what to paste into the passwd file, but not the shadow file.
for the shadow file do I just copy the root line, and paste it below that, changing "root" to "rootwsl"? like this:
- Root line:
root:*:12345:0:99999:7:::
- Pasted Line:
rootwsl:*:12345:0:99999:7:::
HALP!1
I just can't seem to get this working, can anyone please spot the problem for me?
My vipw
My vipw -s
My /usr/local/bin/wsl2hack
#!/bin/bash
# source: https://github.com/atiensivu/wsl2-hacks/blob/e20bbcab6a436147d0ba84cdb237fc7a79e23b00/README.md
# your WSL2 username
UNAME="kacey"
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}"
>&2 echo "cd'ing to ""${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}" "$@"
>&2 echo "Already in systemd environment."
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
# enter systemd namespace
>&2 echo "Attempting to enter systemd namespace."
exec /usr/bin/nsenter -t "${SYSTEMD_PID}" -m -p --wd="${PWD}" /sbin/runuser -s "${USHELL}" "${UNAME}" -- "${@}"
Now when I try to set the default user to root, I get this:
If I ty to open a new Ubuntu tab in Windows Terminal, it shows this (so it at least seems like it's reading the new wsl2hack file):
Can anyone help me out? I'm pretty much a beginner at Linux so this may be something easy that I just don't know how to handle.
UNAME="kacey"
UUID=$(id -u "${UNAME}") UGID=$(id -g "${UNAME}")
I could be missing something here, but didnt you set the user to " kacey " ?
should it be --default-user kacey ?
Hi @wanfuse123
UNAME="kacey"
UUID=$(id -u "${UNAME}") UGID=$(id -g "${UNAME}")
I could be missing something here, but didnt you set the user to " kacey " ?
should it be --default-user kacey ?
I am a bit confused about that too. I'm following these instructions:
https://github.com/shayne/wsl2-hacks/blob/50d379fbc87f4ed9211c7682339cc75eadf1112e/README.md
and it says at # 2:
Add the following, be sure to replace <YOURUSER> with your WSL2 Linux username
# your WSL2 username
UNAME="<YOURUSER>"
My normal Ubuntu username is kacey.
But further down at # 4 it says (btw, I'm using the direct ubuntu version from the store, so my ubuntu.exe has no version # in it):
In a PowerShell terminal run:
ubuntu2004.exe config --default-user root
Am I doing it wrong?
Myself i found additional directions ( sorry don't remember where from) for booting ubuntu under wsl2 from non root user
And just combined them.
Its possible it was directions from another fork of that project.
Search google for :
booting ubuntu under wsl2 from non root user
Also i dont think it should still be trying systemd if you have installed daemonize instead
Note proper changes to /etc/passwd and the corresponding file bash script file it points to.
As i recall daemonize was one of a few of the packages needing to be installed and systemd was bot one of them.
Good luck!
On Sat, Oct 17, 2020, 11:21 PM Kacey [email protected] wrote:
Hi @wanfuse123 https://github.com/wanfuse123
UNAME="kacey"
UUID=$(id -u "${UNAME}") UGID=$(id -g "${UNAME}")
I could be missing something here, but didnt you set the user to " kacey " ?
should it be --default-user kacey ?
I am a bit confused about that too. I'm following these instructions:
https://github.com/shayne/wsl2-hacks/blob/50d379fbc87f4ed9211c7682339cc75eadf1112e/README.md
and it says at # 2:
Add the following, be sure to replace with your WSL2 Linux username
your WSL2 username
UNAME="<YOURUSER>"
My normal Ubuntu username is kacey.
But further down at # 4 it says (btw, I'm using the direct ubuntu version from the store, so my ubuntu.exe has no version # in it):
In a PowerShell terminal run:
ubuntu2004.exe config --default-user root
Am I doing it wrong?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/shayne/wsl2-hacks/issues/7#issuecomment-711110809, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADDBZWIA4PH5VYADG26SKATSLJNMPANCNFSM4M3O4OZQ .
Myself i found additional directions ( sorry don't remember where from) for booting ubuntu under wsl2 from non root user And just combined them. Its possible it was directions from another fork of that project. Search google for : booting ubuntu under wsl2 from non root user
Thanks for helping @wanfuse123 :)
Being a almost total newbie with Linux, can you help me understand how doing a:
ubuntu.exe config --default-user root
as a non-root user would be better (or work at all) over doing it as a root user? I'm a bit confused with that.
The above commands I am running as the "true admin" user account in Windows (the hidden Administrator account); if I do this command from my regular windows user account that has Admin privileges, I get this:
Also, is this what you meant? (and putting a non-root user for the user name? currently I use the user name 'root' just to be able to get back into my Ubuntu after I made all these changes).
wsl.exe --user <user name>
Also i dont think it should still be trying systemd if you have installed daemonize instead
Note proper changes to /etc/passwd and the corresponding file bash script file it points to.
AFAIK I copied the paths and script code verbatim. The only confusion I really have there is the shadow file (vipw -s
) because the tutorial doesn't directly address that.
As i recall daemonize was one of a few of the packages needing to be installed and systemd was bot one of them.
Again, I'm just a newbie but I thought systemd was a default part of Ubuntu and didn't have to be installed via a package? (definitely could be wrong here). I'm not sure why the script tests for systemd; that's beyond my Linux knowledge for sure, lol!
It seems like I'm "halfway" there at least. When I do:
PS C:\Users\Administrator> bash.exe -c "whoami"
It says:
rootwsl
It just seems that I can't switch it to using the default account "root", like the tutorial shows.
Also, I used your tutorial @atiensivu , perhaps you can help? I'd also love some input from the others in this thread, as I have a feeling they know WAY more about this than I do, and probably already have thiers up and running. @scotte @esatapedico @JohnTasto
I'd really genuinely appreciate the help so I can move on to the things I'm stuck holding off on until I can get this working. :)
Well, I've come to the conclusion that this just doesn't work. I give up.
Anyone else who may come across this, I'd suggest not wasting your time on it.
@YouveGotMeowxy, I also struggled, but I managed to get this working for Ubuntu 20.04. I used the instructions on https://github.com/atiensivu/wsl2-hacks. The only part that was missing was the shadow file edit (which you highlighted above), and a closing fi
statement clause in the script. See below:
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
@atiensivu, please update your README.md
file with the missing fi
, and the shadow file edit - thank you. :)
Following these instructions, when I start WSL, I'm root. Even though the default-user is rootwsl. The script successfully logs me in as myself if I run it manually, but it doesn't run on login (because it's set for rootwsl and I'm showing up as root?). What am I doing wrong?
passwd:
root:x:0:0:root:/root:/bin/bash
rootwsl:x:0:0:root:/root:/usr/local/bin/wsl2hack
I've also tried rootwsl:x:0:0:rootwsl:/root:/usr/local/bin/wsl2hack
as suggested here, but it's the same. I'm root when I log in, not rootwsl, and not myself.
My $PATH
also seems to disappear if I run the script manually. So I'll be logged in as myself, but a lot of commands (like sysctl
) will stop working because they're no longer on the path.