[Courses][Linux comands] Runlevels!

Carla Schroder carla at bratgrrl.com
Tue Mar 30 22:00:46 EST 2004


Today is Runlevel Fun! Yaaaayyy!

How do you know what runlevel you are in?

$ /sbin/runlevel
N 5

The N means you have not previously booted to a different runlevel. If you 
had, the number of the runlevel would be displayed.

You must be root to do change to a different runlevel:

# /sbin/init 3

Sometimes you need to change to runlevel 1 to get out of trouble:

# /sbin/init 1

This shuts down all but the minimum necessary services to operate the system.

Why change runlevels with init? Good reasons:
--To drop to runlevel 1 for troubleshooting and repairs.
--To stop or reboot the system when normal methods don't work.
--To restart all services, or to switch to a different set of services

Bad reasons:
--To change from a text console session to an X session, because it must be 
done as root. Root should not run its own X session on a networked machine, 
as it is a bit of security risk. Not to mention a 
accidentally-hosing-the-system risk.
--To exit an X session

telinit works too; as it's simply a hard link to init. "Tell init"- more 
clever geek wordplay. Ha ha.  :P

To change your default runlevel, edit /etc/inittab, look for these lines:

# The default runlevel.
id:2:initdefault:

Simply change id:2 to whatever number you desire. Except 0 or 6.
If you're commitment-shy, and want the most flexibility at boot time, comment 
out the 'id:X:initdefault' line. Then init will ask you during bootup which 
runlevel you want.

This is the common Linux runlevel scheme:
0 Halt
1 Single-user
2 5 Multi-user
6 Reboot

Here are the runlevels in some of the different distributions.

Debian runlevels
0 Halt the system
1 Single-user mode with minimal services
2 through 5, multi-user modes. The defaults are all the same
6 Reboot

Red Hat
0 Halt the system
1 Single-user text mode
2 Not used (user-definable)
3 Multi-user text mode
4 Not used (user-definable)
5 Multi-user graphical mode
6 Reboot

Slackware
0 System halt
1 Single user mode
2 Unused, same as 3
3 Multiuser text-mode
4 Multi-user graphical mode
5 Unused, same as 3
6 Reboot

There may be even more variations in various distributions. And theoretically, 
runlevels 7-9 exist, though they are not used. It's easy enough to see what 
each runlevel does, simply read the rc*.d directories.

Changing runlevels just to kill an X session is drastic. It means restarting 
all services, and it will mess up any other users who are logged in. But 
using xkill is also drastic:

Ctrl+Alt+Bksp

As is using killall:

$ killall XFree86

And there is no handy init script for X.

So, make runlevel 3 the boot default, then run the 'startx' command when you 
want X. Then getting out of X, and returning to a text console, is as easy as 
logging out of X. (A side benefit is you can see all the debugging messages 
generated while X is running, by switching to the console, CTRL + ALT + F1.)

Debian users may have to take some extra steps for this to work, as Debian's 
default 2-5 runlevels are all the same, and will boot to a text or graphical 
screen, according to what was chosen at installation. 

Next week, we will learn how to edit the runlevels, which also means knowing 
how to control which services start at boot. Or, fun with rc scripts! Yaaay!

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Carla Schroder
this message brought to you
by Libranet 2.8 and Kmail
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



More information about the Courses mailing list