Play the Linux subsystem (SSH service) in Windows 10

Source: Internet
Author: User
Tags windows 10 upgrade windows insider

On this year's Build 2016, Microsoft introduced the world to the Linux subsystem under Beta Windows Subsystem for Linux (WSL), which allows developers to use Ba in Windows 10 The SH shell runs the native Ubuntu user-state binary program. If you're involved in the Windows Insider program, you'll be able to experience this feature in the insider build of the latest Windows 10 upgrade version.
Web developers are no longer bothered by the lack of suitable Linux tools and libraries on the Windows development platform. WSL was designed and developed by the Windows kernel team in collaboration with Canonical, allowing developers under Windows 10 to use the rich development environment and tools under Linux without booting to another operating system or making With virtual machines. This is definitely a "developer, service Developer" feature of Windows 10, which is designed to make developers ' daily development work smoother and more convenient.
Playing the Linux subsystem in Windows 10
In this article, I'll show you some of the features I think are very interesting, as well as some resources that will let you find more information. First, I'll show how the main commands that WSL integrates (like SSH) operate on servers and devices. Second, I'll show you how to automate tasks in a concise manner using Bash scripting. Thirdly, I will play a lightweight classical hacker-level game with great command-line compilers, some other tools, and the ability to *nix compatible: NetHack. Finally, I'll show you how to use the existing Python scripts and other scripts from the Web.
Starting with Windows 3.0 running on my first 286, Windows has been my primary operating system and development environment. However, I also have many Linux servers and devices around me. From IoT devices such as Raspberry Pi and router/gateway devices, to Minecraft servers, they are piled up in every corner of my office. And I often have to manage and configure these Linux computers from my main workstation.
Managing servers and devices
I run a non-monitor Ubuntu Minecraft server in my home, a Christmas present for my 10-year-old son last year, but it has become my toy instead of his (well, mostly my toys). I used to manage it on my Windows 10 PC using several clients, but now I want to manage it using the SSH command line inside Bash in Windows. It is also possible to use applications like PuTTY or Tera from Cygwin, but I would like to try a truly native and natural experience is also a good choice. Cygwin is like a pizza ordered in a pizza shop, tasty, but not that atmosphere.
I have set up public-private key pairs using Ssh-keygen and Ssh-copy-id in WSL, so using SSH requires only the following simple inputs:


$ ssh <username>@<server>

I also created an alias for this to be faster. This is a standard Linux/bash feature:


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

Now, I need to access my Minecraft server only by entering "MC" in Bash under Windows 10.
Playing the Linux subsystem in Windows 10
Of course, the same approach can be used on any Linux Web or database server, or even Raspberry Pi or other IoT devices.
SSH is just for convenience in the terminal, but when you work in the shell, you have the possibility of automating various tasks when you have tools like APT, node, Ruby, Python, and so on.
Remote scripting
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 find out how long the remote server has been running since the last reboot, you just need to enter:


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

SSH connects to the server and executes the last-x command, and then searches for a row that contains "reboot". The results I ran on my Ubuntu Minecraft server were as follows:


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

This is just a server, and if you have many servers, you can automate the process. I'm in the WSL. A file named Servers.txt is created in my home directory, which contains a list of Linux server/device names, one for each row. Then I can create a script to read this file.
After using a device like a Raspberry Pi for many years, I've become a nano person (I'm a lsedit on the VMS), and here's the script I opened with my favorite nano editor.
Playing the Linux subsystem in Windows 10
Of course, you can also use Vim, Emacs, or other editors that can be used on Ubuntu terminals.
The script is the Bash script, to execute the script, enter:


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

It iterates through each server/device in the output file, and then executes the command remotely via SSH. Of course, this is a very simple example, but you can turn your local script or other command into a remote one like this. The Bash scripting language is rich enough so you can use it to accomplish most of your remote administration tasks. You can extend its use by downloading it to other applications in the WSL or remote system.
Do you need to use your local Windows files or resources for other Linux computers at work? Or, you don't use Linux at all? Can Bash manipulate local Windows files or resources, or is it a completely separate environment?
Using Windows files
The WSL system can access the file system on your computer through the/mnt/< disk number >/directory (mount point). For example, the C + + on your Windows and D:\ The root directory can be accessed appropriately through/mnt/c and/mnt/d in WSL. This is useful when you want to use the project files, downloaded content, and other files under your Windows in Linux/bash.
Playing the Linux subsystem in Windows 10
The two directories shown correspond to the SSDs and hard drives on my computer, respectively:
Playing the Linux subsystem in Windows 10
This is a logical mount, so when you use a command like mount in your shell, they do not display. But they can work as you expect. For example, in Windows, I have a file named Test.txt in my C packing directory, and I can access it as follows in WSL:
Playing the Linux subsystem in Windows 10
During the Build tour, we want to make sure that all the demos are working without the Internet (you never know what the web is like), so for BASH/WSL to demonstrate the Git operation, the demo accesses the Windows file on the local computer, and I A local repository is set up under C:\git\NetHack on Windows. To do a 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 located under/mnt/c/git/nethack. You can access all the files under your Windows in a similar way.
Warning: Just like in other terminals, if you are not careful, you can modify/delete files in the Windows file system in Bash. For example, you can kill your Windows like this, if you have the right permissions.


$ rm-rf/mnt/c/[Don't try!] [Don't try!] [Don't try!] ]

I'm seriously reminded that many of us are just touching Linux commands, which are not Windows commands.
This magic that allows file systems to be integrated comes from DRVFS. [1] If you want to know more details about the filesystem and how it works in WSL, the WSL team wrote a detailed article about it.
Of course, file system access is only part of the functionality of WSL, and many development tasks require access to remote resources over HTTP or other network protocols.
Initiating an HTTP request
It is useful to initiate REST or other HTTP (or FTP) requests from a script or command line rather than from a compiled program or Web page. As in most Linux distributions, WSL also includes standard features like curl or wget to get resources that can be used to initiate HTTP or other network requests. For example, here's a REST request for Github using curl to get my personal property information:


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


http/1.1 OK


Server:GitHub.com


date:wed, 02:38:08 GMT


Content-type:application/json; Charset=utf-8


content-length:1319


status:200 OK


...


{
"Login": "Psychlist1972",
"Avatar_url": "Https://avatars.githubusercontent.com/u/142****46?v=3",
"url": "https://api.github.com/users/Psychlist1972",
"Name": "Pete Brown",
"Company": "Microsoft",
...
}


$

You can use it and Bash scripts to create a quick test client for the REST API, or to probe a Web page or server and report the status of its return. It is also great to download files from the Web, and you can simply redirect the output to a file instead of displaying it on the screen:


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

I am also a PowerShell user, and even use Windows ten MIDI in powershell[2] to create some interesting extensions and also fix [3] Some file problems that appear on specific recording hardware devices. As a long-time. NET developer and enthusiast, I often use and extend PowerShell to meet my project needs. But PowerShell is not a place to run all of those Bash scripts and open source tools for Linux. I want to accomplish these tasks in the simplest and most comfortable way, in a sense, which means we need to do them in Bash.
I've been through this. Describes bash, bash scripts, and tasks you can do in the shell. So far, I've been talking about the features that contribute to the development effort. But what is the actual development and compilation work in WSL? I demonstrated the following section at the Build Tour conference.
Demo at Build Tour Conference: NetHack
This early summer, speakers from Microsoft showed you some cool developer new features from Windows and Microsoft cloud. As part of this, I demonstrated the WSL in a playful way, and it's a way of being relevant to developers.
I personally want to show the use of Git and some of the traditional terminal development tools, I have written the Bash demo program, including these basics (in Python and Ruby written "Hello World"), but I still want to be more impact.
I recalled my time at college, 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 graphics workstations simply open multiple terminal sessions in different windows and, of course, put a cool moonphase chart on the desktop background. Most of the students use the VT-220 terminal to open their session (the school is not far from Boston, so we have a lot of DEC equipment).
At that time, the students of the computer department played two major games: MUD (mainly LPMud and DikuMUD) and Nethack[4]. NetHack and other roguelikes[5] games are considered one of the most influential games in history, and they are the originator of many of the now-popular Dungeon Adventures and role-playing games.
NetHack has a long history, and now it contains hundreds of thousands of lines of *nix code from decades ago, and some of the code that was added later. The game uses curses[6] (and its substitutes) as a terminal interaction, and needs to be built through Lex, YACC (or Flex and Bison) and CC (or GCC), and a bunch of other development tools.
It is written in C and includes a number of complex script configuration features written in Bourne Shell[7]. I think it's a nice and interesting way to embody the capabilities of WSL and Bash developers on Windows 10. Because of the use of curses (which is the Libncurses library in Linux and WSL), it can also be used to showcase terminal emulation capabilities in command-line windows in Windows 10.
Previously, building NetHack from source code on our timeshare Ultrix server took a lot of our time, and now it takes only a few minutes on my pc to get it done. I like this kind of technological progress. Configuring and compiling NetHack on Linux or WSL is easy and complex in two ways. In order to save time, we will do it in an easy way.
Front-facing requirements
First, update your WSL environment to make sure your software is up-to-date. This is a good practice before installing a new package.


$ sudo apt update


$ sudo apt upgrade

Then, install the necessary development tools. The easiest way to do this is to use the Build-essential package, which includes the vast majority of programs that Linux developers need to build software that is developed in C/s + +.


$ sudo apt install build-essential

It will take a few minutes. If you want to know more, you can install the GCC, GDB, make, Flex, bison, and other tools mentioned in the NetHack documentation separately. But if you are a developer, sometimes you may need some other tools. Build-essential basically provides the set of tools you need.
Then, install Git. As you can imagine, it's easy to:


$ sudo apt install git

Just like in Linux, you can add a git PPA to get a newer version, but here we have one on the line.
Finally, we need to install curses (actually ncurses) to interact with the terminal screen.


$ sudo apt install Libncurses-dev

Once we've completed these steps, we're ready to start building nethack.
Build NetHack
The official NetHack warehouse is placed on github[8], first we need to grab it and put it in our home directory.


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

Playing the Linux subsystem in Windows 10

Http://www.infocool.net/kb/Windows/201705/355764.html

To play the Linux subsystem (SSH service) in Windows 10

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.