CS50 Outside of the Science Center -- Apple Mac OS X

Computer Science 50

Fall 2006

February 5, 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 Mac OS X

You will need to get Apple's implementation of X11 on your machine. You can find out what version of OSX you have by clicking on the apple logo on the top left and then clicking "About this Mac". If you have version 10.3 (or any 10.3.*), you can download X11 here: http://www.apple.com/support/downloads/x11formacosx.html . If you have version 10.4 (or any 10.4.*), there is a link to X11 in the Announcements section of the Bulletin Board. Finally, if you have version 10.5 (or any 10.5.*), it should already be installed-go to the magnifying glass in the upper right and search for "X11" there.[*]

To install X11, double-click on the .pkg file. If you downloaded it from the CS51 link, you will need to unzip it before you double-click the .pkg file.

After installation, you'll be able to launch the X server by clicking on X11 in your Applications folder. For easy access, you may want to drag the X11 icon from your Applications folder to your dock.

Launch X11. By default, a terminal window (xterm) should appear. Later you can use the xterm window for connecting to nice.harvard.edu. But first, you should configure X11 for ease of use with Emacs and SSH. Choose Preferences from the X11 menu, and select the Input tab.

Quit X11, restart it, and check preferences just to verify that the settings were saved.

Mac OS X includes the same implementation of SSH that is used by Linux, so in an xterm window, you can run SSH to make a remote connection. First set up $\sim$/.ssh/config on your Mac almost exactly as you have it in your FAS home directory. Instead of adding just one line to that file, however, make sure that it contains the following two lines:[*]

ForwardX11 yes
ForwardX11Trusted yes
Now you can use the xterm window. (If it hasn't popped up, start one by choosing Terminal from X11's Applications menu.) Run the command
ssh username@nice.harvard.edu
where username is your FAS user name.[*]When you have a prompt from one of the NICE servers (whose names are look like ice$k$ for some digit $k$), type echo $DISPLAY. If the DISPLAY environment variable is defined and looks like localhost:$nn$.0, you're all set. Try typing xeyes followed by Enter or Return in the terminal that's connected to the remote computer. Move the mouse around to check tracking, then type C-c (Ctrl key plus c) in the terminal window to close your eyes.

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.
...[*]
X11 is just a synonym for X, which went through eleven versions by the time it was done. Although X10 is ancient history and there won't be an X12, the ``long'' name X11 is sometimes used to distinguish the X window system from the other popular X's, such as Mac OS X, X-Men, the X-Files, and so on.
...[*]
Eventually, there should be no difference between $\sim$/.ssh/config under your FAS account and the corresponding file on your Mac. The reason for the disparity is that you may need to use your FAS configuration with legacy SSH implementations, which don't understand the ForwardX11Trusted parameter.
...[*]
If you have the same user name on your own machine, then you can shorten the ssh command to ssh nice.harvard.edu.
... 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-05