# Cygwin
## Before experiencing X

Cygwin/X is installed by selecting the necessary packages from the X11 category with the standard Cygwin installer. Users unsure of what is needed should install all of the X11 components.

## Accessing X of your Local Cygwin

You can run the X server and the Window Manager locally and get the X Window GUI environment.
There are various ways you can do this. They are as follows:

### MultiWindow Mode

In the multiwindow mode, there is an internal window manager that creates a Windows window to display the X application on the Windows desktop.

### X

X is a soft-link (shortcut from Windows standpoint) to /usr/X11R6/bin/XWin.
* Start Cygwin
* Execute the command, X -multiwindow &. (Cygwin/X window comes up)
* Execute the command, export DISPLAY=127.0.0.1:0.0
* Execute the command, xclock &. (xclock comes up).
Now you can run any X application. They open up in separate windows individually with your windows title-bar, caption buttons and border. You don't get the desktop environment in this case.

### startxwin.bat

* Run startxwin.bat from usr\X11R6\bin\ folder where your Cygwin is installed. This can be done by double clicking on the batch file. It will bring up an xterm window. If you don't want to go through the hassle of finding this file, you can simply run the startxwin-multiwindow.bat file which should be present on your desktop. Both the scripts are very similar.
* Execute the command, xclock & in the xterm window. (xclock comes up).
Now you can run any X application. They open up in separate windows individually with your windows title-bar, caption buttons and border. You don't get the desktop environment in this case.

### startxwin.sh

* Start Cygwin (The Bash shell comes up)
* Execute the command, startxwin.sh &. It'll bring up an xterm window.
* Execute the command, xclock & in the xterm window. (xclock comes up).
Now you can run any X application. They open up in separate windows individually with your windows title-bar, caption buttons and border. You don't get the desktop environment in this case.

### startx

* Start Cygwin (The Bash shell comes up)
* Execute the command, startx &. It'll bring up an xterm window.
* Execute the command, xclock & in the xterm window. (xclock comes up).
Now you can run any X application. They open up in separate windows individually with your windows title-bar, caption buttons and border. You don't get the desktop environment in this case.

## External Local Window Manager

If you want the complete desktop environment, you will have to run a window manager or Linux desktop to send the GUI to your X server. This section is a discussion on this topic.

### X

X is a soft-link (shortcut from Windows standpoint) to /usr/X11R6/bin/XWin.
* Start Cygwin (The Bash shell comes up)
* Execute the command, X &. (Cygwin/X window comes up)
* Execute the command, export DISPLAY=127.0.0.1:0.0
* Execute the command, wmaker &
* Check your Cygwin/X window. You have got the desktop. :-)

## Accessing X of a Remote Box

In the following discussion the following IP addresses and user name have been assumed:-
* remote_box - IP Address or domain name of remote box
* my_system - IP Address or domain name of your Windows system
* user_name - A valid account in the remote box

## X11 Forwarding

For some of the methods described in this section, X Forwarding should be enabled in the remote box. Whether this is enabled or not can be found out from the file, /etc/ssh/sshd_config of the remote box.
If there is a line X11Forwarding yes, it means X11 Forwarding is enabled and you can go ahead with the steps below.
If it is, X11Forwarding no, X11 Forwarding is disabled. You can edit it to enable it. Of course this can be done by the "root" only. If you need X11 Forwarding enabled and you are not the "root" of the box, you'll have to talk to its admin.

## MultiWindow Mode

In the multiwindow mode, there is an internal window manager that creates a Windows window to display the X application on your Windows desktop.

### X

X is a soft-link (shortcut from Windows standpoint) to /usr/X11R6/bin/XWin directory.
* Start Cygwin (The Bash shell comes up)
* Execute the command, X -multiwindow &. (Cygwin/X window comes up)
* Execute the command, export DISPLAY=127.0.0.1:0.0
* Log in to the remote box with the command, ssh -X user_name@remote_box
* Execute the command, xclock &. (xclock comes up).
Now you can run any X application on the remote box. They open up in separate windows individually with your windows title-bar, caption buttons and border. You don't get the desktop environment in this case.
For the above method to work, X11 Forwarding should be enabled in the remote box.

### startxwin.bat

* Run startxwin.bat from usr\X11R6\bin\ folder where your Cygwin is installed. This can be done by double clicking on the batch file. It'll bring up an xterm window. If you don't want to go through the hassle of finding this file, you can simply run the startxwin-multiwindow.bat file which should be present on your desktop. Both the scripts are exactly similar.
* Execute the command, xhost remote_box to provide remote box access to the X server running on your Cygwin. Alternatively you can execute the command, xhost + to disable access control completely, so that any client can connect to your X server.
* Log in to the remote box with the command, ssh -X user_name@remote_box
* Execute the command, export DISPLAY=my_system:0.0 on the remote box.
* Execute the command, xclock & in the xterm window. (xclock comes up).
Now you can run any X application on the remote box. They open up in separate windows individually with your windows title-bar, caption buttons and border. You don't get the desktop environment in this case.
If X11 Forwarding is enabled in the remote box, you can omit step 2 and step 4.

### startxwin.sh

* Start Cygwin (The Bash shell comes up)
* Execute the command, startxwin.sh &. It'll bring up an xterm window.
* Execute the command, xhost remote_box to provide remote box access to the X server running on your Cygwin. Alternatively you can execute the command, xhost + to disable access control completely, so that any client can connect to your X server.
* Log in to the remote box with the command, ssh -X user_name@remote_box
* Execute the command, export DISPLAY=my_system:0.0 on the remote box.
* Execute the command, xclock & in the xterm window. (xclock comes up).
Now you can run any X application on the remote box. They open up in separate windows individually with your windows title-bar, caption buttons and border. You don't get the desktop environment in this case.
If X11 Forwarding is enabled in the remote box, you can omit step 3 and step 4.

### startx

* Start Cygwin (The Bash shell comes up)
* Execute the command, startx &. It'll bring up an xterm window.
* Execute the command, xhost remote_box to provide remote box access to the X server running on your Cygwin. Alternatively you can execute the command, xhost + to disable access control completely, so that any client can connect to your X server.
* Log in to the remote box with the command, ssh -X user_name@remote_box
* Execute the command, export DISPLAY=my_system:0.0 on the remote box.
* Execute the command, xclock & in the xterm window. (xclock comes up).
Now you can run any X application on the remote box. They open up in separate windows individually with your windows title-bar, caption buttons and border. You don't get the desktop environment in this case.
If X11 Forwarding is enabled in the remote box, you can omit step 3 and step 4.

### PuTTY

* Open PuTTY
* From the Category tree, select "Session" and enter the host details.
* From the Category tree, select "Connection > SSH > X11" and check the "Enable X11 Forwarding" checkbox.
* Click "Open" and login.
* Run an X Server on your Windows machine. There are various ways to do it.
** Open Cygwin and run X -multiwindow &.
** Run startxwin.bat from Windows or open Cygwin and run startx, startxwin.sh. An extra xterm opens which might be a pain since you don't need it. (The previous two steps save you from that).
* Get back to your PuTTY window and execute the command, xclock & (xclock comes up).
* Check your Cygwin/X window. (You have got the desktop :-))
For this method to work X11 Forwarding should be enabled in the remote box and you should be logging into the remote box using the SSH protocol.
If you want to save yourself from the first four steps, simply add the path of the PuTTY directory to the Windows PATH variable or you can simply put PuTTY.exe in the Windows directory. Henceforth, you can simply execute the command, putty -X user_name@remote_host from anywhere and then proceed with step 5.

## External Remote Window Manager

If you want the complete desktop environment of a remote box, you will have to run a window manager or Linux desktop on the remote box and forward the X output to the X server of your Windows PC so that you can see the GUI on your X server. This section is a discussion on how this can be done.

### X

X is a soft-link (shortcut from Windows standpoint) to /usr/X11R6/bin/XWin directory.
* Start Cygwin (The Bash shell comes up)
* Execute the command, X &. (Cygwin/X window comes up)
* Execute the command, export DISPLAY=127.0.0.1:0.0
* Log in to the remote box with the command, ssh -X user_name@remote_box
* Execute the command, wmaker & on the box.
* Check your Cygwin/X window. You have got the desktop of the remote box. :-)
For the above method to work, X11 Forwarding should be enabled in the remote box.

### PuTTY

* Open PuTTY
* From the Category tree, select "Session" and enter the host details.
* From the Category tree, select "Connection > SSH > X11" and check the "Enable X11 Forwarding" checkbox
* Click "Open" and login.
* Execute the command, X &, to run the Cygwin X server on your Windows system.
* Get back to your PuTTY window and run wmaker &.
* Check your Cygwin/X window. (You have got the desktop :-))
For this method to work X11 Forwarding should be enabled in the remote box and you should be logging into the remote box using the SSH protocol.
If you want to save yourself from the first four steps, simply add the path of the PuTTY folder to the Windows PATH variable for once and ever. Henceforth, you can simply execute the command, putty -X user_name@remote_host from anywhere and then proceed with step 5.

## Recipe: Run an X application (on a remote UNIX machine) with a local Windows X Server

- Install Cygwin including the X11 client and Secure Shell SSH.
- Start Cygwin Bash Shell by clicking on the Cygwin icon.
- Start the X Server (This opens another Bash Shell in an X window.
- Allow the remote computer to X Client into your local computer by adding the remoter server to the access control list.
- Use ssh to access the remoter computer. You will be prompted for a UserID and Password.
- While on the remote computer execute the program you want in background mode.

## Cygwin Tips

* Install Cygwin to the default directory, C:\cygwin\ for consistency. You can also make the path longer, for instance to keep multiple versions of Cygwin.
* When installing, check cygrunsrv to allow for running sshd as a Windows service
* To use rxvt as the preferred console, create a Windows shortcut with the Target set to C:\cygwin\bin\rxvt.exe --loginShell -sr and Start in set to C:\cygwin\bin.
* To enlarge the console window and add a title, add -geometry "80x50" -title Cygwin to the Target.
* To start an SSH session, add -e ssh user@machine.name.com to the Target.
* To change the default home directory to My Documents on NT-based Windows (NT/2000/XP/2003/Vista), create an environment variable called HOME with value of C:\DOCUME~1\USERNAME\MYDOCU~1 (where USERNAME is your Windows login username).
* Create an environment variable called SHELL with value of /bin/bash.
* Make sure c:\cygwin\bin; is added to the Path environment variable.
* To mount prefix from /cygdrive to / type this command once, mount -s --change-cygdrive-prefix /
* Or keep cygdrive as is and create mounts (e.g. mount -u C: /C) for every drive.
* Emacs and vi are the most popular editors but Nano is the easiest to use.
* Create a file called .bash_profile in the HOME directory containing the following lines:
** alias dir='ls -lav—color=auto'
** EDITOR=nano; export EDITOR
** VISUAL=nano; export VISUAL
* To use the clear command install the ncurses package (in the lib category)

## External links

* Cygwin User's Guide
* Cygwin API Reference
* Cygwin FAQ