Skip to content

Note

文件更新時間: 20230216

How To: 設定 WSL2

Environment

wsl --version
wsl --version
WSL 版本: 1.0.3.0
核心版本: 5.15.79.1
WSLg 版本: 1.0.47
MSRDC 版本: 1.2.3575
Direct3D 版本: 1.606.4
DXCore 版本: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows版本: 10.0.22621.1265
/etc/os-release
NAME="Red Hat Enterprise Linux"
VERSION="8.7 (Ootpa)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="8.7"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux 8.7 (Ootpa)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:8::baseos"
HOME_URL="https://www.redhat.com/"
DOCUMENTATION_URL="https://access.redhat.com/documentation/red_hat_enterprise_linux/8/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"

REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_BUGZILLA_PRODUCT_VERSION=8.7
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8.7"

Configurations

/etc/wsl.conf
## This is the file content
## Replace the username
## Source for the options: https://docs.microsoft.com/en-us/windows/wsl/wsl-config#configure-per-distro-launch-settings-with-wslconf
[automount]
enabled = true
mountfstab = true
root = /mnt/
options = metadata,uid=1000,gid=1000,umask=0022,fmask=11,case=off

[network]
generatehosts = true
generateResolvConf = false
hostname = rhel8

[interop]
enabled = true
appendwindowspath = true

[boot]
systemd = true
command = "cp -rup /etc/resolv.conf.dist /etc/resolv.conf"

[user]
# Need to change if you have a different username
default = pinhuang
/etc/resolv.conf.dist
nameserver 168.95.1.1
nameserver 8.8.8.8

Troublshooting

1. WSL2 DNS Resolver

關於 generateResolvConf = false 設定,因為經常換地方工作太常遇到 WSL2 的 DNS Resolver 不明問題,所以後來決定把它關掉,改用 cp -rup /etc/resolv.conf.dist /etc/resolv.conf 的方式來確保 DNS 解析運作正常,無任何副作用

2. WSL2 Clock is out of sync with Windows

關於 Clock out of sync 的問題,這是一個 Bug: WSL2 date incorrect after waking from sleep #5324,暫解方式就是在 WSL2 中執行 sudo hwclock -s,或者是 wsl --shutdown; wsl 重開 WSL2 Instance

該問題主因是 Linux Kernel Bug,雖然 Servicing the Windows Subsystem for Linux (WSL) 2 Linux kernel 號稱有解決,但我目前還是有一樣的問題,但無太大影響就先用暫解作法處理

command
10:58:36 pinhuang@rhel8 0 /mnt/c/Users/pinhuang/rhwsl-home
$ sudo hwclock -s
04:08:48 pinhuang@rhel8 0 /mnt/c/Users/pinhuang/rhwsl-home
$

3. 確認 systemd 啟用成功

command
ps --no-headers -o comm 1
systemctl list-units --type=service
systemctl list-unit-files --type=service --state=enabled
output
$ ps --no-headers -o comm 1
ctl list-units --type=service
systemctl list-unit-files --type=service --state=enabledsystemd

$ systemctl list-units --type=service
UNIT                           LOAD   ACTIVE SUB     DESCRIPTION
chronyd.service                loaded active running NTP client/server
dbus.service                   loaded active running D-Bus System Message Bus
systemd-journal-flush.service  loaded active exited  Flush Journal to Persistent Storage
systemd-journald.service       loaded active running Journal Service
systemd-sysctl.service         loaded active exited  Apply Kernel Variables
systemd-tmpfiles-setup.service loaded active exited  Create Volatile Files and Directories
systemd-update-utmp.service    loaded active exited  Update UTMP about System Boot/Shutdown
systemd-user-sessions.service  loaded active exited  Permit User Sessions

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

8 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

$ systemctl list-unit-files --type=service --state=enabled
UNIT FILE                              STATE
[email protected]                        enabled
chronyd.service                        enabled
dbus-org.freedesktop.timedate1.service enabled
[email protected]                         enabled
timedatex.service                      enabled

5 unit files listed.

References