Go to list of users who liked
Share on X(Twitter)
Share on Facebook
More than 5 years have passed since last update.
【シェル芸人への道】Bash on Windowsを探検する
Bash on Windows
知っている人は知っていますが、Windows 10に今夏のアップデートでBash が入るなんていうシェル芸人を目指す人間としては聞き逃せないニュースが入ってきました。
少し出遅れましたが、探検してみたいと思います。
インストール
Insider Previewを有効にしてもなかなか落ちてこないという焦らしプレイ以外には特に困った点もないです。
ここで丁寧に解説するようなものでもないので他記事のリンクで割愛します。
Windows 10のbash on Windowsを試す
Bash on Windows 10を使い始めてみた。(@kmry2045 )
初回起動の使用許諾的なところまでは進んでから探検を開始します。
(備考)
上で紹介した記事はWindows 10 build 14316 ベースで書かれています。
この記事を書き始めてから妙にPCが重かったので再起動したところ、 Windows Updateが入って上の記事同様にWindows 10 build 14316 だったウチのPCが進化してしまいました 。
PCが重かったのは裏でアップデートをせっせと準備していたからのようです。
というわけで、この記事はWindows 10 build 14328 ベースで書かれています。
PS C:\WINDOWS\system32> Get-WmiObject Win32_OperatingSystemSystemDirectory : C:\WINDOWS\system32Organization :BuildNumber : 14328RegisteredUser : ******SerialNumber : *****-*****-*****-*****Version : 10.0.14328とりあえず探検
それでは思いつくままに未開の地、Bash on Windowsを探検したいと思います。
起動
まずは起動です。
bashはスタートメニューに登録されている、Bash on Ubuntu on Windows から起動できます。
なんということでしょう、いとも簡単に Bash が起動してしm...
ちょっと待ってください。いったいこのBash on Ubuntu on Windows は何者なのでしょうか。
プロパティを見てみます。
どうやらこいつはC:\Windows\System32\bash.exe のショートカット、本体はbash.exe というWindowsアプリのようです。
bash.exe
Bash on Ubuntu on Windowsがそもそもどう動いているかという話なのですが、MSDNが公開しているBash on Ubuntu on Windows の中に、
という画像が出てきます。
今回のbashを動かすWindows機能の総称として、Windows Subsystem for Linux(WSL) という名称が使われていますが、その管理コマンドlxrun について、
lxrun: The lxrun command is used to manage theWSL instance.
という記述が確認できます。
インスタンス、とのことなのでユーザごとに何かしらの空間があり、それを呼び出す大本のコマンドがbash.exe という感じなのでしょうか。
基本コマンド
それでは実際にコマンドを実行していきます。
まずは、ここはどこ、私はだぁれ?的に以下から。
- pwd
- id
- hostname
- uname -a
- bash --version
root@PERSONAL:~#pwd/rootroot@PERSONAL:~#iduid=0(root) gid=0(root) groups=0(root)デフォルトではrootユーザとしてログインした状態のようですね。
root@PERSONAL:~#hostnamePERSONALホスト名はWindows自体のホスト名をそのまま(ただし大文字で)引き継ぐようです。
root@PERSONAL:~#uname-aLinux PERSONAL 3.4.0+ #1 PREEMPT Thu Aug 1 17:06:05 CST 2013 x86_64 x86_64 x86_64 GNU/Linuxこの通り、環境上注意しておくこととすれば、LinuxとしてはKernel 3.4.0+ に見えているということですね。
root@PERSONAL:/proc#bash--versionGNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)Copyright (C) 2013 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>This is free software;you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.WSLにおけるbashは4.3.11 です。これシェル芸テストに出ます。
ファイル
それでは色々なディレクトリからファイルを漁りつつを探検していきます。
まずdf でもやってみましょう。
root@PERSONAL:~#dfdf: cannot read table of mounted file systems: No such file or directoryあら、dfコマンドは使えないようです。諦めましょう。
/proc
それではシステムのお城、/procを探検しましょう。
どれどれ。
root@PERSONAL:~#cd /proc/root@PERSONAL:/proc#ls-latotal 4dr-xr-xr-x 1 root root 0 Apr 25 17:19 .drwxrwxr-x 2 root root 0 Jan 1 1970 ..dr-xr-xr-x 1 root root 0 Apr 25 17:20 1dr-xr-xr-x 1 root root 0 Apr 25 17:20 2dr-xr-xr-x 1 root root 0 Apr 25 18:08 30-r--r--r-- 1 root root 0 Apr 25 17:19 cmdline-r--r--r-- 1 root root 0 Apr 25 17:19 cpuinfo-r--r--r-- 1 root root 0 Apr 25 17:19 filesystems-r--r--r-- 1 root root 0 Apr 25 17:19 interrupts-r--r--r-- 1 root root 0 Apr 25 17:19 loadavg-r--r--r-- 1 root root 0 Apr 25 17:19 meminfolrwxrwxrwx 1 root root 0 Apr 25 17:19 mounts ->self/mountslrwxrwxrwx 1 root root 0 Apr 25 17:19 net ->/proc/self/netlrwxrwxrwx 1 root root 0 Apr 25 17:19 self ->/proc/30-r--r--r-- 1 root root 0 Apr 25 17:19 statdr-xr-xr-x 1 root root 0 Apr 25 17:19 sys-r--r--r-- 1 root root 0 Apr 25 17:19 uptime-r--r--r-- 1 root root 0 Apr 25 17:19 versionいくつか見てみましょう。
root@PERSONAL:/proc#catmeminfoMemTotal: 1031052 kBMemFree: 680736 kBBuffers : 34032 kBCached : 188576 kBSwapCached : 0 kBActive : 167556 kBInactive : 157876 kB Active(anon) : 103104 kB Inactive(anon) : 17440 kB Active(file) : 64452 kB Inactive(file) : 140436 kB(略)あれ?meminfo自体珍しいことではないんですが...
root@PERSONAL:/proc#free-h total used free shared buffers cachedMem: 1.0G 342M 664M 0B 0B 0B-/+ buffers/cache: 342M 664MSwap: 0B 0B 0Bやはりメモリ合計が1.0GBで認識されているようですね。
このPC自体は4GBメモリを積んでいるのですが、固定なんでしょうか?詳細不明です。
root@PERSONAL:/proc#catcpuinfoprocessor : 0vendor_id : GenuineIntelcpu family : 6model : 69model name : Intel(R) Core(TM) i7-4500U CPU @ 1.80GHzstepping : 1microcode : 0xffffffffcpu MHz : 2394.000cache size : 256 KBphysical id : 0siblings : 4core id : 0cpu cores : 2apicid : 0initial apicid : 0fpu : yesfpu_exception : yescpuid level : 6wp : yesflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave osxsave avx f16c rdrandbogomips : 4788.00clflush size : 64cache_alignment : 64address sizes : 36 bits physical, 48 bits virtualpower management:(略)root@PERSONAL:/proc#catcpuinfo |grepprocessorprocessor : 0processor : 1processor : 2processor : 3これはまぁいいでしょう。HT込みの論理4コアはWindowsから見たものと同じです。
では/procは次でいったん最後に。
root@PERSONAL:/proc#catmountsrootfs / rootfs ro,relatime 0 0tmpfs /dev tmpfs rw,seclabel,nosuid,relatime,mode=755 0 0devpts /dev/pts devpts rw,seclabel,relatime,mode=600 0 0proc /proc proc rw,relatime 0 0sysfs /sys sysfs rw,seclabel,relatime 0 0以上、/procでした。
/mnt
おや、これは...。
root@PERSONAL:~#cd /mnt/root@PERSONAL:/mnt#ls-latotal 12drwxrwxr-x 2 root 1000 0 Jan 1 1970 .drwxrwxr-x 2 root root 0 Jan 1 1970 ..drwxrwxrwx 2 root root 0 Apr 25 17:20 cどうやらここからCドライブが見えるようです。
さきほど/proc/mounts を見ていますが、どうやってマウントしているかは謎です。
root@PERSONAL:/mnt#cdcroot@PERSONAL:/mnt/c#ls-lals: cannot access Documents and Settings: Input/output errorls: cannot access hiberfil.sys: Permission deniedls: cannot access pagefile.sys: Permission deniedls: cannot access swapfile.sys: Permission deniedtotal 8676drwxrwxrwx 2 root root 0 Apr 25 17:20 .drwxrwxr-x 2 root 1000 0 Jan 1 1970 ..-rwxrwxrwx 1 root root 398156 Jul 26 2012 bootmgr-rwxrwxrwx 1 root root 1 Apr 19 07:21 BOOTNXTdrwxrwxrwx 2 root root 0 Apr 3 16:33 Datad????????? ? ? ? ? ? Documents and Settingsdrwxrwxrwx 2 root root 0 Sep 5 2015 ESDdrwxrwxrwx 2 root root 0 Dec 25 17:27 HashiCorp-????????? ? ? ? ? ? hiberfil.sysdrwxrwxrwx 2 root root 0 Sep 6 2015 Inteldrwxrwxrwx 2 root root 0 Apr 25 17:20 OneDriveTemp-????????? ? ? ? ? ? pagefile.sysdrwxrwxrwx 2 root root 0 Apr 19 07:30 PerfLogsdrwxrwxrwx 2 root root 0 Apr 25 16:56 ProgramDatadrwxrwxrwx 2 root root 0 Apr 25 16:49 Program Filesdrwxrwxrwx 2 root root 0 Apr 25 16:49 Program Files (x86)drwxrwxrwx 2 root root 0 Apr 25 16:48 Recoverydrwxrwxrwx 2 root root 0 Aug 4 2015 $Recycle.Bin-rwxrwxrwx 1 root root 2243 Jan 4 2015 RHDSetup.logdrwxrwxrwx 2 root root 0 Sep 21 2015 Ruby22-x64drwxrwxrwx 2 root root 0 Jan 4 2015 sources-????????? ? ? ? ? ? swapfile.sysdrwxrwxrwx 2 root root 0 Sep 21 2015 System Volume Information-rwxrwxrwx 1 root root 8388608 Apr 25 16:42 tmpgfile.sysdrwxrwxrwx 2 root root 0 Mar 19 15:18 Updatedrwxrwxrwx 2 root root 0 Apr 25 16:49 Usersdrwxrwxrwx 2 root root 0 Dec 30 07:51 vagrantdrwxrwxrwx 2 root root 0 Apr 25 16:54 Windowsdrwxrwxrwx 2 root root 0 Apr 25 16:28 $WINDOWS.~BTdrwxrwxrwx 2 root root 0 Apr 25 16:39 Windows.olddrwxrwxrwx 2 root root 0 Sep 5 2015 $Windows.~WSパーミッションが全て777 のようで落ち着きませんが、MSDNのFAQ によると
All permissions are set to 777 (full permissions of the user who launched the bash session)
とのことで、bash実行ユーザの全権限みたいな感じのようです。
あとはパーミッションが? だったり、rootユーザなのにPermission deniedだったりと怪しい感じですね。当然ここはWindowsの領域なのでパーミッションっていう概念がそもそも合わないのであれこれ考えること自体がナンセンスかもしれないですね。
今後のアップデートでどうなっていくかを見守りましょう。
実体
さて、なんとなく探検を済ませてきましたが、こいつの実体は一体なんなのかという気持ちになってきました。
bashくん、君はいったいどこにいるの?
root@PERSONAL:~#which bash/bin/bashというように、bash自身はまるで「俺?Linuxの住人だけど?」とでも言いたそうな感じですが、悲しいかなここはWindowsです。
無慈悲にエクスプローラで検索をかけてみましょう。
おやおや、ずらずらと出てまいりました。
C:\Users\[Username]\AppData\Local\lxss\rootfs
見つけました。
どうやらここがLinuxにおけるrootのようです。lxssは「linux subsystem」あたりでしょうか。
ただし、隠しファイルを含めて全表示にしていたところで、lxss以降こいつを普通に辿ろうとしても見えません。
とはいえパスを直打ちすると普通に入れてしまいます。
中身を覗いてみると、
というように/bin と思われる場所にたくさんファイルがありました。
当然完全なバイナリファイルですが、触れ込みによるとこいつをUbuntuに持っていってもそのまま動くということなのでしょう。
一方で、/proc なんかを見てみると空っぽになっています。
動的ファイルが見えない感じなんでしょうか。これも詳細不明です。
自由探検
あとは個人的に気になるところをいくつかチェックします。
/mnt経由でまた/
先ほど、
/mntにCドライブがマウントされていること- ユーザディレクトリにrootfsが置かれていること
を確認しました。
もしや、/mntからrootfsに入り、また/mntからrootfsに入れるのでは...などと危険なことを考えました。
root@PERSONAL:~#cd /mnt/c/Users/[Username]/AppData/Local/lxss/rootfs/root@PERSONAL:/mnt/c/Users/[Username]/AppData/Local/lxss/rootfs#ls-latotal 128drwxrwxrwx 2 root root 0 Apr 10 17:55 .drwxrwxrwx 2 root root 0 Apr 25 17:19 ..drwxrwxrwx 2 root root 0 Apr 10 17:55 acctdrwxrwxrwx 2 root root 0 Apr 10 17:54 bindrwxrwxrwx 2 root root 0 Apr 10 17:54 bootdrwxrwxrwx 2 root root 0 Apr 10 17:55 cache(略)drwxrwxrwx 2 root root 0 Apr 10 17:55 usrdrwxrwxrwx 2 root root 0 Apr 10 17:55 varとりあえず予定通り、2回目のrootにやってくることができました。
ただし、実際のパーミッションは以下の感じなのであくまでWindows領域のファイルとして見えているようです。
root@PERSONAL:/#ls-latotal 125drwxrwxr-x 2 root root 0 Jan 1 1970 .drwxrwxr-x 2 root root 0 Jan 1 1970 ..drwxr-xr-x 2 root root 0 Apr 25 18:59 acctdrwxr-xr-x 2 root root 0 Mar 23 20:45 bindrwxr-xr-x 2 root root 0 Mar 23 20:54 bootdrwxrwx--- 2 1000 2001 0 Jan 1 1970 cache(略)drwxr-xr-x 2 root root 0 Mar 23 20:41 usrdrwxr-xr-x 2 root root 0 Mar 23 20:45 varさらに、mntの中身が空っぽだったのでもう1周とはいきませんでした。
root@PERSONAL:/mnt/c/Users/[Username]/AppData/Local/lxss/rootfs#cdmnt/root@PERSONAL:/mnt/c/Users/[Username]/AppData/Local/lxss/rootfs/mnt#ls-latotal 4drwxrwxrwx 2 root root 0 Apr 10 17:54 .drwxrwxrwx 2 root root 0 Apr 10 17:55 ..とても危険な香りのするアイデアだったのですが、残念です。
その他コマンド
以下のコマンドを試します。
- ps aux
- top
- netstat -tanp
- ifconfig
root@PERSONAL:~#ps auxUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.0 0 0 ? Ss 2385 0:00 /initroot 2 0.0 0.0 0 0 ? Ss 2385 0:01 /bin/bashroot 66 0.0 0.0 0 0 ? R 2385 0:00 ps auxPID 1でinitが動いているのは至極まっとうな感じがしますね。
root@PERSONAL:~#toptop - 19:05:44 up 1:46, 0 users, load average: 0.52, 0.58, 0.59Tasks: 3 total, 1 running, 2 sleeping, 0 stopped, 0 zombie%Cpu(s): 4.1 us, 49.1 sy, 0.0 ni, 46.6 id, 0.0 wa, 0.2 hi, 0.0 si, 0.0 stKiB Mem: 1031052 total, 350316 used, 680736 free, 0 buffersKiB Swap: 0 total, 0 used, 0 free. 0 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 0 0 0 S 0.0 0.0 0:00.01 init 2 root 20 0 0 0 0 S 0.0 0.0 0:01.12 bash 67 root 20 0 0 0 0 R 0.0 0.0 0:00.01 topさて、これがなんだという話なのですが、topコマンドはbuild 14316 では動かなかったんですよね。
私も「よーし、探検記事書くかー」と思った段階でさらっと触っていて噂通り動かないことを確認していました。
root@PERSONAL:~#netstat-tanpActive Internet connections (servers and established)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program namenetstatで見たところは、特にリッスンしたりしているポートはないように見えます。
root@PERSONAL:~#ifconfigWarning: cannot open /proc/net/dev (No such file or directory). Limited output.あら、NICすら見えないようですね。
root@PERSONAL:~#ping localhostping: icmp open socket: Socket type not supportedroot@PERSONAL:~#ping 127.0.0.1ping: icmp open socket: Socket type not supportedpingすら通らないとは一大事ですね。仕事中だったら泡吹いて倒れているところです。
FAQによれば、
Why isn't ICMP working?
This is a known issues that will effect several networking commands such as ping. Stay tuned for updates.
ネットワークがダメなことは分かってるから待っててね、とのことらしいです。気長に待ちましょう。
apt-get
Ubuntuファミリーとして、apt-getが使えるようです。
root@PERSONAL:~#apt-getinstallslReading package lists... DoneBuilding dependency treeReading state information... DoneThe following packages were automatically installed and are no longer required: libfreetype6 os-proberUse 'apt-get autoremove' to remove them.The following NEW packages will be installed: sl0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.Need to get 26.7 kB of archives.After this operation, 126 kB of additional disk space will be used.Get:1 http://archive.ubuntu.com/ubuntu/ trusty/universe sl amd64 3.03-17 [26.7 kB]Fetched 26.7 kB in 2s (9,698 B/s)E: Can not write log (Is /dev/pts mounted?) - openpty (2: No such file or directory)Selecting previously unselected package sl.(Reading database ... 25130 files and directories currently installed.)Preparing to unpack .../archives/sl_3.03-17_amd64.deb ...Unpacking sl (3.03-17) ...Processing triggers for man-db (2.6.7.1-1ubuntu1) ...Setting up sl (3.03-17) ...やった!SLが走った!
やった!牛がしゃべった!
シェル芸
シェル芸タグをつけながら芸っぽいことを何もやらないのはあれなのでシェル芸をひとつ。
root@PERSONAL:/#yestakasukurinikkuそうなんです。
残念ながら、まだこの Bash on Ubuntu on Windows 環境は日本語入力に対応していない のです。
しかし、安心してください。まだ道はあります。
Windows側から例のキーワードを入れたテキストファイルを作ってください。もちろんUTF-8で。
root@PERSONAL:/#KEYWORD=$(catyes_utf8.txt)root@PERSONAL:/#yes${KEYWORD}高須クリニック高須クリニック高須クリニック(略)このように、UTF-8であれば文字化けもなく表示可能なようです。
もう1つ。
このbash、Powershell含め従来のWindows CLIから起動できます。
Windows PowerShellCopyright (C) 2016 Microsoft Corporation. All rights reserved.PS C:\WINDOWS\system32> bash --versionGNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)Copyright (C) 2013 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>This is free software; you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.PS C:\WINDOWS\system32> bash -c "echo 'Hello Bash!'"Hello Bash!そして、Powershell自体は日本語入力に対応しています。つまり...
PS C:\WINDOWS\system32> bash -c "yes 高須クリニック"高須クリニック高須クリニック高須クリニック(略)のようなことが可能です。
build 14328
さて、記事を書いている途中でbuild 14328 に変わったわけですが、何が変わったのでしょうか。
色々書いてありますが、
Hostname is now set to the Windows computer name, no more @localhost
とあるように、プロンプトでWindowsのホスト名を引き継ぐこと、
top and htop now display
topが正しく表示されることはbuild 14328 からのようです。
初めてのbash公開から約2週間ですが、こうやって着実に良くなっていくことを期待しています。
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme









