Welcome!

Linux Authors: Gilad Parann-Nissany, Maureen O'Gara, Glenn Rossman, Hovhannes Avoyan, RealWire News Distribution

Related Topics: Linux

Linux: Article

Run levels revisited

What happens when Linux starts

Q: I have loaded up Linux Mandrake 7.1. I am skilled in MS-DOS, Win9x, NT, and Netware; however, I am reasonably new to Linux. I have read the HOWTOs on the differences with regard to commands (ls, mv, mkdir, etc.) and the multiuser/multitasking environment. However, I do not know how a Linux system starts. I know there is no config.sys or autoexec.bat, but what takes the place of those files?

I read your recent LinuxWorld.com articles regarding chkconfig, but what is a run level, and why do I want a service to start on one particular run level rather than another? Where does the personality of a machine come from? I know IPs come from the /etc/hosts file, but what about the machine name?

--David

A: First of all, David, you are correct. Linux is not like other operating systems such as MS-DOS and Windows 9x. Linux is its own beast. Quite a few would call Linux a UNIX clone, but I don't think that is fair to Linux. Linux is much more open and capable than most UNIX systems.

When Linux starts, it calls a program named init, which runs all subsequent startup tasks. Those tasks include the multiuser environment, networking, and services such as sendmail.

The run level is just that -- the level of functionality in which the operating system is running. The levels range from level 1 to level 6, and each level has a different degree of functionality. The run levels are specified within the /etc/inittab file. /etc/inittab is the master file in which the init program looks to execute its subsequent services. The very first services it executes are those located in the /etc/rc.d directory tree.

On most Linux distributions, the startup scripts are located in /etc/rc.d. Those scripts are linked using the ln command back to the central init directory, init.d. The init.d directory is typically located in either /etc/init.d or /etc/rc.d/init.d. We will use /etc/init.d for the rest of the article.

As mentioned previously, the files that are located in /etc/rc.d are links to the actual executable files located in /etc/init.d. The links are named somewhat differently. For example, in /etc/rc2.d/, you will find a file named S10Network.

There are two things to notice here. The first is the directory /etc/rc2.d. That directory is stating that the files within it should be processed at run level 2. The second item is the S10 located in front of the word Network. The S stands for start, or start the service. The 10 is the order in which it will start. For example, you will also see S80postfix in the same directory. The postfix program is typically useless without starting the network first. Therefore, we start the network at 10 and the postfix program at 80.

If you look in /etc/rc.d/rc3.d, you will see the file S60nfslock, but you will not find it in /etc/rc.d/rc2.d. The S60nfslock program is used by the NFS suite of software and is normally used in a multiuser environment. As such, it is in the /etc/rc.d/rc3.d. In other words, it is executed at run level 3.

On the other hand, you will notice files within the /etc/rc.d/rc2.d that start with K. For example, /etc/rc.d/rc2.d/K45named. K stands for kill instead of start. The standard run level for a Linux machine is run level 3 or 5. If you are in run level 3, your system is enabled for a network and multiusers. If you are in run level 5, it is enabled for a network and multiusers, usually running the X Window system. If you are currently in run level 3 or 5, and you use the init program to drop to run level 2, init will execute the K45named script.

The different run levels are defined (per /etc/inittab in Red Hat) as:

# 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.)
#

Run level 0 is halt. At run level 0, the machine is, for all intents and purposes, off.

Run level 1 is single user mode. Think of it as safe mode on Windows 9x.

Run level 2 is multiuser mode, but without NFS.

Run level 3 is full multiuser mode or the standard run level.

Run level 4 is typically unused; I personally have modified that to be my laptop mode. When I am about to run off battery power, I use run level 4 to change some settings.

Run level 5 is X11, or the X Window system.

Run level 6 is reboot -- if you type init 6 the machine will reboot.

There are different reasons for having programs start or stop at different run levels. One reason is for your dependencies. You may need to make sure that one service is started before another. Another is diagnostics. If your machine is acting strangely, it would be nice to shut down certain services without having to boot the entire machine. The classic example is the loss of the root password. If you lose the root password, you can reboot into run level 1 and change it with the following commands:

At the lilo prompt on boot up:

init=/bin/sh rw

That will boot the machine into run level 1 with the root file system mounted read-write. It will bypass all authentication and allow you to use the passwd program to change the root password and then boot into a different run level.

That is the long and short of it. The run levels are processed by the init program to enable differing functionality levels to the operating system and/or user.

FYI, the IP actually does not come from the /etc/hosts file. The IP is set in another file, /etc/sysconfig/network-scripts/ifcfg-eth0 on Red Hat/Mandrake. The /etc/hosts file is used for general IP-to-machine name mapping for small networks that do not utilize DNS. That is a configurable option per the /etc/nsswitch.conf file. That is the file that is used to tell the system where to look first for the name service.

For example, you can set it to look at the /etc/hosts file first or only. You can also set it so that it checks the /etc/hosts hosts file but, if it doesn't find what it needs, it will check DNS or NIS.

That's it for this week's Ask the Geek. As a side note, I'm still surprised that so many people are not taught much on computer fundamentals. That is not a criticism of users but of the people creating documentation and software. David asked some good questions. I had never really thought about why Linux/UNIX does what it does. Thanks, David, for making me take a step back for a moment.

If you have more questions, post it in the Ask the Geek discussion forum or drop me a line at Ask the Geek. I will do my best to answer.

Hope you had some happy holidays and see you in two weeks!

More Stories By Joshua Drake

Joshua Drake is the co-founder of Command Prompt, Inc., a PostgreSQL and Linux custom development company. He is also the current author of the Linux Networking HOWTO, Linux PPP HOWTO, and Linux Consultants HOWTO. His most demanding project at this time is a new PostgreSQL book for O'Reilly, 'Practical PostgreSQL'

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.