Win-win with Cygwin
Mind the Gap
If you work in a larger computing environment, you likely have to deal with Windows systems, Linux systems, and commercial Unix systems, and some of those heterogeneous systems might be under your direct administrative jurisdiction. If you're using virtualization technology as a Windows administrator, you're likely using Linux whether you know it or not. The exception would be Hyper-V: It's possible, although not likely, that you work in an all-Windows shop.
If, like 99% of people, you work in a heterogeneous environment, at some point you probably will want to expand your skills to include Linux or Unix. Cygwin is the answer to bridging that gap painlessly.
Cygwin is a powerful set of Linux and Unix (*nix) tools, but its real power comes from being able to turn your heterogeneous environment into a more homogeneous one. Although tools are available that you can add to *nix systems to make them more compatible with Windows, Cygwin creates a true hybrid Windows/*nix system. For scripted solutions, Cygwin includes a rich set of command-line tools, but it also has the ability to use graphical ones through its X Window utilities.
In this article, I will guide you through the installation and introduce you to the use of Cygwin, so you can hone your Linux- and Unix-related skills and expand your capability and knowledge in the *nix world. In one of my online articles [1], I provide an overview of Cygwin, but here, I give you a start-to-finish look at Cygwin and its features.
Although I can't teach you Linux commands in a single article, the intent is to give you access to this very powerful toolkit as a starting point for further study.
Preparing for Cygwin
Before you download and install Cygwin, I suggest you check the available space on your C:
drive (or system drive), because a full installation of Cygwin consumes 1GB or more of space. The default install location is C:
, but you can change that during the installation process. If you decide to install to an alternative location, you will have to check the available space there, too.
Additionally, you'll need a bit of temporary space in which to download the Cygwin files and perform the install. For the installation I chose for this article, my required space was approximately 300MB and just under 100MB for the temporary staging space.
Keep the temporary staging directory intact for future installations and customizations. The disk cost for these files is minimal compared with the final installation size.
Cygwin Installation
On the system on which you want to install Cygwin, open a browser and point it to the Cygwin homepage [2]. Scroll down the page until you see a link for setup.exe
, and click that link to launch the installation wizard. The wizard begins with an informational screen (Figure 1). Note the last sentence on the screen that says, "You can always run this program at any time in the future to add, remove or upgrade packages as necessary."
Clicking Next to continue brings up the screen in Figure 2, which gives you three install choices: Install from Internet, Download Without Installing, and Install from Local Directory. If you select Install from Internet (recommended), be sure to read the note about the downloaded files being kept for future re-use. This is the staging directory to which I referred in the previous section. Click Next.
Then, you can select the location on your Windows computer to install Cygwin. The default location is C:\Cygwin
. Change the location here, if you want the Cygwin files to use a different drive or folder. Also, you can choose whether to allow all users on the system to use Cygwin or to keep it private.
After clicking Next, you can choose the disk location to which the compressed Cygwin files download and install. I always use a temporary directory, such as E:\Temp
. To continue, click Next. On the next screen, you must select the Internet connection through which you'll download Cygwin. Possible selections are Direct Connection, Use Internet Explorer Proxy Settings, or Use HTTP/FTP Proxy. If you select HTTP/FTP proxy, you have to fill in the information for your proxy host and port number.
Next, you need to select a download site from the list displayed on the screen. In Figure 3, I chose ftp://mirrors.xmission.com. Try to choose one that's physically close to you or that you know has bandwidth to spare. Also, you can add a URL of your own if you have a private repository that you'd like to use.
Now it's time to select the Cygwin packages for installation. As shown in Figure 4, you have a list of categories from which to choose. Each category has a default list of applications ready to install. To to expand the list and see which packages install by default, click the (+) to the left of each category.
For example, I've expanded the Shells category and selected bash and xterm from the list. To make a package available for installation, click the Skip link so it changes to show a version number and a checkbox. Clicking the checkbox selects the package. These two handy packages are useful in a basic installation. Bash is almost mandatory if you're going to do any scripting.
Additionally, you should select the xinit package from the X11 category and the openssh package from the Net category for a basic functional installation. In a later section, I'll explain how a complete Cygwin installation might work better for certain applications.
Once you've selected all of the additional packages that you want to install, click Next to continue. Package dependencies will be selected for you by default. The next screen prompts you to verify that you want the dependencies. Because you do, click Next to continue, and your Cygwin installation begins.
Depending on which site you selected and how many additional packages you marked for installation, the process could take from 10 minutes to an hour or more. Be patient and allow the process to continue. When complete, the wizard prompts you to Add a Cygwin icon to the Desktop and to the Start Menu, then click Finish. You've successfully installed Cygwin to your system and can use it now, with no reboot required.
Customization
You might realize that you need to install one or more packages after this initial installation. If so, you need to run setup.exe
again from where you saved it or directly from the website.
Step through the wizard, accepting the defaults. (The defaults should reflect your earlier selections.)
When the wizard displays the list of Cygwin packages, select the category and the package you require. If you don't know the category under which your package exists, you can search for it with the Search field (upper left corner of the Select Packages window) (Figure 5).
Once you've selected the package or packages you want, click on through the wizard normally until package installation completes. You can select more than a single package from a category or from multiple categories during a rerun of the setup wizard. If you want to uninstall a package, you can click the Keep link until it reads Uninstall and proceed through the wizard.
When selecting a package for installation, you will see two checkboxes. Selecting the left checkbox installs the binary package (executable and supporting libraries and dependencies) and selecting the right checkbox installs the package source code. Although you do not need the source code in this case, you might decide to install it for another use.
Setting the Path
To add Cygwin to your PATH
, open a CMD prompt on your Windows system, type PATH
, and press Enter. Your response should look something like Listing 1. To use Cygwin effectively, I need to add E:\Cygwin\bin
to my PATH
environment variable. Of course, you use the location of your installation's bin
folder. To do so, right-click and choose Computer | Properties | Advanced System Settings | Environment Variables | System Variables, scroll down until you see Path (Figure 6), and click the Edit button. Enter a semicolon after the last entry and the full path to your Cygwin bin
directory (Figure 7).
Listing 1: Adding Cygwin to your PATH.
01 C:\Users\Administrator>PATH 02 PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32 03 \WindowsPowerShell\v1.0\;C:\Program Files (x86)\Common Files\Citrix\System32\
Now when you open a CMD prompt or use the Cygwin Terminal, the Cygwin binaries will be in your path. Close and re-open any CMD windows to use the new environment variable.
Command Line
As a Windows administrator, chances are good that you don't spend much time at the command line. The occasional ipconfig
or iisreset
notwithstanding, you probably don't open a command prompt and work there multiple hours a day. If you do, you're a rare individual, and you'll have no trouble making the transition to the *nix world. Most *nix administrators exclusively use the command line for their work. In fact, few *nix server systems have a GUI desktop installed on them. In the next section, "Using the X GUI," I describe how to interact with graphical applications, if they exist on your *nix systems.
One of two ways to enjoy Cygwin at the command line is to use the Cygwin Terminal icon located on your Desktop (Figure 8). This icon is a shortcut to a terminal window using the Bash interactive shell. The window shown in Figure 9 is your command-line environment. From this prompt, you can execute almost every Windows command-line program and every Cygwin command. As a test, try entering a few Windows commands. For example, ipconfig
returns your system's IP information. Now, enter a few *nix commands, such as ls
or pwd
to see the results.
At the command prompt, you can create scripts, edit files, connect to remote systems, or navigate your filesystem. Note that when using Cygwin's Terminal application, you must refer to your drives in their *nix equivalents, so your C:
drive is now /cygdrive/c
and the E:
drive is /cygdrive/e
. If you attempt to perform an ls
on E:
, you will receive an error:
$ ls E: ls: cannot access E:: No such file or directory.
However, if you refer to the *nix version with ls /cygdrive/e
, you'll see a listing of files in your current folder. Likely you don't have any files to see. To create a few, use the following command:
$ touch file1.txt file2.txt file3.txt $ ls file1.txt file2.txt file3.txt
The touch
command touches a file to update its timestamp and will create an empty file, so this is a very useful tool in scripting when you want to test for the existence of a file but not act on it. The touch
command has other uses as well. To learn more about touch (and, in the same way, other installed commands), type man touch
at the prompt to view the online documentation. The result is a view of the *nix man (manual) pages. To navigate man pages, use the Enter, Space, and q (quit) keys. Man pages are an essential part of any *nix system. Use them freely.
The second way to use Cygwin is to use individual applications in your Windows CMD window. Not all of the commands located in E:\Cygwin\bin
will work this way. Some of the applications only work within the interactive Cygwin shell. The easiest way to know which ones work is to check out a program's extension.
Although extensions have no meaning in *nix, they do in Windows. A file named test.txt
is very different from text.exe
. If you look in your Cygwin bin
folder and list its contents, note the applications with the .exe
extension: These commands are available to you in the CMD window, which means you can leverage these new commands in your CMD scripts, pass CMD variables to them, and use them with your knowledge of the familiar Windows CMD syntax.
Using the X GUI
Windows administrators often prefer a graphical environment to the command line. I understand. By installing the xinit
package and its dependencies, you've installed support for the X Window GUI (X) environment. To use X, you must log in to a remote *nix system via SSH (secure shell). SSH is the secure equivalent of the old Telnet application; it provides an interactive shell session on the remote system from which you can work at the command line.
Also, you can direct applications back to your Windows system from the remote *nix computer. I'll walk you through the steps to test X on your remote system using the classic "Xeyes" program. Xeyes is a fun application that, when running, displays a pair of graphical eyes that follow your mouse around the screen. It's a silly, useless application, but it's great for testing X because it's small and installed on practically every *nix system.
To begin, set up the X Window environment on your Windows system. This is known as starting the X server. This process also launches an X terminal window. Type the following command in the Cygwin Terminal window:
$ startxwin
A lot of information scrolls by, and in a few seconds, an Xterm window appears. This Xterm window is your new interactive shell window for use with graphical applications.
Type the following commands as shown. Replace <system> with your *nix system's name or IP address and replace <username> with your username (login ID) on the remote *nix system.
$ ssh -X <system> -l <username> $ ssh -X 192.168.1.250 -l khess
The first time you perform this action, you'll receive the following response:
The authenticity of host '192.168.1.250' can't be established. RSA key fingerprint is 87:17:c3:92:44:ba: 1a:df:d7:9c:44:b2:5b:73:52:09. Are you sure you want to continue connecting (yes/no)?
This prompt wants to establish a permanent secure key between the two hosts for future reference so that your system recognizes the remote system as legitimate. Type yes
at the prompt and press Enter.
After you enter your account password for the remote system, you're logged in when you see the $
prompt, and you're now in the remote system's interactive shell.
The -X
switch in the ssh
commands in the examples means to forward X output back to your system. In other words, you're redirecting graphical programs to your Windows system. The -l
in the ssh
command means you want to use that account ID (username) for the connection. If you don't use -l
, SSH attempts to connect to the remote system using your Windows account ID.
Once logged in to the remote *nix system, type the following command:
$ xeyes &
If you receive the message
Error: Can't open display:
you need to exit your remote *nix session, by typing exit
to return to your Windows Xterm and re-enter the ssh
command with a capital -X
switch. The Xeyes application should open on your Windows system (Figure 10).
All GUI applications operate in this same way. Be aware that some applications are quite large and might take a few minutes to appear on your Windows computer. Be patient.
Remember that any interactions via the graphical applications take place on the remote *nix system. You're basically enjoying a remote view of the application from your Windows system.
A Network Install
A multisystem install point, as I call it, is a Cygwin installation that you install on a network drive so that multiple Windows systems can map a drive to it and use the applications and utilities from that location. Cygwin does not need to be installed locally to work well because the installation makes no changes to the system folders, nor does it add any files to system folders. All Cygwin files reside under the Cygwin directory tree.
To install to a network drive, create a new share on your network storage or file server and map a drive to that location – I'll call it drive X:
. Direct the Cygwin installation wizard to install Cygwin under the X:\Cygwin
folder and use X:\Temp
for the installation source folder.
Performing a complete installation is convenient so you have every possible application and executable available for your work. When the wizard displays the categories list, click All (at the top of the Window) until it reads Install.
This will change each of the categories to Install as well. A complete installation might take an hour or more to perform.
Once installed in this network location, you can map a drive from any system with access to it. You can add X:\Cygwin\bin
permanently to the PATH
environment variable by editing the system path. You can add temporary access by issuing the following command
PATH=%PATH%;X\Cygwin\bin
at an administrator CMD prompt. Although it's a bit cumbersome to do so, you can also use a NetBIOS-style path, such as \\system\share\cygwin\bin\
to each command you issue. For example:
\\system\share\cygwin\bin\ls.exe
Any system on which you might use Cygwin regularly should have a permanent (persistent) drive mapping created to the network share and have its PATH
variable altered.
Summary
What Cygwin does for you, as a Windows administrator, is provide a consistent set of scripting tools like those you'll find on *nix systems. It also presents you with a basic level of compatibility between two very different platforms.
Cygwin transforms your Windows servers and workstations into advanced hybrid operating systems. Cygwin gives Windows administrators the tools and utilities needed to interact effectively with other data center operating systems.
For more information, read Cygwin's FAQ and documentation. Also consult *nix man pages for any command-related questions.