WSLのセットアップ(旧)
概要
WSL を導入するにあたっての Tips をまとめました。おおまかなゴールとしては、Windows で快適なターミナルライフが送れるようになることです。
前提条件
Microsoft Store にて以下がインストールされているものとします
LTS 版の Ubuntu
Windows Terminal
環境
Windows 10 Pro
Version 1909
OS build 18363.836
Ubuntu 18.04.4 LTS (Bionic Beaver)
Windows Terminal 0.11.1251.0
方針
Windows 側からは Linux のファイルを変更しない
Ubuntu のパッケージを最新化
code:パッケージリストを最新化
$ sudo apt update
code:パッケージをアップグレード
$ sudo apt upgrade -y
metadata を有効にして再マウント
初期状態では、C ドライブなどの Windows 側のファイルシステムがパーミッション 777 のような状態でマウントされてしまいます。この問題を回避するため、まず次のような /etc/wsl.conf ファイルを作成します。
code:/etc/wsl.conf
enabled = true
root = /mnt/
options = "metadata,umask=22,fmask=11"
mountFsTab = true
generateHosts = true
generateResolvConf = true
enabled = true
appendWindowsPath = false # Windows の PATH を引き継がない
なお、/etc/wsl.conf を設置しただけでは状態は変わらないため、一旦 Ubuntu を terminate し、WSL を再起動します。
code:Rebooting Ubuntu on WSL
# PowerShell で WSL を terminate
PS > wsl -t Ubuntu-18.04
# Ubuntu 側では次のメッセージが出力され terminate される
The Windows Subsystem for Linux instance has terminated.
# ターミナルを再起動し metadata が有効化されていることを確認
$ mount -l
rootfs on / type lxfs (rw,noatime)
none on /dev type tmpfs (rw,noatime,mode=755)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,noatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,noatime)
devpts on /dev/pts type devpts (rw,nosuid,noexec,noatime,gid=5,mode=620)
none on /run type tmpfs (rw,nosuid,noexec,noatime,mode=755)
none on /run/lock type tmpfs (rw,nosuid,nodev,noexec,noatime)
none on /run/shm type tmpfs (rw,nosuid,nodev,noatime)
none on /run/user type tmpfs (rw,nosuid,nodev,noexec,noatime,mode=755)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,relatime)
cgroup on /sys/fs/cgroup type tmpfs (rw,relatime,mode=755)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,relatime,devices)
C:\ on /mnt/c type drvfs (rw,noatime,uid=1000,gid=1000,umask=22,fmask=11,metadata,case=off)
E:\ on /mnt/e type drvfs (rw,noatime,uid=1000,gid=1000,umask=22,fmask=11,metadata,case=off)
結果、再マウント後は metadata,umask=22,fmask=11 と認識されるようになりました。しかし、umask コマンドの結果は相変わらず 0000 のままだったため、結局 /etc/profile か ~/.bashrc のどちらかに umask 0022 を追加する必要がありました。なお、Version 2004 より wsl.conf の [filesystem] セクションに umask=0022 のような指定が可能となるため、umask の設定はもしかしたら wsl.conf に一本化できるようになるかもしれません。
ホームディレクトリの設定
ホームディレクトリは、Linux 側の /home/<user> とするのであれば特に何もする必要はありません。また、Windows Terminal には startingDirectory という設定項目があり、Home とは別にこちらで WSL 起動時のデフォルトディレクトリを定義することができます。参考までに自分の settings.json を貼っておきます。主にこちらの Windows Terminal (Preview) メモ を参考にさせていただきました。 code:setting.json
// To view the default settings, hold "alt" while clicking on the "Settings" button.
{
"defaultProfile": "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}",
"copyOnSelect": true,
"profiles":
{
"defaults":
{
// Put settings here that you want to apply to all profiles
"cursorShape": "filledBox",
"fontFace": "Meslo LG S",
"fontSize": 12
},
"list":
[
{
"guid": "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}",
"hidden": false,
"name": "Ubuntu-18.04",
"commandline": "wsl.exe -d Ubuntu-18.04 -u perforb",
"colorScheme": "One Half Dark",
"startingDirectory": "//wsl$/Ubuntu-18.04/home/perforb"
},
{
// Make changes here to the powershell.exe profile
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"hidden": false
},
{
// Make changes here to the cmd.exe profile
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "cmd",
"commandline": "cmd.exe",
"hidden": false
},
{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
"hidden": false,
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure"
}
]
},
// Add custom color schemes to this array
"schemes": [],
// Add any keybinding overrides to this array.
// To unbind a default keybinding, set the command to "unbound"
"keybindings": []
}
bash から zsh に変更
bash から zsh への変更手順です。.zsh_profile や .zshrc などはお好みで。
code:install zsh
$ sudo apt install zsh
$ zsh --version
zsh 5.4.2 (x86_64-ubuntu-linux-gnu)
$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/bin/rbash
/bin/dash
/usr/bin/tmux
/usr/bin/screen
/bin/zsh
/usr/bin/zsh
$ chsh -s /bin/zsh
ただし zsh に変更したところ、/etc/wsl.conf の appendWindowsPath を false にしているにもかかわらず、なぜか Windows の PATH が引き継がれるようになってしまいました。そのため現在は bash に戻しています。時間が取れれば改めて調査したいと思います。
Install peco
bash prompt
.bashrc と .bash_profile
基本的には .bashrc に寄せますが、eval \`ssh-agent\` など一回だけ実行したいコマンドは .bash_profile に記述します。また、.bash_profile 内で .bashrc を読み込むようにします。
code:.bash_profile
eval ssh-agent
test -r ~/.bashrc && . ~/.bashrc
ref:
Windows エクスプローラから WSL のファイルシステムへアクセス
Windows 10 build 18342 より \\wsl$ でアクセスできるようになったようです。
参考