TranslateProject/sources/tech/20200519 How to use Windows Subsystem for Linux to open Linux on Windows 10 machines.md
DarkSun 6d25c49ab9 选题: 20200519 How to use Windows Subsystem for Linux to open Linux on Windows 10 machines
sources/tech/20200519 How to use Windows Subsystem for Linux to open Linux on Windows 10 machines.md
2020-05-25 19:44:52 +08:00

8.7 KiB
Raw Blame History

How to use Windows Subsystem for Linux to open Linux on Windows 10 machines

Opening a Linux terminal on a Windows 10 desktop can help you practice your Linux skills and explore Windows from an entirely different point of view. In this post, we look at Ubuntu 18.04 running through Windows Subsystem for Linux (WSL). Nicolas Solerieu modified by IDG Comm. / Linux (CC0)

Believe it or not, it's possible to open a Linux terminal on a Windows 10 system and you might be surprised how much Linux functionality youll be able to get by doing so.

You can run Linux commands, traipse around the provided Linux file system and even take a novel look at Windows files. The experience isnt altogether different than opening a terminal window on a Linux desktop, with a few interesting exceptions.

[Get regularly scheduled insights by signing up for Network World newsletters.]

What is needed to make this happen is something called the Windows Subsystem for Linux (WSL) and a Windows 10 x86 PC.

Linux versions for WSL

There are a number of options for running Linux on top of Windows. The Linux OS choices include:

Ubuntu 18.04 LTS is just one option and, in this post, well take a look at how the terminal runs on Windows using this particular distribution and how much it feels like working on a Linux system directly.

If you want to look into the process of putting an Ubuntu distribution on your Windows system, you can start with this page:

https://ubuntu.com/tutorials/tutorial-ubuntu-on-windows#1-overview

As part of the initial setup of installing your Linux on Windows terminal, youll be asked to create your user account. Once you do that and open the terminal, you can start to explore. One of the most noticeable differences between your Linux-on-Windows terminal and a terminal window on a Linux system is that examining processes isnt going to show you much. After all, Windows will be providing the bulk of the required OS support. Youre likely to see something like this:

myacct@hostname:~$ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 12:45 ?        00:00:00 /init
root         7     1  0 12:45 tty1     00:00:00 /init
shs          8     7  0 12:45 tty1     00:00:00 -bash
shs        166     8  0 13:32 tty1     00:00:00 ps -ef

Yes, that's it.

If youre anything like me, one of your next moves might be to get a handle on the available commands. If you just count the files in the /bin and /usr/bin directories, you should see that there are a lot of commands:

myacct@hostname:~$ ls /bin | wc -l
171
myacct@hostname:~$ ls /usr/bin | wc -l
707

You can list available commands with commands like these (output truncated for this post):

myacct@hostname:~$ ls /bin | head -25 | column
bash              btrfs-map-logical   bunzip2          bzegrep           bzip2recover
btrfs             btrfs-select-super  busybox          bzexe             bzless
btrfs-debug-tree  btrfs-zero-log      bzcat            bzfgrep           bzmore
btrfs-find-root   btrfsck             bzcmp            bzgrep            cat
btrfs-image       btrfstune           bzdiff           bzip2             chacl

myacct@hostname:~$ ls /usr/bin | head -25 | column
NF            aa-exec             apport-cli      apt                apt-extracttempl*
VGAuthService acpi_listen         apport-collect  apt-add-repository apt-ftparchive
X11           add-apt-repository  apport-unpack   apt-cache          apt-get
[             addpart             appres          apt-cdrom          apt-key
aa-enabled    apport-bug          apropos         apt-config         apt-mark

You can update the system with apt commands (sudo apt update, sudo apt upgrade). You can even use Linux commands to move to the Windows disk partitions as you like and . Notice the last three entries in the output below. These represent several drives on the system.

myacct@hostname:~$ df -k
Filesystem     1K-blocks      Used Available Use% Mounted on
rootfs         973067784 326920584 646147200  34% /
none           973067784 326920584 646147200  34% /dev
none           973067784 326920584 646147200  34% /run
none           973067784 326920584 646147200  34% /run/lock
none           973067784 326920584 646147200  34% /run/shm
none           973067784 326920584 646147200  34% /run/user
cgroup         973067784 326920584 646147200  34% /sys/fs/cgroup
C:\            973067784 326920584 646147200  34% /mnt/c        <== C drive
I:\            976760000 231268208 745491792  24% /mnt/I        <== external drive
L:\            409599996    159240 409440756   1% /mnt/l        <== USB thumb drive

If youre interested in moving out of the Linux space and into the Windows portion of the file system within your WSL session, you can do that easily. Replace “myname” with your Windows account name and a cd /mnt/c/Users/myname/Desktop will take you to your Windows desktop. From there, dont be surprised if in listing your files you see WRL####.tmp files that dont seem to exist when you look at your desktop and dont show up if you look at your files by opening a command prompt. These appear to be temporary files used by Windows for document management. You might also see files listed that look like ~$nux notes.docx perhaps ghosts of files that were once located on your desktop. You wont see those files when you look at your desktop on Windows even using a cmd window.

Note that youll also see Windows directories such as Program Files in single quotes when listed in your Linux terminal as you would any file with blanks included in their names. You can even start a Windows executable from your Linux terminal. For example:

myacct@hostname: $ cd /mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0
myacct@hostname: $ powershell.exe

If you do this, type exit when you want to end the powershell session.

Linux commands all seem to work as expected, though I dont get any output when I run the who command.

Windows .txt files will display with cat commands, but the last line in a file will likely be displayed on the same line as the following shell prompt. This is because these files wont end with a linefeed as Linux text files do.

You can create other accounts and switch user to them (e.g., su nemo) if you like, but not log into them directly.

You can also update the system with apt commands (sudo apt update, sudo apt upgrade).

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.


via: https://www.networkworld.com/article/3543845/how-to-use-windows-subsystem-for-linux-to-open-linux-on-windows-10-machines.html

作者:Sandra Henry-Stocker 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出