What's the connection between "/etc/init.d" and "/etc/rcX.d" directories in Linux?
Let's forget init.d
or rcx.d
and keep things very simple. Imagine you were programming a program whose sole responsibility is to run or kill other scripts one by one.
However your next problem is to make sure they run in order. How would you perform that?
And lets imagine this program looked inside a scripts
folder for running the scripts. To order the priority of scripts you would name them in lets say numerical order. This order is what dictates the relation between init.d
and rc
In other words init.d
contains the scripts to run and the rcX.d
contains their order to run.
The X
value in rcX.d
is the run level. This could be loosely translated to the OS current state.
If you dig inside the rcX.d
scripts you will find this formatting:
Xxxabcd
X
is replaced withK
orS
, which stands for whether the script should bekilled
orstarted
in the current run levelxx
is the order numberabcd
is the script name (the name is irrelevant however where it points is the script this will run)
There are several different init systems for Linux. The main ones are SysVinit (the traditional one), Upstart (Ubuntu's replacement), and SystemD (pushed by Fedora and Gnome). The directories /etc/init.d
and /etc/rc?.d
are used by SysVinit. The book may be mentioning them with regards to Ubuntu because the information is a bit dated (Ubuntu used to use SysVinit like everyone else) or because those directories still exist for compatibility.
/etc/init.d
contains a bunch of scripts, each containing instructions to start and stop a service. Some of these services need to be started at boot time; others need to be started in multiuser mode but not in single-user maintenance mode; and it's possible to define different modes with different sets of desired services. SysVinit manages this through runlevels. The directory /etc/rc$N.d
contain the scripts to run when entering runlevel N (/etc/rc$N.d/S*
) and the scripts to run when leaving runlevel N (/etc/rc$N.d/K*
). Because many runlevels have scripts in common, rather than store a copy of the scripts for each runlevel, the scripts are all stored in a single location /etc/init.d
, and the runlevel-specific directories /etc/rc?.d
contain symbolic links. Furthermore the names of the symbolic links indicate whether the service is to be started (S*
) or stopped (killed, K*
) in that runlevel, and a numeric prefix is used to control the order in which the scripts are executed.
The script responsible for traversing /etc/rc$N.d
is /etc/init.d/rc
(on pre-Upstart Ubuntu and on Debian; locations may vary on other Linux distributions).
/etc/init.d
is the directory where the init scripts belong.
The etc/rcX.d
is where links controlling which services are killed or started when entering runlevel X
. Files in rcX.d
starting with K are executed with the parameter stop
, and files starting with S
are executed with the parameter start
. It is typical to sequence startup and stop order using a two digit number after the K
or S
. To ensure proper start and stop ordering is it common for the two sequences to add up to 100.
Programs can be disabled in a run level by removing the links or changing the case of the K
or S
to k
or s
.
EDIT: Administrators typically run the scripts from /etc/init.d
which may be a symbolic link to another directory depending on distribution. (Different distributions have different standards.)
The rcX.d
are used by the initialization code to change run levels.