Using Linux on Windows with Cygwin

Home Away from Home

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.

Secure Tunneling

Cygwin also lets you set up a remote desktop session through a secure SSH tunnel. First type X & at the Cygwin console to open a local X server. (The & sends the service into the background and ensures that you can continue using the console.) This command calls the X binary directly. The X server starts in a large window, with no terminal for setting up an SSH session. This approach helps you avoid closing the wrong window in the desktop environment and thus terminating all open applications. After opening the X window, go back to the Cygwin window and run the following command to launch a KDE desktop environment remotely:

DISPLAY=:0.0; ssh -Y -f \
  <IP-of-Linux-PC>; startkde

After setting DISPLAY, you need to run an SSH call that again releases the console after executing a command on the remote host (-f option). The -Y parameter initiates X forwarding in trusted mode, so the window manager can use the X instance remotely.

For Xfce, replace startkde with startxfce4; and for LXDE, use startlxde. Gnome 3 proved problematic in our lab with the gnome-session startup command: The desktop environment failed because of the lack of 3D support. A general method to enforce fallback mode over the network does not seem to exist.

Native Everything

Another option is to install a ported desktop environment (or at least a window manager) locally in the Cygwin environment. The desktop offers the look and feel of Linux – for remote applications, but also for locally installed ports. The standard Cygwin repository lets you install Windowmaker and the FWM and Twm window managers. Gnome, KDE, LXDE, Mate, ROX, Xfce, and the Sugar desktop environment are all available as Cygwin ports [5].

I tried to install KDE, and the libqt4 dependency turned up missing. Although libqt4 is available in the normal repositories, it is not provided in the Cygwin Ports version. To resolve the problem, I needed to restart the installer and integrate the appropriate package. After this step, I managed to launch a KDE desktop in the Cygwin environment (Figure 5), but only after a relatively long startup time.

Figure 5: Ported, not remotely executed: a full KDE desktop running in the Cygwin environment.

The Gnome shell was not available on the server, so I was unable to launch the Gnome desktop. The spartan Windowmaker environment (Figure 6) worked out of the box, however. The download volume is fairly small, and Windowmaker is included in the standard Cygwin repository.

Figure 6: The still experimental -mwextwm parameter lets you launch a transparent local Windowmaker environment.

The startx startup script is also used in the Cygwin environment to launch X plus a display manager. The following command opens a KDE session:

$ startx /usr/bin/startkde -- \
  -nodecoration

The start scripts for the other desktop environments are the same as for the remote calls. Before you launch any other programs, you need to set DISPLAY manually. The "Starting the Cygwin X Server" box gives more details. The Cygwin Installer also offers a graphical user interface named XLaunch, which removes the need for users to enter the command lines described in the box to users (Figure 7).

Starting the Cygwin X Server

Typing startxwin in the Cygwin console or selecting the menu item created by the installer starts the X server in multiwindow mode (one window per application with Windows window dressing).

Without parameters, the tool – which replaces the startxwin.sh and startxwin.bat scripts of earlier versions of Cygwin – opens an Xterm window with the DISPLAY variable set. You can log in directly by typing

ssh -X <IP-of-Linux-PC>

and starting graphical programs remotely. The command-line parameters for startxwin and its configuration file ~/.startxwinrc are explained in the tool's man page [8].

Figure 7: The XLaunch program lets you launch without starting the Cygwin X server at the command line.

XWin.exe or X

A call to X only starts the X server; you launch programs that access the server at the Cygwin console after setting the DISPLAY=:0.0 shell variable. The Cygwin X server parameters differ in part from those of the Linux version and are described in the man page [9].

Startx

Typing startx connects the X server to a window manager running locally in Cygwin. For example, /usr/bin/wmaker adds Windowmaker window decorations to the program window. Parameters for the X server itself follow the -- delimiter; startx interprets text before the delimiter as the program to be launched in the Cygwin environment, with any necessary options trailing:

$ startx /usr/bin/wmaker -- -nodecoration

The preceding command initiates a Windowmaker environment on the client side, which occupies the entire desktop above the taskbar. The command

$ startx /usr/bin/wmaker -- -mwextwm

is quite different. It hides the Linux desktop background, keeping the Windows desktop visible. For more information, check out the X.org man page for startx [10].

Conclusions

Cygwin ports the X Window System, OpenSSH, and many other Unix and Linux programs to Windows, allowing Windows users to access Linux computers remotely. Cygwin performance is only satisfactory over a fast local network connection with low latency – a limitation currently only lifted by the no-cost NX Client [11] for remote X on Windows.