Go to the Linux subsystem in Windows 10

Source: Internet
Author: User
Tags how to use git

Go to the Linux subsystem in Windows 10
GuideAt Build 2016 this year, Microsoft introduced to the world that they are still in Beta stage.Linux subsystem in Windows(WSL), which allows developers to run the native Ubuntu user-state binary program in Windows 10 through Bash shell. If you are involved in the Windows Insider program, you can experience this function in the Insider build version of the latest Windows 10-year upgrade.

Web developers do not have to worry about the lack of appropriate Linux tools and libraries on the Windows development platform. WSL is designed and developed by the Windows Kernel team in partnership with Canonical, so that developers in Windows 10 can have strong support in Windows, you can also use a variety of development environments and tools in Linux, instead of starting to another operating system or using virtual machines. This is definitely a "developer, service developer" Windows 10 feature. It aims to make every day's development work smooth and convenient for developers.

In this article, I will show you some functions that I think are very interesting and some resources that can help you find more information. First, I will show how the main Commands (such as ssh) integrated by WSL operate servers and devices. Second, I will demonstrate how to use Bash scripts to automate tasks in a concise manner. Third, I will use awesome command line compilers, some other tools, and * nix-compatible capabilities to play a lightweight classical hacker-level game: NetHack. Finally, I will show you how to use existing Python scripts and other scripts from the Internet.

Since running Windows 286 on my first Server 3.0, Windows has always been my main operating system and development environment. However, there are many Linux servers and devices around me. From Iot devices like Raspberry Pi and routers/gateways to Minecraft servers, they are full of every corner of my office. I often manage and configure these Linux computers from my main workstation.

Manage servers and devices

I am running a Ubuntu Minecraft server without a monitor in my home. This is a Christmas gift I gave my ten-year-old son last year, but it has become my toy rather than his (well, mainly my toy ). I used to use several clients on my Windows 10 computer to manage it, but now I want to use the ssh command line in Bash in Windows to manage it. You can also use applications such as PuTTY or javasterm from Cygwin, but it is also a good choice to try a real native and natural experience. Cygwin is like a pizza ordered at a pizza shop. It's delicious, but it doesn't have that atmosphere.

I have set up a public-private key pair using ssh-keygen and ssh-copy-id in WSL, so you only need to enter the following simple input to use ssh:

$ ssh <username>@<server>

I also created an alias for this to make it faster. This is a standard Linux/Bash function:

$ alias mc='ssh <user>@<server>'

Now, to access my Minecraft server, you only need to enter "mc" in Bash under Windows 10.

Of course, you can use the same method on any Web or database server on Linux, or even Raspberry Pi or other Iot devices.

Ssh in the terminal is only for convenience, but when you work in shell, if there are tools like apt, node, Ruby, Python, and so on, you have the possibility of automating all kinds of work.

Remote Script

If you have a lot of Linux servers and devices, and you want to execute a remote command on them, if you have configured a public/private key pair, you can execute commands remotely in Bash.

For example, to know how long the remote server has been running since the last restart, you only need to enter:

$ ssh <user>@<server> 'last -x|grep reboot'

Ssh will connect to the server and execute

last -x

Command, and then search for a line containing "reboot. My running results on my Ubuntu Minecraft server are as follows:

reboot system boot 4.4.0-28-generic Thu Jul 7 08:14 still running

This is just a server. If you have many servers, you can automate this process. I created a file named servers.txt under my home directory in WSL, which contains a series of Linux Server/device names, each row. Then I can create a script to read the file.

After using devices like Raspberry Pi for many years, I have become a nano (I am an LSEdit person in VMS ), below is the script I opened with my favorite nano editor.

Of course, you can also use vim, emacs, or other editors that can be used on Ubuntu terminals.

This is a Bash script. to execute this script, enter:

$ ./foreachserver.sh 'last -x|grep reboot'

It iterates each server/device in the output file and then remotely executes the command through ssh. Of course, this example is very simple, but you can change your local script or other commands to remote ones like this. Bash script language is rich enough, so you can use it to complete most of your remote management tasks. You can use WSL or other applications in the remote system to extend its usage.

Do you need to use local Windows files or resources for other Linux computers at work? Or are you not using Linux at all? Bash can operate on local Windows files or resources, or is it a completely independent environment?

Use Windows files

The WSL system can access the file system on your computer through/mnt/<disk number>/directory (mount point. For example, the C: // and D:/root directories on Windows can be accessed through/mnt/c and/mnt/d in WSL. This is useful when you want to use your project files, downloaded content, and other files in Windows to Linux/Bash.

The two displayed directories correspond to the SSD and hard disks on my computer respectively:

This is a logical mount, so they are not displayed when you use commands like mount in shell. But they can work as expected. For example, in Windows, I put a file named test.txt under the root directory of my C drive. I can access it in WSL as follows:

During the Build Tour Conference, we need to ensure that all the demonstrations can work normally without the Internet (you will never know what the network of the venue looks like ), so in order to allow Bash/WSL to demonstrate Git operations, the demo accesses Windows files on the local computer, and I set up a local repository under C:/git/NetHack on Windows. To perform the clone operation in WSL, I executed the following command:

$ git –clone file:///mnt/c/git/NetHack

This command tells git to use the file: // protocol and clone the repository in/mnt/c/git/NetHack. You can access all files in Windows in a similar way.

Warning: Like on other terminals, if you are not careful, you can modify/delete files in Windows file systems in Bash. For example, you can destroy your Windows as follows, if you have the right permissions.

$ Rm-rf/mnt/c/[never try again!] [Never try!] [Never try!]

I solemnly remind you that many of us are new to Linux commands, which are not Windows commands.

This magic that allows the file system to be integrated comes from DrvFs. If you want to learn more about the file system and how it works in WSL, The WSL team wrote a detailed article about it.

Of course, file system access is only part of WSL. Many development tasks also need to access remote resources through HTTP or other network protocols.

Initiate an HTTP request

It is useful to initiate REST or other HTTP (or FTP) Requests from scripts or command lines instead of from a compiled program or Web page. Like in most Linux distributions, WSL also includes standard functions such as curl or wget to obtain resources, which can be used to initiate HTTP or other network requests. For example, the following uses curl to initiate a REST request to Github to obtain my personal property information:

$ curl -i https://api.github.com/users/Psychlist1972HTTP/1.1 200 OKServer: GitHub.comDate: Wed, 13 Jul 2016 02:38:08 GMTContent-Type: application/json; charset=utf-8Content-Length: 1319Status: 200 OK...{  "login": "Psychlist1972",  "avatar_url": "https://avatars.githubusercontent.com/u/1421146?v=3",  "url": "https://api.github.com/users/Psychlist1972",  "name": "Pete Brown",  "company": "Microsoft",   ...}$

You can use it and the Bash script to create a restful API quick test client. It can also be used to detect a Web page or server and report the returned status. It is also great to download files from the Internet. You can simply redirect the output to a file rather than display it on the screen:

$ curl -i https://api.github.com/users/Psychlist1972 > pete.json

I am also a PowerShell user. I even used Windows 10 MIDI in PowerShell to create some interesting extensions and fixed some file problems that may occur on specific recording hardware devices. As a long-time. NET developer and enthusiast, I often use and extend PowerShell to meet my project needs. However, PowerShell is not a place where you can run all the Bash scripts and open-source tools for Linux. I want to complete these tasks in the simplest and most comfortable way. In a sense, this means we need to complete them in Bash.

I have already introduced Bash, Bash scripts, and tasks that you can complete in shell. So far, I have been talking about the features that help the development work. But what is the actual development and compilation work in WSL? I demonstrated the following section at the Build Tour Conference.

Demonstration at Build Tour Conference: NetHack

At the beginning of this summer, speakers from Microsoft demonstrated some cool new developer features from Windows and Microsoft cloud. As part of this, I demonstrated WSL in a fun way, and it is related to developers.

I personally want to demonstrate how to use git and some traditional terminal development tools. I have already written the Bash demo program, it includes these basic things ("Hello World" written in Python and Ruby), but I still want to have more impact.

I think back to my college days when we were tossing between Unix (DEC Ultrix and SunOS) and VAX/VMS, and Unix was almost entirely a command line environment. In our school, the vast majority of users who use graphic workstations only want to open multiple terminal sessions in different windows. Of course, they will put a cool monthly chart on the desktop background. Most students use VT-220 terminals to open their sessions (the school is not far from Boston, so we have many DEC devices ).

At that time, computer students mainly played two major games: MUD (lpMUD and DikuMUD) and NetHack. NetHack and other Roguelikes games are regarded as one of the most influential Games in history. They are the originator of many popular dungeon adventures and role-playing games.

NetHack has a long history. Now it contains hundreds of thousands of lines of * nix code from decades ago and some code that was later supplemented. The game uses curses (and its alternatives) as a terminal interaction mode and needs to be built through lex, yacc (or flex and bison), cc (or gcc), and a bunch of other development tools.

It is written in C and includes some complex script configuration functions written using the Bourne shell. I think it is a good and interesting way to reflect the developer capabilities of WSL and Bash on Windows 10. Because curses (libncurses library in Linux and WSL) is used, it can also be used to demonstrate terminal simulation capabilities in the command line window in Windows 10.

In the past, it took us a lot of time to build NetHack from the source code on our time-sharing Ultrix server, but now it takes only a few minutes on my PC. I like this technological progress. Configuring and compiling NetHack on Linux or WSL can be both easy-to-use and complex. To save time, we will do it in an easy way.


First, update your WSL environment to make sure your software is up to date. This is a good practice before installing a new software package.

$ sudo apt update$ sudo apt upgrade

Install necessary development tools. The simplest way is to use the build-essential software package, which includes the vast majority of programs required by Linux developers to build software developed in C/C ++.

$ sudo apt install build-essential

It takes several minutes. If you want to learn more, you can install other tools mentioned in gcc, gdb, make, flex, bison, and NetHack. However, if you are a developer, you may need some other tools. Build-essential basically provides the set of tools you need.

Then install git. As you think, it is easy:

$ sudo apt install git

Just like in Linux, you can add a git PPA to get a newer version, but here we just have one.

Finally, we need to install curses (actually ncurses) for terminal screen interaction.

$ sudo apt install libncurses-dev

After completing these steps, we can start to build NetHack.

Construct NetHack

The official NetHack repository is put on GitHub. First, we need to capture it and put it in our main directory.

$ cd ~$ git clone http://github.com/NetHack/NetHack

Because NetHack supports many operating systems, we need to make some basic configuration to tell it that we are using Linux, and use open source gcc to replace the function of cc on Unix.

As I mentioned, there are several ways to achieve this. Some people want to put the configuration information in the hints file. Believe me, using the hints file will avoid a lot of trouble mentioned in the GitHub repository. The README file and other documents do not focus on how to use the hints file. We can do this:

$ cd NetHack/sys/unix$ ./setup.sh hints/linux

This will set Makefile to correctly use the tools, libraries and their paths in Linux. This setting script is very powerful and has done a lot of configuration work. I'm glad it works well in WSL. If you are curious about how the script is written, you can use your editor to open it.

Then, start the final build:

$ cd ~/NetHack$ make all

After the build is complete, you need to install it. This is to copy the executable file to the target location:

$ make install

It will be installed to your ~ In the/nh folder, place NetHack IN ~ /Nh/install/games directory named nethack. To run it, switch to the directory (or enter the full path) and enter:

$ cd ~/nh/install/games$ nethack

Then, the screen is displayed and you can play NetHack. Note that everything is done in this Ubuntu Linux environment and does not require anything unique to Windows.

Play NetHack

Due to the limitations of terminal games and the complexity of NetHack, it is only possible here. There are some mysteries for those who first came into contact with it, but I think our programmers are never afraid of the unknown challenges.

The direction keys are the same as those in vi (vim). HJKL is left, bottom, top, and right. To exit the game, you can find the stairway exit on the top floor of the underground city and use it, or press the CTRL-C to force the exit.

In NetHack, the @ symbol represents yourself. Each floor consists of a room, corridor, door, And a stairway up or down. Monsters, treasure boxes, and items are comprised of various ASCII characters, and you will gradually become familiar with them. In order to comply with Roguelikes game specifications, there is no disk storage function, you only have one life. If you die, you can only play the game again. The underground city environment is randomly generated, and various items are disrupted.

The NetHack game aims to survive in the underground city, collect gold and items, and try to kill various monsters as much as possible. In addition to these purposes, you are constantly seeking for them. The rules generally follow the "Dragon and underground city (DnD)" weapons, skills, and other rules.

The following NetHack screenshot shows three rooms and two corridors. The upward stair is in the upper left corner of the room. I am now in the upper right corner of the room, there are some treasure chest and other items.

If no color is displayed in your game, you can create ~ /. Nethackrc file, and put the following content:


Note: If the ASCII character graph is not your dish, but you like this type of game, you can search "roguelike" in the Microsoft Store to find a visually better game.

Of course, NetHack is very old and may only be liked by people of a specific age group. However, building it uses a large number of important development tools and * nix operating system functions, as well as terminal simulation functions. From here we can see that from gcc, gdb, make, bison and flex to more modern git, all work well in WSL.

If you want to see the demonstration at the Build Tour Conference, you can see this lecture at the Build Tour Canada conference. This section of WSL starts.

I hope you will enjoy exploring the NetHack underground.

C and C ++ are both great, just like other classic development tools. You can even use common Bash scripts. However, many developers prefer to use Python as their scripting language.


You can find many examples of Python scripts on the Internet, which means that Python is becoming more and more popular and useful. Of course, in most cases, these examples are run in Linux. In the past, we had to have another machine installed with Linux to run them, or use virtual machines and multi-boot, otherwise, you need to modify something to make them run in the Python environment in Windows.

This is not an unsolved problem, but it will gradually kill the daily lives of developers. With WSL, you have a compatible subsystem with Python functions and shell variables.

To install the latest Python development version and Python package installer pip, run the following command in Bash:

$ sudo apt install python-pip python-dev$ sudo pip install --upgrade pip

Now that Python is installed, I want to show you how to obtain a typical Python example in Linux from the Internet and let it work directly. I went to the Activestate Python menu station to find a top Python example. Well, I am just a little confused. The top one is the script that prints the integer name, which seems boring, so I chose the second place: Tetris. We can see Python in various places every day, so this time let's play another game.

I opened the nano Editor, copied the 275 lines of Python code from the page opened in a Windows browser, and pasted it into the nano in my WSL terminal window terminal, and save it as tetris. py, and then execute it:

$ python tetris.py

It immediately clears the screen and shows the game of Tetris. Like NetHack, you can use the direction keys of the same vi standard to move (in the past, the mouse and the WSAD key were used to move, while the right hand was more convenient to use the HJKL key ).

As I mentioned, you can certainly run Python in Windows without WSL. However, to be quick and easy, you do not need to modify the Python code in Linux, but simply copy and paste the code to run it, which can greatly improve the efficiency of developers.

This is true. This is not a substitute for Windows native tools, such as Python, PowerShell, C #, etc. It is when you need to quickly and effectively complete some things in modern development workflows, it can avoid all kinds of tossing.

Including Bash, Python, and all other native Linux Command Line development tools. WSL provides all the required tools for my development work. This is not a Linux server, or even a complete client. On the contrary, it is a tool that can help me avoid daily difficulties, let me develop something more efficient and exciting on Windows!

Reset your WSL Environment

Try it. If you break down your WSL environment, it will be easy to reinstall. Before proceeding, make sure that you have backed up any important content.

C:/> lxrun.exe /uninstall /fullC:/> lxrun.exe /install
How do you feel when using Bash and WSL?

We hope that WSL, especially Bash, can help you increase efficiency and reduce the daily development difficulties.

What do you think about WSL on Windows 10? Do you like to use it?

The development team has done a lot of work to make WSL a powerful terminal tool for developers. If you have any feedback or running problems, we recommend that you check the GitHub feedback page and the user voice feedback and voting site. We really want to hear your voice.
Linux shell programming is a huge topic, and there are a lot of content on the Internet. If you are not familiar with them yet and want to learn more, you can take a look at various Bash tutorials. You can start with this.

From: https://linux.cn: 443/article-7613-1.html

Address: http://www.linuxprobe.com/windows-linux-system.html

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.