Using Linux on Windows with Cygwin

Home Away from Home

Article from Issue 164/2014
Author(s):

The Cygwin environment lets you run Linux applications on a Windows system.

If you're locked into Windows, but you don't want to give up your favorite Linux/Unix tools, the Cygwin project [1] offers a trusted and time-honored option. Cygwin, which has been around since 1995, is a Windows run-time environment that supports many Linux programs. You can even compile your Linux and Unix source code for the Cygwin Windows environment.

According to the project website, Cygwin is

  • a large collection of GNU and open source tools that provides functionality similar to a Linux distribution on Windows, and
  • a DLL (cywin.dll) that provides substantial POSIX API functionality.

The website is also careful to note what Cygwin isn't (see the box).

Cygwin Isn't

The Cygwin website also points out what Cygwin 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 make native Windows apps magically aware of Unix functionality such as signals, ptys, and so on. (Again, you need to build your apps from source if you want to take advantage of Cygwin functionality.)

The Cygwin Windows run-time environment ports many Linux programs to Windows – in particular, OpenSSH and the X server. Cygwin therefore lets you launch Linux programs remotely on a Windows computer. A version of Bash and other popular tools add icing to the cake.

In some ways, you can think of Cygwin (which runs Linux programs on Windows) as a counterpart to the Linux Wine environment (which runs Windows software on Linux) [2]. However, Cygwin and Wine aren't exactly equivalent: Wine launches pre-built Windows binaries, but Linux programs must be customized and recompiled to run on Cygwin (see the box titled "How Cygwin Works").

How Cygwin Works

The main reason Linux programs do not run on the Windows operating system is that Linux and Windows use different system calls. The Cygwin environment inserts a layer between the operating system and application that translates the Linux system calls to the appropriate Windows equivalents.

Unlike Wine, which launches original Windows programs piggyback-style, Cygwin requires you to build the Cygwin layer into each program's executable file. To integrate the Cygwin layer, you need to compile the program using a modified version of GCC; starting with only slightly modified source code, GCC generates Cygwin-ready Windows EXE and DLL files. Because Linux ports with Cygwin support are often very easy to create, Windows versions of many other Linux programs are readily available (see the "Cygwin Ports" box).

Cygwin Ports

Cygwin Ports is a repository of Unix/Linux applications that have been ported to the Cygwin environment. You can install a Cygwin port with the same installer as the Cygwin base system. However, you must not start the installation routine with a double-click in Explorer; otherwise, the system will not accept the signature of the Cygwin Ports repository. The workaround is to tell the installer about the repository through the Cygwin console, as follows:

$ cygstart  -- /path/to/setup-x86.exe \
  -K http://cygwinports.org/ports.gpg

When asked about the download mirror, enter ftp://ftp.cygwinports.org/pub/cygwinports and click Add. When you use a mirror, make sure it is not located on the same server as the last download; otherwise, the Cygwin installer will not detect it as a new source. The download URL must point to the cygwinports/ directory on the server.

Not all programs are converted for 64-bit architecture, so it might be worthwhile to install the 32-bit variant on a 64-bit system.

Highlights of the Cygwin Ports repository include applications such as AbiWord, Amarok, Apache2, Avidemux, Bluefish, Calligra Office, digiKam, DVDAuthor, Emacs, eSpeak, Evolution, FFmpeg, FileZilla, FluidSynth, Gimp, Git, GnuCash, Gnumeric, GNU Chess, Inkscape, Jokosher, Kate, KMail, Kontact, KStars, MonoDevelop, MPlayer, Scribus, and XMMS2. Desktop environments such as Gnome, KDE, LXDE, Mate, ROX, and Xfce are also available as ports.

Setup

To launch the Cygwin graphical installer, click setup_x86.exe [3] (or setup_x86_64.exe [4] for 64-bit systems). The setup program asks you for the root directory and suggests C:\cygwin. All installed files will appear below this folder in an ordinary Linux filesystem configuration.

What is not-so-Windows is the built-in automatic resolution of package dependencies. To set up an environment for running X programs remotely, for instance, you just need to install the xinit, openssh, and xterm packages (Figure 1). All dependencies, such as the X server or the Cygwin run-time environment, are automatically installed by the setup program. The total download volume is 70MB.

Figure 1: The graphical Cygwin installer automatically resolves the dependencies of selected components.

The search box at the top of the window helps you find these packages in the long list. A click on Skip alternates between installing, updating, uninstalling, or omitting. To update all the installed packages, just start the setup program without changing the package selection.

Getting Started

By default, the installer creates an icon for the Cygwin console on the desktop and in the Windows Start menu. The Cygwin X Start menu folder also has an entry for starting the X server without going through the Cygwin console.

Start by opening the Cygwin terminal. In the console window (Figure 2), you are welcomed by a largely unmodified Bourne Again Shell. The Cygwin Bash understands both Unix file paths and Windows paths with backslashes and drive letters.

Figure 2: Linux on Windows: Linux geeks feel at home in the Cygwin environment thanks to the familiar Bash.

Absolute Unix paths such as /home are translated into paths relative to the folder in which Cygwin is installed (c:\cygwin or c:\cygwin64) by the Cygwin version of the shell. You can call .exe files without specifying the suffix, but all program files you port to Cygwin must use the .exe extension to run under Windows. Calls such as /usr/bin/xterm work on the Cygwin console, and you can still use Bash scripts written for Linux.

Cygwin with X

The Cygwin X server lets you launch a Linux graphics environment. Like other X environments, Cygwin X, which is a modified version of X.org, supports remote connections. An SSH connection to a Linux machine and X forwarding thus conjures up Linux programs on the desktop with Windows window dressing (Figure 3).

Figure 3: Remote X on Windows: An entry in the Start menu opens an X terminal and an X server in the background.

To start the X server, select Cygwin-X/XWin Server in the Start menu. An Xterm window opens on the desktop (Figure  3). Although the X server appears in the system tray, it is not assigned to a window.

You can now log on to a remote Linux machine using ssh -X <IP-of-Linux-PC>. The -X parameter enables X11 forwarding. Use the terminal to launch graphical applications on the remote Linux computer, and the program will show up on the desktop, along with Windows-style window dressing.

This procedure relies on multiwindow mode to display remotely launched programs, but it also depends on the program for the Xterm running locally in a separate window. Ports of Linux applications installed in the Cygwin environment organically blend into the Windows desktop, as do applications started remotely with SSH.

Locally installed X-based applications also require access to the X server. You can call these local apps without previously setting up an SSH connection in the Xterm window.

The Cygwin X server can also import a full Linux desktop instead of individual applications. Of course, you could also import a session using a remote desktop tool such as VNC, but remote X improves image quality and provides a quicker response time – at least on the local network. You can also launch a standalone desktop session. Users locally logged in to the remote machine can continue to work undisturbed.

Cygwin X uses the XDMCP (X Display Manager Control Protocol) to connect directly with the display manager on a remote Linux system. The login screen of the Display Manager (KDM, GDM, or LightDM) appears in the X server under Windows (see Figure 4). XDMCP is only really useful in safe local networking conditions, because it sends passwords in cleartext, and unlike a standard remote X connection, XDMCP uses the UDP protocol, so it cannot tunnel through SSH.

Figure 4: A Cygwin X server uses XDMCP to connect directly with the display manager on a Linux machine.

The insecure nature of XDMCP has led many distributions to disable it by default. To enable XDMCP, go to the [Xdmcp] section in the display manager configuration file and change the value of the Enabled variable to true. For KDM, the configuration file is /usr/share/config/kdm/kdmrc; LightDM on Ubuntu uses /etc/lightdm/lightdm.conf. For GDM, look for /etc/gdm/custom.conf.

Now restart the display manager service (kdm, gdm or lightdm). Then, in the Cygwin console, enter X -query <server-IP> to obtain the familiar feeling that you are logging in to a Linux machine.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News