Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Run a Linux Shell on Windows

DZone's Guide to

Run a Linux Shell on Windows

Learn how you can get all the favorite features of a Linux shell, including a high-resolution command line tool, on a Windows environment.

· Cloud Zone
Free Resource

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

The Windows 10 anniversary update brought a new feature called “bash on Ubuntu on Windows.” This feature provides a basic bash environment that allows users to run common (basic) command line utilities such as grep, sed, and awk via a Ubuntu bash. However, the tool is very limited and far from being an alternative to a fully fledged Linux VM, as it doesn’t handle Docker, Oracle Java, or other significantly important tools for any developer.

On the other hand, the idea of accessing a bash terminal by simply running a Windows app is a very nice one, as opposed to, for example, starting the VirtualBox app, selecting a VM, clicking on start, watching as the VM itself boots a graphic environment to be able to have a high-resolution fullscreen terminal, etc, etc.

To have the best of both worlds, i.e. a fully fledged Linux environment and a native, high-resolution command line tool that seamlessly connects you to that environment, I used two free applications (VirtualBox and cmder), and I made a handy batch script that checks if the Linux VM is running, starts it if it isn’t, then connects to it via SSH. All the end user does is start the command line tool and run the script — you get a high-resolution bash that is connected to a fully fledged Linux VM.

Setup

Port Forwarding

To establish an SSH connection from the host to the VM, we attach the network adapter to NAT in the VM network settings and, in port forwarding, we establish a TCP rule to forward connections from the Windows host’s port 2222 to the Linux guest’s port 22. That way, when we issue the following in our Windows host...

ssh jihedamine@127.0.0.1 -p 2222

...it is forwarded to our linux VM as

ssh jihedamine@127.0.0.1 -p 22

Port 22 is the default port where the OpenSSH server listens to SSH connection requests.

SSH key

If we don’t want to be asked for our user password every time we connect to the Linux VM via SSH, we can generate an SSH key in our Windows machine...

ssh-keygen.exe

...and append the content of the generated file ~\.ssh\id_rsa.pub to the ~/.ssh/authorized_keys file in our Linux VM.

Create Scripts

Now to the interesting part!

Start and Connect to the VM (run-ubuntu.bat):

@echo off 
for /f "tokens=\* usebackq" %%i in ( 
    '"c:\Program Files\Oracle\VirtualBox\VBoxManage.exe" showvminfo Ubuntu ^| grep -c "running"' 
) do set var=%%i 

%var% > ubuntu.txt  

for /f "delims=" %%x in (ubuntu.txt) do set isUp=%%x  

if "%isUp%" == "0" ( 
    "c:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm Ubuntu --type headless 
) 

ssh jihedamine@127.0.0.1 -p 2222


Stop the VM (stop-ubuntu.bat):

@echo off 

for /f "tokens=\* usebackq" %%i in ( 
'"c:\Program Files\Oracle\VirtualBox\VBoxManage.exe" showvminfo Ubuntu ^| grep -c "running"' 
) do set var=%%i 

%var% > ubuntu.txt  

for /f "delims=" %%x in (ubuntu.txt) do set isUp=%%x  

if not "%isUp%" == "0" ( 
"c:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm Ubuntu poweroff 
)


Advantages

In my opinion, this setup, with cmder and a headless VirtualBox VM, is a very nice way to get all the Unix tools power and a very powerful command line environment via an application that runs natively on your Windows desktop environment.

How it Looks

This video capture shows a cmder app running on Windows, in which we execute the run-ubuntu.bat script. The script starts up a headless Ubuntu VM or connects to the already-started one. From there, interact with your Unix system as you wish.

alt txt








Advantages Over Bash on Ubuntu on Windows

  • Full-blown Unix environment where you can run Docker, PostgreSQL, and whatever app/tool that’s available.

Advantages over using the VirtualBox GUI

  • Better integration with Windows OS (easy switching between cmder app and other windows apps like a running IntelliJ Idea instance, your Chrome browser, or your iTunes app).
  • High-resolution console (as opposed to the low resolution console displayed by VirtualBox when no XServer is running) and no separate desktop environment (as opposed to running Gnome/Kde/xfce on the VirtualBox VM).

Conclusion

Bash on Ubuntu on Windows is a good feature, but it currently is a very limited tool. Running a Ubuntu desktop environment inside a VM manager on Windows isn’t as seamless and straighforward as accessing your terminal app in a Linux or Mac OS. The cmder/VirtuablBox/Ubuntu setup uses free software, so feel free to test this config for yourself and see if it suits your needs. 

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

Topics:
vm ,virtualbox ,cloud ,ssh

Published at DZone with permission of Jihed Amine Maaref. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}