If you are coming from a Windows environment, the way that GNU/Linux starts up may seem very confusing. Just like Windows, Linux has several places where start-up scripts and actions can be stored. The methods used in Linux though, are more structured and organised, and once you get the hang of it, it’s real easy to understand as well.
Chances are, you’re using an external bootloader such as LILO or GRUB to boot your Linux distribution. Settings such as your default runlevel, and other boot options, are sometimes specified in your boot loader. The default runlevel is specified in /etc/inittab.
The bootloader can be installed to the master boot record of a disk, or to a specific partition on your disk. This means that you can use one boot loader as your main boot loader, and link to other boot loaders to your disk. Dual booting is achived this way, since most operating systems are capable of writing their bootloaders to the partition it is installed on.
The bootloader will load your kernel (and an initial ramdisk, if needed), and specify to the kernel which partition to use as the root partition. The kernel will then initiate a program called “Init”, which will continue the boot process.
Init, the mother of all processes
In GNU/Linux, every program you run acts as a process. Each process may have sub-processes, and parent processes. Each process has a unique I.D. by which it can be identified. Init is usually the first process started by the Linux kernel, and will have a process ID of “1”. Each process started by Init, will be a child process of init. Killing Init will kill all child processes, and will result in a kernel panic.
Init will then take a peek at /etc/inittab, to find out how it should boot your system. If you specified your own options at boot time, it will override the settings in inittab.
If you remember the MS-DOS days, you’ll remember that DOS started up with one script, called AUTOEXEC.BAT. In GNU/Linux, there are several different paths that you can choose when booting up. These are called runlevels, which also control the shut-down and reboot sequences.
You may have up to 9 runlevels on your system. Most distribution has 6 runlevels configured in the inittab file.
Runlevel 0 is used to shut down the system.
Runlevel 1 boots up in single user mode, with no networking
Runlevel 2 is the default for Debian based systems, and gives full multi-user-system and the X-window system (if configured). On some systems, runlevel 2 is configured to be single user mode with networking
Runlevel 3 is often used on many distributions for booting to a multi-user console without the X-window system
Runlevel 4 is usually reserved for custom user runlevels
Runlevel 5 is often used on many distributions for full multi-user-system with the X-window system.
You can assess the current runlevel by typing “runlevel” inside a terminal window or from a console:
[email protected] ~ $ runlevel
The first character shows your previous runlevel. The second character shows your current runlevel. The N suggests that you weren’t previously in another runlevel. The “2” means that you are in runlevel 2.
You can change runleve by using the “init” command. You need to be root or run with sudo to change runlevel.
[email protected] ~ # init 0
System is going down for reboot NOW!
Runlevel 0 is used for shutting down your system. Changing to this runlevel will have the same effect as running “halt”. Similarly, typing “init 6” will have the same effect as “reboot”.
The default runlevel your system boots in is specified in the /etc/inittab file:
# The default runlevel.
In this example, the default runlevel is runlevel 2.
On most GNU/Linux systems, the startup scripts are stored in “/etc/init.d”. On some systems, this might be at “/etc/rc.d/init.d”, or just “/etc/rc.d”.
Many scripts need to run regardless of the runlevel you choose. These scripts will all be run from “/etc/rcS.d”, and is specified in the inittab by:
Some files from /etc/rcS.d:
You may notice that these aren’t physical scripts stored in this directory. Instead, they are symbolic links (shortcuts) to the real files stored in “/etc/init.d”. The “S” at the beginning of the filename is short for “start”, and means that this service should be running in this runlevel. In the shutdown runlevel, most of the filenames will start with a “K”, which is short for kill, which means that services should be stopped in this runlevel. The numbers next to the “S” is the order in which the scripts should run. ntpdate, for instance, is a program that syncs your computers system date over the Internet. It won’t work unless your networking is up, so it needs to run after your networking is activated. You can also run several scripts in parrallel, by making the scripts start at the same time by giving them the same numbers.
If you’d like to add a script to a runlevel, for instance, let’s say I want my webserver to start when I boot into runlevel 2, I can use “ln -s” to create a symbolic link to “/etc/rc2.d”.
ln -s /etc/init.d/apache2 /etc/rc2.d/S91apache2
In the example above, apache will start after all other processes in runlevel 2.
If I would want apache2 to stop when switching to runlevel 3, I can also do it by creating a symbolic link:
ln -s /etc/init.d/apache2 /etc/rc3.d/K05apache2
When the symlink starts with an “S”, the script is called with the parameter “start”, and if it starts with a “K”, the parameter is “stop”. The K05apache2 above will have the same effect in runlevel 3 as:
For those who do not want to do it by hand:
YaST (on SUSE Linux)
sysv-rc-conf (text based tick-box editor)
chkconfig (Red Had based systems)
ksysv (Graphical editor)
Other start-up scripts
Most shells also have scripts that start up when you log in. These scripts will typically set some shell settings, such as they way your prompt looks, start programs, and other useful tools, such as programmable bash auto-completion.
In Bash, the most commonly used shells in Linux distributions, this file is called “.bashrc”, and can be found in your home directory. Edit this file to manipulate what happens when you start Bash.
You can also edit “/etc/bash.bashrc” to edit the Bash startup settings for all users. “/etc/profile” can be used for initial settings for all Bourne-like shells (bash, ksh, ash, etc)
Just like Bash, there are certain scripts that are run each time you log into an X session. These scripts are stored in “/etc/X11/Xsession.d/”. Scripts are executed in numerical order from 1st to last.
PAM is an acronym for Pluggable Authentication Module. Pam manages four types of verification: authentication, account, session and password. A pam aware application will have a small stub configuration in /etc/pam.d. This configuration can specify session modules which may perform certain functions whenever you start a new session.
Logging in usually starts a new session. Common tasks performed at login time is to check if you have mail and print a message informing you if you do (pam_mail.so).