Runlevels introduction
From NewbieDOC
- Will Trillich
- will@dontUthink.com (author)
- Tom Huckstep
- (publisher)
- Romain Lerallut
- rom1 AT users DOT sourceforge DOT net (publisher)
- Chris Lale
- chrislale AT users DOT berlios DOT de (publisher)
Go to NewbieDOC index
Revision History
Revision v1 | Revised by Will Trillich | ||||||||
The HTML version. http://newbiedoc.sourceforge.net/system/runlevels-intro.html.en | |||||||||
| |||||||||
Revision v2.0 | 18 February 2001 | Revised by Tom Huckstep | |||||||
The SGML version. | |||||||||
| |||||||||
Revision v2.1 | 5 January 2002 | Revised by Romain Lerallut | |||||||
Fixed update-rc.d example. | |||||||||
| |||||||||
Revision v2.2 | 24 January 2006 | Revised by Chris Lale | |||||||
Adapted for NewbieDOC wiki by translating source into wikitext. Updated the abstract. Made a few minor textual amendments. Removed some broken links. | |||||||||
| |||||||||
Revision v2.3 | 26th November 2007 | Revised by Chris Lale | |||||||
Reformatted for newbiedoc package. | |||||||||
| |||||||||
Revision v2.4 | 6th December 2007 | Revised by Chris Lale | |||||||
Corrected runlevel for reboot - telinit 6. (Thanks to anonymous wiki user.) | |||||||||
Runlevels determine the behaviour of the system once it has booted.
1 AssumptionsI'm writing this in January of 2001 ("Open the pod bay doors, Hal") as it applies to my Debian "potato" release (that is, the uname -a command tells me I'm running Linux kernel 2.2.17). Your mileage may vary -- chances are SOMETHING in your system is different from mine, right? Debian "Potato" Release Kernel 2.2.17 Other Linux distributions MAY find this information useful as well, but it's tailored for the Debian "Potato" crowd. By the way -- for you Mac and Windows folks out there -- my Debian system, with exim and mutt for email, and web and ftp servers, has been up without any hiccups for 108 days as of today. (My Mac has stretches when it behaves delightfully for weeks, and then it freezes up in spurts, five times a day for the next ten days... Windows folks know the feeling, too.) That "fits-and-starts" syndrome just doesn't happen with Linux. 2 IntroductionSure, you can Ever wonder: So what creates the first process? (If not, you should.) What starts all these processes? Where exactly IS the god of the machine? If you haven't tinkered with $ ps axf And to learn about $ man ps What Using $ ps afx PID TTY STAT TIME COMMAND 5790 ? S 1:41 /usr/sbin/xinetd 25445 ? S 0:00 \_ in.telnetd: wdt 25446 pts/0 S 0:00 \_ -tcsh 25878 pts/0 T 0:00 \_ vi runlevels-intro.wml 26045 pts/0 R 0:00 \_ ps afx I have edited out lots of extraneous lines to reduce distractions; you'll always see a heck of a lot more than just five lines, in your The above listing confirmed our initial suspicions about logging in creating the shell, which creates all the other command processes we execute: you can see that Below, I run the ps program again, but ask for a LONG listing via $ ps afxl F UID PID PPID STAT TTY TIME COMMAND 100 0 1 0 S ? 0:17 init [2] 140 0 5790 1 S ? 1:41 /usr/sbin/xinetd 100 101 25445 5790 S ? 0:00 \_ in.telnetd: wdt 100 1000 25446 25445 S pts/0 0:00 \_ -tcsh 000 1000 25878 25446 T pts/0 0:01 \_ vi runlevels.wml 000 1000 26072 25446 R pts/0 0:00 \_ ps axfl (Again, I've whittled down on the rows, and this time, the columns, too. Your display will have lots more info than this, guaranteed.) Notice that the process-ID (PID) of Now -- notice how the PPID (parent) of Think of 3 Init is the granddaddy of them allAll processes are spawned by Great. So how the heck does When id:2:initdefault: which tells init that until another runlevel is specified, wake up using 'runlevel 2' by default. But even before the runlevel-specific stuff is executed, the 'system-wide gotta-have' stuff is specified in si::sysinit:/etc/init.d/rcS which runs all the initial stuff, like loading kernel modules, mounting partitions, hardware clock settings, yada yada... What the Whenever you boot into single-user mode (also called 'runlevel S') init runs only rcS stuff and doesn't continue any further – enabling you to log in as root and twiddle with your settings, before starting a services-oriented runlevel. (By services-oriented, I mean something like "on runlevel A we've got X and 3.1 But how about the feature-sets for a "user runlevel"?For the runlevel-specific stuff (and this is what determines the difference between runlevel 3 and 5 and 2 and ...) further down in l2:2:wait:/etc/init.d/rc 2 which says that for runlevel 2, to run the Anyhow, what the /etc/rc[runlevel-digit].d/* /etc/rc2.d/* for scripts to run. A very nice, modular structure. And all those 3.2 What makes init start up a login sequence?Even further down in 1:2345:respawn:/sbin/getty 38400 tty1 2:23:respawn:/sbin/getty 38400 tty2 3:23:respawn:/sbin/getty 38400 tty3 4:23:respawn:/sbin/getty 38400 tty4 5:23:respawn:/sbin/getty 38400 tty5 6:23:respawn:/sbin/getty 38400 tty6 According to this, for runlevels 4 and 5, only tty1 will be active; for 2 and 3 tty[1-6] will all be active. (It's actually more sensible to say that tty1 is gonna be active for runlevels 2-5, and tty[2-6] will be active only for runlevels 2-3.) And when the connection goes down (either you log out in a nice, neighborly fashion, or you get rudely disconnected by a power failure or because your neice chewed through your modem cable), 3.3 What runlevel am I in?How do you find out which runlevel you're in right now? This one's easy. Try this: $ /etc/runlevel S 2 It displays both the previous runlevel (which may be S after a successful system startup, or perhaps N, signifying that there was no previous runlevel) and the current runlevel. (My system is at runlevel 2 after last being at the single-user [S] runlevel.) Nothing to it. 3.4 Changing to a different runlevelThis is not something to do lightly, especially if you have several users on your system. They'll track you down eventually (and have been known to use weapons) and you'll regret your careless act unless you have a good reason... If it's really a good idea to do so, here's the recommended way to change from whatever runlevel you're using, to another runlevel: # telinit 4 # telinit S # telinit 2 Very simple. Just 3.5 How do the runlevels differ?Sorry, I can't answer that... But you can. Remember the l0:0:wait:/etc/init.d/rc 0 1:1:wait:/etc/init.d/rc 1 l2:2:wait:/etc/init.d/rc 2 l3:3:wait:/etc/init.d/rc 3 l4:4:wait:/etc/init.d/rc 4 l5:5:wait:/etc/init.d/rc 5 l6:6:wait:/etc/init.d/rc 6 They all run the same script -- So we hafta check into that script to see what the argument does. If you look at the # Is there an rc directory for this new runlevel? if [ -d /etc/rc$runlevel.d ] ... for i in /etc/rc$runlevel.d/K[0-9][0-9]* ... for i in /etc/rc$runlevel.d/S* Can you see what that does? If you enter runlevel 3 (perhaps via When entering a runlevel, you may need to "turn off" features that might have been turned on by another runlevel. Then, you turn on the features for the new runlevel. So first, /etc/rc3.d/K20postgresql stop Which stops the pgsql server for runlevel 3 After it's all done running available "kill" scripts it then runs the "start" scripts (which have names staring with "S") in much the same way: /etc/rc3.d/S60sshd start Which launches the secure shell daemon for runlevel 3. So now you should see why YOU can answer the question better than I can: only YOU can determine the difference between your runlevels ... by looking at your And note that the scripts will be run "in sequential order" meaning that S10* would run before S60* and so forth. This is how you can ensure that the load order works properly. For example, you may need to establish a network connection with a fileserver before launching a remote-log daemon. BUT! There are certain preset runlevels that have important meanings:
4 Changing what a runlevel doesThere are Debian ways of munging the directory of scripts that the 4.1 Create a script to start or stop a featureIn order to affect a runlevel, you need to create a script that does some work. # cd /etc/init.d # cp skeleton my_sample_script That skeleton is a nice way to start you off on the right foot. Once you get your 4.2 Determine where it belongs in the START or STOP sequenceEven when your script works properly, you'll need to know where it belongs in the startup sequence for example, to check out runlevel 4: # cd /etc/rc4.d $ ls -F S10sysklogd@ S20gpm@ S20postgresql@ S50wu-ftpd@ S99rmnologin@ S12kerneld@ S20inetd@ S20ssh@ S60sshd@ S19bind@ S20logoutd@ S20xinetd@ S89atd@ S20cipe@ S20makedev@ S22ntpdate@ S89cron@ S20exim@ S20mysql@ S23ntp@ S91apache@ This sample, taken from my system, shows there are zero KILL scripts, and 21 START scripts. If you wanted your new script to be STARTed after sshd (which is number 60) and before atd (number 89) then using any number near S70 for startup would do the trick. Let's say you also want to run the KILL portion (maybe to refresh or disable something, it's up to you) when entering runlevel 4, as well. In my example above, I have NO kill scripts, so there's no established sequence to fit into so any number will do, but let's use 50. 4.3 Install the links that call the scriptHere's how you set up your script to START as sequence 70 and STOP as sequence 50 for runlevel 4: # update-rc.d my_sample_script start 70 4 . stop 50 4 . That'll create links in You could also add your start/stop script to several runlevels at once. For example, add it to runlevels 2 through 5 like this: # update-rc.d my_sample_script start 70 2 3 4 5 . stop 50 2 3 4 5 . Easy as pie! 4.4 Removing a feature from a runlevelWhen you decide to REMOVE your script (or any other service) from a runlevel, do like so: # cd /etc/init.d # mv my_sample_script my_sample_script.DISABLED # update-rc.d my_sample_script remove If you don't remove (or rename, if you're obsessive like I am and deplore actually deleting anything, ever) the actual script first, you'll need to supply other arguments to Of course you can always tangle things up manually if you like, by editing 5 Need to know more?Don't stop here! Learn more by trying these: $ man init $ man inittab $ man ps $ man update-rc.d http://packages.debian.org/procps 6 Appendix A: LicenceCopyright (c) 2001 Will Trillich. will@dontUthink.com. Copyright (c) 2001 Tom Huckstep. Copyright (c) 2001 Romain Lerallut. rom1 AT users DOT sourceforge DOT net Copyright (c) 2006-2007 Chris Lale. chrislale AT users DOT berlios DOT de. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license can be found at http://www.fsf.org/copyleft/fdl.html. Go to NewbieDOC index |