Skip to main content

Creating a Linux-Windows hybrid system with Cygwin

When you need a consistent scripting platform in a heterogeneous data center, Cygwin delivers.

If you've never heard of Cygwin, you're in for a treat. I've used it for years to homogenize my environments into one where I can write scripts once and deliver them everywhere for consistent automation solutions. It's a pain to write bash scripts that work on Linux, on Macs, and on commercial Unices, and then have to jump headlong into PowerShell and batch files to attempt to match the level of automation that I can achieve with some Bash scripting.

According to the website, Cygwin is:

  • A large collection of GNU and Open Source tools that provide functionality similar to a Linux distribution on Windows.
  • A DLL (cygwin1.dll) which provides substantial POSIX API functionality.

And it is not:

  • A way to run native Linux apps on Windows. You must rebuild your application from source if you want it to run on Windows.
  • A way to magically make native Windows apps aware of UNIX® functionality like signals, ptys, etc. Again, you need to build your apps from source if you want to take advantage of Cygwin functionality.

Note: Cygwin works equally well on Windows desktops and on server systems.

Why Cygwin?

Cygwin is still relevant if you wish to have native *nix commands available to you on the Windows platform. Yes, I know about Windows Subsystem for Linux (WSL), but Cygwin and WSL aren't the same. Cygwin gives you the power of *nix commands in your native Windows environment. There's no emulation, no virtual machine, and no extra layers between the operating system and this collection of utilities. They are, in fact, authentic open source commands compiled for the Windows platform. 

Installing Cygwin

Generally, you can accept the default option for each prompt. I provide guidance when there are choices to be made. You can select your own, but I've found that these selections work best in my environments and for the way I work.

Open a browser on your Windows system and point it to the Cygwin web site. Select the Install Cygwin by running setup-x86_64.exe link to download the setup executable file. Run the executable file to begin the installation and click Next on the Cygwin Setup screen.

On the Choose Installation Type screen, select the type of installation you want. For myself, I always select the default, which is Install from Internet.

Image
Installation Type

Click Next to continue.

Select where you want the Cygwin directory and all of its support files and binaries to be installed. The default is C:\cygwin64, but I change it to C:\cygwin to make future me a little happier. I also allow all users to have access to Cygwin.

Image
Select the install directory

Click Next to continue.

Enter the directory where you want Cygwin to download its files. I always use C:\Temp. If you select a directory that doesn't exist, the installer prompts you to create it.

Image
Select local package download directory

Click Next to continue.

Select your Internet connection type. If you have some sort of proxy, enter the setting here. Mine is a direct connection. If you don't know, try direct. If that doesn't work, check your browser configuration for proxy settings.

Image
Select Internet Connection Type

Click Next to continue.

Choose a download site on this screen. Select one from the list or add your own. I always choose mirrors.xmission.com for no logical reason--I've just stuck with it for a long time, and it works. I assume that any of the selections will work.

Image
Choose Download Site

Click Next to continue.

[ You might also like: How to join a Linux system to an Active Directory domain ]

Selecting packages

Finally, you're at the package selection screen. If you select nothing and continue with a default installation, you get a full complement of *nix commands in the C:\cygwin\bin directory. They're too numerous to list, but it's a list of standard commands.

I suggest you peruse the different categories and make selections based on what you use on your Linux systems. I generally select OpenSSH, Bash, Bash-completion, Python, Tk/Tcl, and some other random applications. Once you've made your selections, click Next to continue.

Image
Select packages to install

Review and confirm your selections. Dependencies will be satisfied for your selections, so don't worry. Click Next to begin installing your selected packages.

Image
Review and confirm changes

The installation might take from a few minutes to close to an hour, depending on your selections and internet speed.

Image
Cygwin installation with progress indication

The last screen notifies you that the installation is complete and prompts you to create some handy icons. I always create the icons. Click Finish to complete the installation process.

Setting the environment PATH variable

For the best results, you need to add the C:\cygwin\bin directory to your PATH environment variable. To set the System PATH to include your Cygwin bin directory, search for "environment" and select the Control Panel option, Edit the system environment variables, when it appears. Select Environment Variables on the System Properties screen.

Image
System Properties

On the Environment Variables screen, select Path from the System Variables pane, and click Edit.... Click New. Enter C:\cygwin\bin into the field and click OK when finished. Click OK on the other screens to save the entry and to close the System Properties window.

Image
Setting the system environment path variable

Setting this system environment variable allows you to execute a Cygwin command without providing the full path to the executable. This is handy for scripting too.

Scripting notes

Now that Cygwin is installed and the binaries are in your system path, you're probably wondering how you make this work. To invoke Bash, open a CMD window and type bash <ENTER>. You're now in a Bash shell that works exactly like your Linux one does. Try a few commands such as clear, ls, ls -la, pwd, and so on.

There are some differences that can be frustrating to new users if you're not aware of them. In Bash, you'll notice that your drives, C: and D:, are mounted as /cygdrive/c and /cygdrive/d, for example. So, the C:\Temp directory is /cygdrive/c/temp in Cygwin Bash.

Familiar directories such as /, /usr/bin, and /usr/lib exist under C:\cygwin with the C:\cygwin directory as /. If you don't understand this layout, open a separate CMD window and CD into the C:\cygwin directory and look at the directory in both CMD and in Bash.

First, the CMD listing.

DIR C:\cygwin

Volume in drive C has no label.
 Volume Serial Number is ACBE-2D0D

 Directory of C:\cygwin

10/15/2020  06:58 AM    <DIR>          .
10/15/2020  06:58 AM    <DIR>          ..
10/15/2020  06:57 AM    <DIR>          bin
10/15/2020  06:58 AM            53,342 Cygwin-Terminal.ico
10/15/2020  06:57 AM                88 Cygwin.bat
10/15/2020  06:58 AM           157,097 Cygwin.ico
10/15/2020  06:57 AM    <DIR>          dev
10/15/2020  06:57 AM    <DIR>          etc
10/15/2020  06:56 AM    <DIR>          home
10/15/2020  06:57 AM    <DIR>          lib
10/15/2020  06:57 AM    <DIR>          sbin
10/15/2020  06:57 AM    <DIR>          tmp
10/15/2020  06:57 AM    <DIR>          usr
10/15/2020  06:57 AM    <DIR>          var
               3 File(s)        210,527 bytes
              11 Dir(s)  31,950,209,024 bytes free

And, the Bash listing.

ls /

Cygwin-Terminal.ico		Cygwin.ico	cygdrive	etc	lib	sbin	usr
Cygwin.bat			bin 		dev		home  proc 	tmp	var

So, you see from these anomalies that Bash on Windows is not 100 percent equivalent to *nix Bash. Some scripts will translate directly, while others will have to be Window-ized. That is to say, customized to accommodate path differences, mount differences, and some file location differences. One really cool aspect of hybridizing your Windows environment is that you can also use Windows command line commands. You can pretty easily integrate them into your scripts. Remember to provide the full path to the executable in Cygwin format (/cygdrive/c/windows/system32/blah.exe).

[ A free course for you: Virtualization and Infrastructure Migration Technical Overview. ]

Wrapping up

Cygwin is an excellent cross-platform toolset, especially for those of you who must work in a heterogeneous environment, as most of us do. I can't count the number of times I've typed ls, or some other Linux command, in a CMD window only to have it fail. Cygwin gives you the expanded capability of writing a script once and being able to use anywhere, with minor tweaks. Imagine the possibilities. Now, add Ansible to the mix and again imagine the possibilities. To further enhance your happiness, all of the executables under C:\cygwin\bin that have a .exe file extension can also be run in a CMD window without requiring you to be in Bash. Interesting? I thought so.

Check out these related articles on Enable Sysadmin

Topics:   Linux   Linux Administration   Windows   Interoperability  
Author’s photo

Ken Hess

Ken Hess is an Enable SysAdmin Community Manager and an Enable SysAdmin contributor. Ken has used Red Hat Linux since 1996 and has written ebooks, whitepapers, actual books, thousands of exam review questions, and hundreds of articles on open source and other topics. More about me

Related Content

OUR BEST CONTENT, DELIVERED TO YOUR INBOX