Sunday, February 8, 2004

To X or not, for RedHat linux

Sometimes it's useful to have an X GUI on your linux box, sometimes it just consumes resources. And sometimes you just want to ssh in and use xterm from a pc. I finally sat down and figured out the ideal setup for me, and this may help you as well. Especially if you are using linux primarily as a server, but still want the X GUI for days when it's your workstation.

Background

As I'm sure you are aware, linux can run with X (the graphical display) and use desktop managers such as Gnome, etc. This document is not a howto on X, GUIs, or the likes. This document assumes you already have a functioning linux system up and running with X, and want to turn that off and on as needed.

Run level services

Linux has up to 9 runlevels that it can run in. Each runlevel determines a set of features that are to be installed or used. There are six interesting runlevels, which are described in the file /etc/inittab as follows:

# Default runlevel. The runlevels used by RHS are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this) 

Runlevels 3 and 5 are the two that are used for an "up and running" system. As indicated, only runlevel 5 has the X11 GUI. Thus, by changing the runlevel from 5 to 3, we can disable X11 GUI. And changing from 3 to 5 we can enable the X11 GUI. The following two commands do that:

su -
(type root password)
init 3

Changes to runlevel 3, thus disabling the GUI.

(still as root)
init 5

Changes to runlevel 5, thus enabling the GUI.

Other services

On my install, I found that in runlevel 3, I did not have the samba or web servers enabled. I'm not sure if that's just RedHat 8 distro, or common to many distro's. But here's how to fix it.

We want to see exactly what services are enabled and disabled differently between the runlevels. The directory /etc/rc.d has directories for each runlevel as shown below.

[root@devx]# cd /etc/rc.d
[root@devx]# ll
total 64K
drwxr-xr-x    2 root     root         4.0K Aug 15 05:03 init.d/
-rwxr-xr-x    1 root     root         2.3K Jul 14  2002 rc*
drwxr-xr-x    2 root     root         4.0K Jul 24 12:15 rc0.d/
drwxr-xr-x    2 root     root         4.0K Jul 24 12:15 rc1.d/
drwxr-xr-x    2 root     root         4.0K Jul 24 12:15 rc2.d/
drwxr-xr-x    2 root     root         4.0K Aug 15 04:48 rc3.d/
drwxr-xr-x    2 root     root         4.0K Jul 24 12:15 rc4.d/
drwxr-xr-x    2 root     root         4.0K Jul 24 12:15 rc5.d/
drwxr-xr-x    2 root     root         4.0K Jul 24 12:15 rc6.d/
-rwxr-xr-x    1 root     root          220 Jul 10  2001 rc.local*
-rwxr-xr-x    1 root     root          22K Aug 22  2002 rc.sysinit*
[root@devx]#

The init.d directory contains all the scripts that are used to start and stop processes. A look in a directory such as rc5.d reveals a link back to a script in init.d for each process that should be killed or started. This is a small sample of the listing for rc5.d:

(lines shortened by removing owner/group)
[root@devx]# ll rc5.d
total 0
lrwxrwxrwx    1    19 Feb 25 17:19 K05saslauthd -> ../init.d/saslauthd*
lrwxrwxrwx    1    16 Jul 22 14:58 K12mysqld -> ../init.d/mysqld*
lrwxrwxrwx    1    20 Feb 25 17:49 K15postgresql -> ../init.d/postgresql*
lrwxrwxrwx    1    13 Feb 25 17:25 K20nfs -> ../init.d/nfs*

lrwxrwxrwx    1    15 Feb 25 17:22 S05kudzu -> ../init.d/kudzu*
lrwxrwxrwx    1    18 Feb 25 17:24 S08iptables -> ../init.d/iptables*
lrwxrwxrwx    1    14 Feb 25 17:22 S09isdn -> ../init.d/isdn*
lrwxrwxrwx    1    17 Feb 25 17:19 S10network -> ../init.d/network*
lrwxrwxrwx    1    16 Feb 25 17:18 S12syslog -> ../init.d/syslog*

Links that start with K are processes that are killed (the indicated scripts are run with a parameter stop) and those that start with S are started (the scripts are run with the parameter start). To find out any differences between runlevels, do the following:

[root@devx]# diff rc3.d rc5.d
Only in rc3.d: K15httpd
Only in rc3.d: K35smb
Only in rc5.d: S85httpd
Only in rc5.d: S91smb
[root@devx]# 

This indicates that runlevel 3 stops httpd (web server) and smb (samba server), while runlevel 5 starts both of these. To make these runlevels consistent, do the following:

mv rc3.d/K15httpd rc3.d/S85httpd
mv rc3.d/K35smb rc3.d/S91smb
[root@devx]# diff rc3.d rc5.d
[root@devx]# 

Now, when you change to runlevel 3 via the "init 3" command, you won't lose these services.

Starting X from runlevel 3

You can start the X11 GUI from runlevel 3 with the following command:

2% startx

Once started, you can exit from the X11 GUI by simply logging out (using the RedHat menu), and you'll be back to your nice memory saving text console.

Default run level

Setting the default runlevel is also easy. Just su to root, and open the file /etc/inittab. Change the following line:

#id:5:initdefault
id:3:initdefault

Conclusion

Now when you boot your machine, it will have simple console prompt. It is still multiuser, so you can Alt+F1, Alt+F2, etc. to separate console windows. You can ssh / telnet into the box, and even bring up xterms on another workstation. Finally, you can start the X11 GUI as needed on the console by simply logging in and running 'startx'. Logging out of the GUI brings you right back to the nice console windows.

No comments: