🙌

Windows + WSL(2)の関する備忘録

2021/12/05に公開

この記事の目的

  • WSLでよく使うコマンドを忘れたとき用

使用技術

1.WSL2
2.Windows Terminal
3.Ubuntu 20.04 LTS

現在、登録しているWSL一覧

wsl -l

バージョンを表示したい場合

wsl -l -v

エクスポート

wsl --export [distribution_name] [file_name].tar

いつでもimportできるtarファイルとして保存できる。

インポート

wsl --import [distribution_name] [path/to/install/dir] [file_name].tar --version 2

デフォルトユーザの変更

Function WSL-SetDefaultUser ($distro, $user) { Get-ItemProperty Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\*\ DistributionName | Where-Object -Property DistributionName -eq $distro | Set-ItemProperty -Name DefaultUid -Value ((wsl -d $distro -u $user -e id -u) | Out-String); };
WSL-SetDefaultUser [DistroName] [UserName]

新しいバージョンではwsl.confから設定する(userの作成は手動で行う)

// USER_NAMEさんを、sudoというグループに追加するUbuntuコマンド
sudo gpasswd -a USER_NAME sudo

// 同義
sudo usermod -aG sudo USER_NAME

WSLのシャットダウン

wsl -t [distribution_name]

WSL 登録削除

wsl --unregister [distribution_name]

wsl.confの設定例

# Automatically mount Windows drive when the distribution is launched
[automount]

# Set to true will automount fixed drives (C:/ or D:/) with DrvFs under the root directory set above. Set to false means drives won't be mounted automatically, but need to be mounted manually or with fstab.
enabled = true

# Sets the directory where fixed drives will be automatically mounted. This example changes the mount location, so your C-drive would be /c, rather than the default /mnt/c. 
root = /

# DrvFs-specific options can be specified.  
options = "metadata,uid=1003,gid=1003,umask=077,fmask=11,case=off"

# Sets the `/etc/fstab` file to be processed when a WSL distribution is launched.
mountFsTab = true

# Network host settings that enable the DNS server used by WSL 2. This example changes the hostname, sets generateHosts to false, preventing WSL from the default behavior of auto-generating /etc/hosts, and sets generateResolvConf to false, preventing WSL from auto-generating /etc/resolv.conf, so that you can create your own (ie. nameserver 1.1.1.1).
[network]
hostname = DemoHost
generateHosts = false
generateResolvConf = false

# Set whether WSL supports interop process like launching Windows apps and adding path variables. Setting these to false will block the launch of Windows processes and block adding $PATH environment variables.
[interop]
enabled = false
appendWindowsPath = false

# Set the user when launching a distribution with WSL.
[user]
default = DemoUser

# Set a command to run when a new WSL instance launches. This example starts the Docker container service.
[boot]
command = service docker start

参照元Webサイト

https://matsudamper.hatenablog.com/entry/2020/01/06/173939
https://learn.microsoft.com/ja-jp/windows/wsl/wsl-config#user-settings

Discussion