The X Window System

Last modified: Aug 24, 2023
Contents:

X is the windowing system for Unix. By running X, you can have several windows on the screen open at once, each devoted to a different task. For example, you can be reading electronic mail in one window while a lengthy compilation is running in another. X also allows the display of graphics and of a variety of fonts.

1 X - an Overview

If you were seated at a Unix or Linux system, you would treat X much the same as you normally treat Microsoft Windows on most PCs - it’s there, it lets you launch your programs, and it makes things look pretty. Most of the time, you are less interested in Windows than in the programs that you are running under it.

The reason that X is of interest in this course is that X was designed, from its beginnings, for remote use. That means that, even if you are not seated at a Unix or Linux system, you can run programs on a remote Unix/Linux machine and you can see and interact with programs that are running on some other, remote machine somewhere else on the network.

Now, you can do remote access with Windows as well (“remote desktop”). But that gives you a separate “box” on your screen that often “feels” very unlike the local applications you might be running on your own machine. And, because you have to transmit an entire desktop worth of graphics, this can be slow and unresponsive if your network connection isn’t up to it. With X you can transmit an entire desktop if you wish, but for remote access you have the option of transmitting an application’s windows separately and having them look and behave like windows on your local machine’s operating system.

1.1 Clients and Servers

To use X, you need to run two different programs. One is an X server, the program that is responsible for controlling the display of your local machine. The server must run on the machine at which you are seated — its job is to actually draw things on your display and handle mouse and keyboard inputs.

The other is an X client, which is simply an application program that was compiled to work with X. The client program might be running on your local machine (e.g., if your local machine runs Linux or OS/X) or on a remote machine somewhere on the Internet, such as one of our Dept. Linux machines.

To run X, then you need to

  1. Run an X server on your local PC.

  2. Launch an X client application, probably on a remote machine.

This assumes, of course, that you have X server software installed on your local PC (or are carrying a portable X server on a flash drive). You may also need software to launch the remote application. Typically, this is done via SSH.

The course Resources page has tips on what X servers and SSH programs are recommended, particularly for Windows PCs, and how to get them.

1.2 X Window Managers

X is a windowing system that can present a number of different appearances to the user. The appearance and behaviors that you actually see is controlled by a window manager, a program that is generally run as part of the X start-up procedure. A window manager controls both cosmetic details like the colors used for window borders and other control elements, but also what elements actually appear on each window (e.g., does every window get a “close” button, what does that button look like, and where does it appear in the window?) and what menus appear in response to mouse clicks in various parts of the display. Figure 1, for example, shows X running the twin (Tom’s Window manager). Compare to Figure 2, which shows the same programs running under the ICE window manager.

 
 
 

Of course, if you are sitting at a machine that already runs a windowing operating system, then you can usually just use that as your X window manager. Figure 3 shows those programs running under a version of X that uses MS Windows as its window manager. That’s the approach we’re going to explore.

 

1.3 X Performance and Your Internet Connection

One thing that should have been obvious from the examples above is that, in X, you are sending a lot more than just plain text from the remote machine to your local PC. Graphics will require a lot more bytes than text, and it takes time to transmit those bytes. Even “plain text” may require a lot more time than it would in an ordinary text-mode SSH session. To send a line of text under X, the remote machine may need to send information on what font to render the text in, and may in some cases even need to send the font itself to your PC.

If even ordinary text-mode connections seem slow to you, you will have real difficulties running X. Nonetheless, you can’t expect to get useful computing work done transferring only plain text in all circumstances, so trying to work out a connection scheme that you can live with is important.

When we talk about connection speed to the Internet, most Internet service providers (ISPs) only talk about how many bytes per second they can ship to you. For web browsing and audio/video streaming, that may be the most important factor. But programming activities tend to be highly interactive. Even in a telnet session, you type a command, you pause to see it echoed on your screen so you can make sure it’s what you wanted, you hit the Enter or Return key, you wait for a result to appear on your screen, then you start typing the next command. It’s that back-and-forth nature that we need to think about.

For our purposes, then, what’s important is not only the bandwidth, the number of characters per second that can flow along a connection once the data stream has gotten started, but also the latency, the amount of time it takes for the last byte of a request to go from your machine the remote machine and, assuming the remote machine responds immediately, for the first byte of its response to reach you. Think of the Internet as a hose (or a series of tubes) carrying water. A fire hose is high-bandwidth — it’s nice and wide so that, once the water is flowing, you can get a tremendous amount of water, especially as compared to a narrow (low-bandwidth) garden hose. But if either kind of hose is very long, then there is a long wait from the moment when someone on one end turns the spigot on and when you see the water emerge from the open end - that’s high latency.

X dates back to a time when networks were far less reliable and packets of information were often lost during transmission.
When X is displaying something complicated on your screen, there’s a lot of back-and-forth communication. The remote X client sends a packet of information, then waits for your X server to acknowledge that the packet was received and is correct before sending the next packet. That means that latency is at least as important as bandwidth in determining how well X will work for you.

Ideally, you would have an ISP that gives you both high bandwidth and low latency. Some cable services and DSL will give you that. Dial-up services are fairly low bandwidth, but the better ones offer very low latency and so may be OK with X.

Beware of “satellite broadband” ISPs. These bounce their data streams off a satellite in orbit. Even at the speed of light, this adds a lot of time to the delivery of each byte. Satellite broadband can offer high bandwidth, which is good, but they also frequently have very high latency. Even text-mode connections can be painful on a satellite broadband service. You hit Enter and wait…. Satellite broadband may be fine for web browsing, email and even for streaming audio or video because, once the data stream gets going, it just flies. But most programming tasks are interactive, so latency is more critical for programming students.

Luckily, there is an option for people with moderately high-latency connections. The CS Dept’s Linux SSH servers support X2Go, a variation on the X protocol that exploits data compression and caching to offer tremendous speed improvements in high-latency situations.

1.4 X and Firewalls

To recap, if you want to run an X application on a remote machine, then you need to

  1. Start an X server on your local PC.
  2. Launch the X client application on the remote machine.

Now, there’s something quite unusual about this comapred to the other network protocols we have employed in this class.

When you do web browsing, or read email, or connect via SSH, or transfer files via SFTP, the client has always been on your local PC and the server has been on the remote machine. X reverses this pattern. Your local PC is the server that offers to display graphics sent from client programs running on remote machines. That represents a challenge to the firewall that protects your PC.

1.4.1 What Does a Firewall Do?

Firewalls are software security programs that most people use to protect their networks from outside hacking. Even if you don’t think you are running a firewall on your PC, there is probably firewall software incorporated into the router that connects your home network to the Internet.

Communication of any kind of service or data over the Internet is addressed to a socket, a combination of a specific machine’s address (the IP address) and the number of a specific port (a communications channel). Some port numbers are reserved for specific network services: SSH, SFTP, web browsing, and email, for example, all have their own specific port numbers. But any program can try to accept communications from, or send communications to, any port number on itself or on some other machine.

A firewall is a hardware or software system that restricts the ports that can actually be used to communicate with a particular machine. A firewall can be set to

  1. allow both incoming and outgoing communications on any port (in effect, no firewall at all)
  2. allow outgoing communications (from your PC) on any port to other machines, and replies from that machine on that same port. Incoming communications from other machines and on other ports are blocked.
  3. allow only outgoing communications (from your PC) on selected ports (such as those for web browsing and email) to other machines, and replies from that machine on that same port.
  4. allow no communications on any port, in either direction (in effect, unplugging from the network)

Most firewalls are set, by default, designed to run at level 2 above. At that level, your PC can initiate communications with other machines, and can get responses from them. But other machines cannot initiate contact with your PC. Another way of saying this is that your PC can be a client for services, but it cannot be a server taking requests for services from other machines. But that’s exactly what X requires.

When you connect to a remote machine is text mode, using SSH or the older telnet protocol, your PC functions as a client that initiates communications with the server via a specific port number (port 22 for SSH, 23 for telnet). The server responds using that port.

Even if your PC is behind a firewall, the connection works if your firewall is set, like most, to “local PC can initiate contact with other machines and other machines can then reply on that port”.

But if the remote machine is the client and initiates the communication, it won’t get through the same firewall.

1.4.2 Tunneling X via ssh

Luckily, there’s a way around this problem that does not require you to poke holes in your firewall (which most firewalls will let you do — many gamers do so to play multi-player games over the Internet, but it can be risky).

SSH can be used to tunnel communications to other ports within its own data stream.

An SSH tunnel is a encrypted comunnications path that appears tothe machines on each end as if it were a purely local communication. The X client program on the remote thinks that it is sending the graphics to a program on the same remote machine (the SSH server) and the X server on the local machine thinks that it is receiving the graphics from a program (the SSH client) running on that same local machine. In reality, the SSH server and SSH client are exchanging the graphics information via the SSH protocol.

1.5 What Do You Need to Run X?

To recap, if you want to run an X application on a remote machine, then you need to

  1. Start an X server on your local PC.
  2. Launch the X client application on the remote machine.

but you can also, optionally,

  1. Use an SSH client on the local PC to contact an SSH server on the remote server.
  2. Connect the X server and X client to the “tunnel” formed by that SSH connection.

2 Running X - Accelerated X Variants

X2Go packages all of these steps together, using an accelerated protocol that resolves the many problems with high latency connections.

The CS Dept runs X2Go servers on its Linux workstations.

You can run an X2Go client. When you use that client, 1. It establishes an SSH connection to the Linux server. 2. It launches an X server on your PC, connecting it to one end of the SSH tunnel. 3. It then uses SSH to ask the remote PC to launch a client program of your choice, which is connected to the other end of the SSH tunnel.

2.1 X2Go Client

The X2Go client is my recommended way for most people to use X on our servers. X2Go clients are available for Windows, OS/X, and Linux machines. Go to the Resources page for information on how to obtain the X2Go client and install it on your local machine.

With X2Go you will be launching the X server on your local machine and a client program on the remote machine in one step.

2.1.1 Setting Up an X2Go Session

 

The first time you run the client, you will see a main window looking like this. The left column is used to present information on your current remote connection. The right column shows the session presets that you have created and saved. Initially, both will be empty.

 

A “new session” dialog may pop up immediately on your first execution of the client. If not, find the new session button or the Session->new menu item to launch it.

 

To define a new session:

  1. Fill in an appropriate name for your session.

  2. Choose one of our Linux servers and fill its name in for the “Host:”.

    • What server should you use? I recommend that you pick any one of the “real” servers, rather than linux.cs.odu.edu, the “random server selection” machine.
    • Why? Because if you shut down your X2Go session, deliberately or accidentally, without having closed the programs you were running, X2Go will preserve your running programs and allow you to reconnect to them, but only if you reconnect to the same server.
    • Once you have successfully set up a session for one server, go ahead and set up some more sessions for the other servers, so that if a server is down, misbehaving, or overloaded, you can quickly move to a different one.
  3. Fill in your CS Dept. network login name, just as you normally type it to log in to our Linux servers, to the “Login” field.

  4. Change the “Session type” to “Single application”.

  5. Type “xterm” for the command.

    This won’t be in the drop-down list of suggestions. Just type it in.

     

  6. On the Media tab, clear the check boxes for sound support and printing.

  7. Click “OK” to save those settings.

 

You’ll be returned to the main window. Your new session settings will appear in the right column.

 

If you later want to change some of the settings (or remove them), there is a small button on the lower right of a settings box that drops down a menu allowing you to modify your settings.

2.1.2 Running an X2Go Session

 

From the main X2Go client window, click on a session settings box in the right column to open a connection to the remote machine.

 

Your session info moves over to the left column. Fill in your password and click OK to connect.

 

Within a few seconds, an xterm window should pop up.

To close your session, simply exit from all programs you have launched from your xterm, and then type “exit” in the xterm (just as if you were logging out of a familiar SSH session).

3 Running “Classic” X

Most people should use the accelerated X package, X2Go.

But, if you are on our local network and don’t see an accelerated package available, you might be able to use one of these unaccelerated X servers instead. You might also find this information useful if you yourself have two or more Linux or OS/X machines on your home network and want to remotely run programs from one on the other.

If you are using X2Go, however, you cna skip this section and move on to (Working in X)[#working-in-x].

3.1 “Native” X Servers on Linux or Mac OS/X

If you are running a Linux PC and see windows, menus, etc., when you log in to your local machine, then you are already running an X server and don’t need to do anything special in this step. (Linux is an option for MS Windows users as well. You can get Linux distributions that can be booted and run from a CD or a flash drive. See the Resources page for further information.)

Apple OS/X used to include a native X server. Later this became a package that was provided on some Mac models but not others. Now X is no longer provided on new Macs, but Apple supports the the XQuartz X server project. Check the Resources page for information on this.

On these systems, your X server is always running. So you just need to launch a client program on a remote machine. You can do this via SSH.

3.2 Launching a Client via an ssh command

Open a terminal/console window where you can type commands.

Give the command

ssh -Y -f username@machinename xterm

replacing “username” by your CS Unix login name and replacing “machinename” by the name of one of the CS Linux machines.

An xterm window should open up within a few seconds. Commands that you type into this new window are being run on the remote machine. For example, if you type

xterm &

you will see another new xterm window open up within a few seconds.

If you find these ssh commands a bit tedious to type out, consider adding some lines to your ~/.bashrc file1 on your local machine to add a simple alias for these ssh commands, similar to the aliases shown here. For example, I have

alias linux="ssh -Y -A -f zeil@linux.cs.odu.edu xterm -title linux -sl 500 -sb "

and similar aliases for all the remote machines I use on a regular basis. Then I can launch a remote xterm by simply typing the machine name.

3.3 StarNet X-Win32

StarNet X-Win32 is a commercial X server for Windows machines. It is installed on most of the CS Dept. lab machines.

 

From the “Start” button menu, find and run X-Config. A dialog box should come up listing known connections. If you are working on a lab machine, there may be some connections pre-defined. But these won’t necessarily be to machines that you want to connect to. If none of the connections are what you want, click the Wizard… button to start defining a new connection.

 

Give the connection a descriptive name and select “ssh” as the connection type.

 

Give the full name (including the “.cs.odu.edu”) of one of the Linux servers as the host.

 

Enter your login name, if you wish, but leave the password empty.

 

Select “Linux” from the commands list to get the appropriate xterm command into the command box.

 

You’ll be returned to the main X-Config window. Select your newly created connection, and click the Launch button. You’ll be prompted for your password, then an xterm window should open up shortly. You should also see a small blue X icon in your taskbar tray. If you want more applications on the same remote machine, launch them from that xterm.

If you exit from that xterm, you will see that even after it closes, X is still running — the X icon will still be in your taskbar tray. Right-clicking on this will bring up a menu. Under “My Connections”, you will find the connection that you have just created. You can launch a new xterm by simply selecting this.

In future sessions, if you rerun the X-Config program, you will find your new connection listed and you can simply select it and click the Launch button. Alternatively, you can run X-Win32 instead of X-Config, then right-click on the taskbar icon and select the saved connection from the menu.

3.4 Xming

xming is a free, open-source X server for Windows machines.

There are a few different ways to launch a client with xming. The easiest way is to use the XLaunch program (find it in the usual Start button menu) to start a wizard that will start Xming and a remote application at the same time. This is only good for starting your first remote application (usually xterm). If you want more applications on the same remote machine, launch them from that xterm.

 

Once you have started XLaunch, I recommend that you select “Multiple Windows”.

 

Because we’re trying to keep this a simple, one-step process, select “Start a Program”. (You would choose to start no client if you intended to later start a client in a separate step.)

 

For the program, type “xterm”. Select “Using PuTTY (plink.exe)”. For the name of the computer to connect to, use any of the CS Linux machines. Enter your login name and your password.

 

If you have a three-button mouse (the middle “button” is often a clickable scroll wheel), leave the “Additional parameters for xming” blank. If you have a two-button mouse, you might try adding the Xming parameter “-emulate3buttons 50”. This will allow you to simulate clicking a middle mouse button by pressing both mouse buttons at once.

3.5 Cygwin/X

CygWin is a free *nix emulation for Windows. Many open-source programs originally developed for Linux can be compiled, without any changes, in CygWin, yielding a Windows version of that Linux program.

If you have installed Cygwin/X, you may have a shortcut on your desktop or in your start menu to run it. Do so. You should see a small black “X” in the tray at the right end of your Windows task bar.

If you do not have such a shortcut, you can open a CygWin command window and give the command

startxwin

You should see the small black X mentioned above and will also probably see an xterm window open up in a few moments. This is an X application that is running on your local machine, not on a remote Unix box. This is your signal that the X server is running.

To launch a client program on a remote machine, follow the same instructions as given earlier for launching clients from Linux and OS/X machines using ssh commands.

4 Working in X

Example 1: Try This
  1. Once you have X working, try opening up some additional windows with commands like:

    xterm &
    xterm -bg Yellow -fg Green -sl 1000 -sb -geometry 40x16 &
    xclock &
    emacs &
    evince ~cs252/public_html/clientserver.eps  &
    

    The exact appearance of what you will see will depend on the window manager used by your X server.

  2. Most programs that run under X support a very simple “copy-and-paste” facility. Simply drag the mouse across a block of text in any window while holding down the left mouse button. Then position the mouse into a window where you would like that text to be “typed”. Click the middle mouse button, and the selected text will be sent to that window just as if you had typed it yourself.

    Try typing one of the above commands (again) into one of your open xterm windows. Left-drag the mouse across that command. Left-click on the other xterm window to select it. Position your mouse near the cursor and click your middle mouse button.2 Hit Enter/Return if necessary, to complete the command.

A few things to keep in mind:

4.1 Process Control

Once you start using the & to run commands in the background, it’s easy to accumulate a large number of processes, all running simultaneously. It’s worthwhile at that point to learn a little bit about how to control your running processes.

Each time you type a command, a new process is launched to run that command. (That process may, in turn, launch other processes.)

When we talk about a process running in the foreground or in the background, we’re mainly talking about its relation to your keyboard. If a process is in the foreground, your command shell will not accept new keyboard input until the process finishes. If you do type additional characters, they will be passed on to the standard input of the foreground process (unless you have piped or redirected that input). If a process is in the background, the command shell will not wait on that process to complete before prompting you for another command.

You can run any command in the background by appending an ampersand (&) to the command line when you launch it. Now, it may not make a whole lot of sense to run many commands in the background. If they really need standard keyboard input or are going to be dumping a lot of text on your screen via standard input, you probably want to keep it in he foreground. But most X commands don’t use standard input and output. They communicate with you via the graphics interface. So you can, and probably should, launch most X commands in the background.

You can move a running command from the foreground to the background. Typing a ^z will ask the shell to pause the current foreground command. Then you can give the command

bg

(background) to move it into the background.

You can reverse this as well. The command

fg

(foreground) will move the most recently backgrounded command back to the foreground.

You can use the ps command to get a listing of the processes you have running. This command has a bewildering array of options, but the most useful is the -u option that allows you to request a list of all processes belonging to a particular user. (Even if you are running on your own private Linux box, you’ll find that there are dozens of processes belonging to “root” or other specialized accounts created for administrative purposes.)

For example:

> ps -u zeil
PID TTY          TIME CMD
6924 ?        00:00:00 sshd
6925 ?        00:00:00 tcsh
6927 ?        00:00:00 nxnode
7273 ?        00:00:00 nxnode
7276 ?        00:00:00 nxnode
8047 ?        00:00:00 nxnode
8050 ?        00:00:00 nxnode
8052 ?        00:00:00 tee
8054 ?        00:00:00 nxnode
8059 ?        00:00:00 nxnode
8094 ?        00:00:41 nxagent
8381 ?        00:00:00 xterm
8436 pts/59   00:00:00 tcsh
11619 pts/59   00:00:06 emacs
14827 pts/59   00:00:37 xxe
25369 pts/59   00:00:00 dbus-launch
25370 ?        00:00:00 dbus-daemon
25373 ?        00:00:00 gconfd-2
33127 ?        00:00:00 gnome-settings-
79867 pts/59   00:00:00 ps

The output shows 4 columns. The PID column gives the unique process ID of each running process. The TTY column isn’t all that important. The TIME column shows how much CPU time has been consumed by this process. (If this number gets extremely large, it can indicate a program caught in an infinite loop.) The CMD is, perhaps the most interesting. It shows the name of the program running in that process. “sshd” is my SSH connection to this machine. “tcsh” is my command shell (running in the “xterm” shown later.) Because I am using NX, there are several processes associated with that. You can also see that I am running emacs and a program named xxe, which is the editor that i use to prepare these lecture notes. At the very bottom of the list, you can see the process running the ps command that actually produced this output.

As noted earlier, it’s common for some processes to launch other processes. Adding the -f option to the ps command gives a great deal more information about the processes, including a PPID column that tells you the process ID of the “Parent” process that launched this one. For example,

> ps -fu zeil
UID        PID  PPID  C STIME TTY          TIME CMD
zeil      1020  8436  0 09:50 pts/59   00:00:00 /bin/sh /home/zeil/usr/local/Doc
zeil      1024  1020  2 09:50 pts/59   00:00:52 java -Xss4m -Xmx512m -DXXE_GUI=
zeil      2585  8436  0 10:23 pts/59   00:00:00 ps -fu zeil
zeil      6924  6907  0 09:02 ?        00:00:00 sshd: zeil@notty    
zeil      6925  6924  0 09:02 ?        00:00:00 tcsh -c /usr/lib/nx/nxnode --sla
zeil      6927  6925  0 09:02 ?        00:00:00 /bin/bash /usr/lib/nx/nxnode --s
zeil      7273  6927  0 09:02 ?        00:00:00 /bin/bash /usr/lib/nx/nxnode --s
zeil      7276  7273  0 09:02 ?        00:00:00 /bin/bash /usr/lib/nx/nxnode --s
zeil      8047  7276  0 09:02 ?        00:00:00 /bin/bash /usr/lib/nx/nxnode --s
zeil      8050  8047  0 09:02 ?        00:00:00 /bin/bash /usr/lib/nx/nxnode --s
zeil      8052  8047  0 09:02 ?        00:00:00 tee /home/zeil/.nx/C-atria-2056-
zeil      8054  8047  0 09:02 ?        00:00:00 /bin/bash /usr/lib/nx/nxnode --s
zeil      8059  7276  0 09:02 ?        00:00:00 /bin/bash /usr/lib/nx/nxnode --s
zeil      8094  8050  2 09:02 ?        00:01:58 /usr/bin/nxagent -persistent -R
zeil      8381  8059  0 09:02 ?        00:00:00 xterm
zeil      8436  8381  0 09:02 pts/59   00:00:00 tcsh
zeil     11619  8436  0 09:36 pts/59   00:00:08 emacs xwinlaunch/xwinlaunch.dbk
zeil     25369     1  0 09:06 pts/59   00:00:00 dbus-launch --autolaunch=d29af3f
zeil     25370     1  0 09:06 ?        00:00:00 //bin/dbus-daemon --fork --print
zeil     25373     1  0 09:06 ?        00:00:00 /usr/lib/libgconf2-4/gconfd-2
zeil     33127     1  0 09:24 ?        00:00:00 /usr/lib/gnome-settings-daemon/g

In this listing, you can see that the tcsh shell in process 8346 was launched by process 8381, which is an xterm that, in turn, was launched by one of the NX processes, 8059. Among the other information provided are the time at which each process started (STIME) and a more detailed view of the command actually running in the process.

The ability to see process IDs can come is useful if you have a runaway process that will no longer respond to your keyboard. For example, if in your own programming, you accidentally create a program that goes into an infinite loop, the first thing that you would try is ^c to kill that process. But of that does not work, or if the process is in the background and won’t receive any such keyboard commands anyway), you can attempt to kill the process by command.

kill processID

will attempt to kill the indicated process ’gently“, giving it a chance to save and close files and perform other housekeeping. If a subsequent check, after a few seconds, with the ps command shows that the errant process is still running, you can give the much more definitive ”hard" kill:

kill -9 processID

which pretty much almost always does the job.

5 Some X Applications

Here’s some X-based programs you might want to try. Remember that help files on most programs can be accessed via the “man” command.

eclipse
A powerful IDE (Integrated Development Environment) for Java and C++
evince
A viewer for Postscript and PDF files
firefox
the popular browser. Will probably be slow over X, but sometimes it’s convenient for doing a quick download.
gedit
An easy-to-use text editor.
nemiver
A graphical front end to the gdb debugger.
xfontsel
A program that lets you explore the fonts available on the machine. (Use in conjunction with xlsfonts, which gives a simple listing of available fonts.)
xterm
Probably the most commonly used X application: opens a command window.

1: On recent MacOS machines, your login shell is zsh instead of bash. So you would place this information in ~/.zshrc instead of ~/.bashrc

2: This assumes, of course, that you have a middle mouse button. If your mouse has two buttons with a clickable scroll wheel, the scroll wheel will function as a middle button. If you have a true two-button mouse, your X server may be configured to treat a simultaneous click of both mouse buttons as a middle-click. Give it a try.