Automatic Unix DISPLAY variable export

With the servers that I manage, I sometimes need to run a command that starts some sort of GUI interface requiring an X server. This holds especially true if the server holds an Oracle database. The GUI tools Oracle provides make it easy to install Oracle, use the database configuration assistant (DBCA), run the wallet manager (OWM), and other utilities, however they all require an X Server.

What I don’t like to do is to have to determine my local computer's IP address, and from the Unix server manually export my DISPLAY to that IP address every time I want to run one of the programs requiring an X server.

Instead, what I do on my RedHat Linux AS3 servers is edit the /etc/profile script, and add in one simple line:

export DISPLAY="`who am i | awk '{print $NF}' | tr -d ')''('`:0.0"

Now when you login, the who am i command is run to grab the current user and IP address. Then it is run through awk to strip out everything except for the IP address. Lastly the tr command removes the parenthesis around the IP address. I manually append on the 0:0 string to the result. The result is your DISPLAY environment variable being setup to your IP address.

Go ahead and try it out. Add the code above to the end of your /etc/profile file, re-login to your Unix box, and type:

echo $DISPLAY

Your IP address with a :0.0 appended should be output. On my server the output looked like:

192.168.1.1:0.0

If you have an X Server running on your remote workstation you can try one more test. Run the command xclock from your Unix shell prompt. This should open up a graphical clock on your workstation. I typically just use Cygwin for my remote X-Server as it’s free and easy to use. Maybe another day I’ll describe how to install and configure Cygwin.

As I said above, this script has been tested on a RedHat Linux AS3 system, but should work on most other Linux systems. Hopefully this simple tip makes your life a little bit easier. It's one of the first things I do on new Linux installs.



v2.0