CS50 Outside of the Science Center -- Microsoft Windows

Computer Science 50

Fall 2006

February 3, 2008

Introduction

This guide is about setting up your machine(s) outside of the Science Center so that you can work on your CS50 assignments. You are not required to setup any of your home machines to work on assignments; you are always welcome and encouraged to use the Science Center machines.

NICE Linux

In assignment 1, you were introduced to the Linux machines at the Science Center. Those machines are a part of the larger computer setup in the Science Center referred to as the NICE environment (The name NICE stands for New Instructional Computing Environment). Although you can't log in remotely to those workstations, there are Linux servers in the NICE environment that you can log into without having to go to the Science Center. They run the same software as the workstations, and you have the same home directory when you log into them.

This guide will help you setup a SSH (Secure SHell) client to connect to the NICE linux servers. These clients only give you a simple command line terminal interface to the server. This guide will also help you set up the X windows system (the graphical user interface system for Linux) so you get a good user interface.

To log in to a NICE Linux server, you make an SSH connection to nice.harvard.edu.[*] SSH is a secure communication protocol. Mac OS X and Linux machines normally include an implementation of SSH that can be used from the command line. In a terminal window on your personal machine, you issue an SSH command that connects to the remote server. After you supply your password, it leaves you at a command prompt from the server machine. So SSH effectively turns your local terminal into a remote terminal of the server, until you choose to log out of the server.

Windows users usually obtain SSH by installing a terminal emulator such as SecureCRT or PuTTY. You launch one of these applications for the express purpose of creating a command window that's securely connected to the remote server. But the net effect is the same as in the Mac OS X/Linux case: you end up with a command-line interface to the remote server, not a GUI.

So how can you benefit from the GUI features of an application like Emacs when you're connecting remotely from your own PC?

The X Window System

It turns out that most implementations of SSH provide support for the open-source window system called X, which is the native window system of Linux and UNIX. While it has much in common with other window systems, X is unusual in that its implementation is distributed. That means that every time an X-based program like Emacs changes the user's display or receives input from the keyboard, it uses the Internet to transmit the information. As a result, the computer controlling the display, mouse, and keyboard can be different from the computer running the X-based application (such as Emacs), as long as they are both on the Internet and they both support X.

When you use a Linux workstation in the Science Center, you won't normally take advantage of the distributed nature of its window system, because your GUI-based applications will be running on the same machine that's managing the display, mouse, and keyboard. But if your personal computer supports X, it can handle the graphical user interface for applications that are actually running on a remote machine that you have connected to via SSH. So you can make an SSH connection to one of the NICE servers, start Emacs on that server, and it will create a new X window on your own machine. Apart form its border decorations, that window looks just like it would if you were at a Linux workstation in the Science Center. And you can control it just as conveniently with your mouse and keyboard.

We're about to explain how to make your computer support X windows in this way. But first, a bit of lore and jargon.

Whose serve is it?

The terminology of X involves a confusing reversal. Normally when centralized computers are shared by distributed machines that act as terminals for users, the central machines are called servers and the terminal machines (and/or the programs on them that talk to the servers) are called clients. But with X, the ``server'' is the program closest to the display, keyboard, and mouse. It's the program at the terminal that actually draws on the screen, tracks the mouse, and reads input from the keyboard. The X ``client'' is a program such as Emacs that wants to have windows in which to interact. So the confusing reversal, when you're logged in remotely, is that X clients run on the server host (like nice.harvard.edu) and X servers run on client machines, like your own PC.

SSH!

To connect to one of the NICE servers, you must use SSH, the secure connection protocol. The most obvious use of the SSH connection is to let you type commands to the remote computer and see the results that it puts out. But the same secure connection can also carry the messages used by the X window system to control windows on your screen or read input from your mouse and keyboard. If you configure it to do so, SSH makes X clients like Emacs think that there is an X server on the same machine that they are running on, and it also makes the X server on your PC think that its remote clients like Emacs are actually local. (You may see this called tunneling or port forwarding. After you learn about networking later in the term, it will make more sense.) This arrangement means that you can tell your X server only to accept ``local'' connection requests. Requests that come through the SSH tunnel from your remote X client will be accepted, since they appear to be local. But any other X connections will be rejected, which is a good security feature.

By default, forwarding of X connections through SSH is not turned on. To be sure that it's enabled when you need it, you can change your personal SSH configuration. Here's a series of commands to do that when you're logged into fas.harvard.edu or nice.harvard.edu:

mkdir -p $\sim$/.ssh
chmod go-rwx $\sim$/.ssh
cd $\sim$/.ssh
touch config
echo "ForwardX11 yes" » config
The mkdir above makes sure that you have a .ssh directory in your home directory. Then the chmod command makes sure that no one but you can read or write that directory. The touch command makes sure that a file called config exists under $\sim$/.ssh. And the echo command adds this line to the end of that file:
ForwardX11 yes
Of course, if you'd rather just use an editor to make sure that $\sim$/.ssh/config exists and contains the line above, that's fine too. But remember to use chmod to restrict access to the config file.

No matter how you make your SSH connection to nice.harvard.edu, there's a simple way to tell whether it's capable of supporting X windows. Type the command echo $DISPLAY at the shell prompt from the remote machine. The response should look like localhost:$nn$.0, where $nn$ is a two- or three-digit number. SSH should have set DISPLAY automatically to this value. When an X client like Emacs uses this setting of DISPLAY to contact its X server, the messages it sends are piped through the secure SSH tunnel to your terminal.

If the DISPLAY value is something else, then your current shell initialization may be setting DISPLAY explicitly, rather than allowing it to be set by SSH. Use an editor to inspect the files $\sim$/.login and $\sim$/.cshrc, and eliminate commands that set DISPLAY.

Setting up Software for Microsoft Windows

If your personal machine runs Microsoft Windows, you need to install third-party packages for SSH and for the X window system.

You've probably already installed an SSH-based terminal emulator called SecureCRT that FAS makes available on its software download page.[*]We'll first describe how to add a SecureCRT ``session'' for connecting to nice.harvard.edu.[*] Then we'll show how to add an X server that can work together with SecureCRT to give you GUI control of Emacs and other X-client applications.

SecureCRT.

SecureCRT comes preconfigured with sessions for connecting to Harvard destinations such as nice.fas.harvard.edu (which is equivalent to nice.harvard.edu). A session customizes SecureCRT for a particular remote host. There's a button for starting each of the configured sessions in the Connect dialog box that (by default) comes up when you launch SecureCRT.

Unfortunately, the preconfigured sessions need some tweaking for use with X windows in general and Emacs in particular. Rather than changing the nice.fas.harvard.edu session, we recommend that you modify your default-session settings and apply the changes to all existing sessions. That way, SecureCRT will behave consistently no matter which remote host you connect to.

SecureCRT version 5.

Here are the steps for configuring the default settings for SecureCRT version 5 or later and applying the changes to existing sessions.[*]

Try out your new session settings by double clicking on nice.fas.harvard.edu. You may see a New Host Key dialog box. Click Accept & Save, then finish logging in. Try typing the command hostname, followed by Enter. The server should print its own name as ice$k$, for some digit $k$. Next, issue the command echo $DISPLAY to verify that the value is localhost:$nn$.0, where $nn$ is a two- or three-digit number. If so, proceed to the next paragraph and install the X server. If SecureCRT is not working as expected, bring up the Connect dialog again, select nice.fas.harvard.edu and click on the Properties icon, fourth from the right in the task bar of that dialog box. Check the session options against the instructions above to be sure everything has been set and saved correctly.

XMing.

How to Run Emacs

Now suppose that you have a terminal window connected to a nice.harvard.edu server, and that your $DISPLAY variable is correctly set. To start Emacs, putting it into the background so that you can keep using your terminal, type[*]

emacs &

Emacs without X.

Of course, there will be times when you want to run Emacs, but X isn't available. In that case, you start Emacs without putting it into the background (no & on the command line). If X is available (so your $DISPLAY variable is set), but you want to use Emacs in non-X mode anyway, add -nw as a command-line option:
emacs -nw

In non-X mode, you'll have to experiment to find out which keys work as they do under X and which don't. It varies from terminal emulator to terminal emulator. There's a way to get everything done, but it may sometimes be inconvenient. The ``GNU Emacs Reference Card'' can be used as a survival aid. Except for the strictly mouse-related commands, it usually shows at least one key sequence per command that will work on almost any terminal.

When configured properly, the SecureCRT and PuTTY terminal emulators provide surprisingly good windows in which to run Emacs. But, Emacs is much nicer to use under X.


Footnotes

...nice.harvard.edu.[*]
When logging in remotely to work on CS50, always use nice.harvard.edu. You shouldn't use ice.harvard.edu or fas.harvard.edu. Since the actual name of the host that you reach by connecting to nice.harvard.edu is not nice, but ice$k$ (e.g., ice2), you might think that ice.harvard.edu is a synonym for nice.harvard.edu. It's not. It's the generic name for connecting to a different kind of computer, running a different operating system. The machines that you reach when you connect to fas.harvard.edu do have the right hardware and operating system, but they lack some of the software that you will need, such as Emacs for X windows. They're just not nice.
...[*]
http://www.fas.harvard.edu/cgi-bin/software/download.pl
...nice.harvard.edu.[*]
PuTTY (http://www.chiark.greenend.org.uk/~sgtatham/putty/) is another popular terminal emulator that implements SSH. It's free, and it includes all of the features that you'll need for work in CS50. If SecureCRT bores or bothers you, check out PuTTY.
...[*]
If your SecureCRT installation is older than version 5, these instructions won't fit it. Update to the version offered at http://www.fas.harvard.edu/cgi-bin/software/download.pl.
... type[*]
Remember that you should kill all background processes before logging out. To remind you, and give you a chance to exit Emacs gracefully, the $\sim$lib50/usr/cs50.logout script emits the message
Waiting for background jobs to be terminated...
and then waits until all background processes have finished.
CS51 Course Library 2008-02-03