1

I am trying to understand Run Levels in Unix, I found Runlevel 6 is state of reboot. So I tried to switch to run level 6 using the command telinit 6 and it worked same as reboot on my computer. I am using Kali Linux, so sudo is not required.

Is changing/switching runelevels just commands to invoke some process? When we are booting up or restarting or powering off, are we just changing run levels?

bareMetal
  • 151
  • 1
  • 9

2 Answers2

1

There is a init system in Linux called SysVinit which is modeled after UNIX System V's init system. Some versions of Linux actually still use this system, maintain compatibility with it such as Systemd's SysVcompat or have replaced it in part (no sysvcompat but they still use sysv scripts like Debian) or replaced it all together such as "pure" Systemd systems.

It is SysV that essentially implements the concept of run levels. Scripts are placed in /etc/rc{runlevel}.d/ where runlevel is the runlevel. SysV also defines the default ordering behavior of the runlevels including which one to go to by default. Each script in each runlevel is run with its startup or shutdown functions depending on if that runlevel is going up or going down. So in a sysvinit system yes when you boot up its first running /sbin/init and that program will subsequently run the startup functions of every script in each runlevel. I should also point out you can have more scripts installed than ones linked in /etc/rc* and thats how you enable and disable the scripts and thus the services they control.

Systems that don't use sysv don't have to do this at all like systemd. To put it simply systemd has units that define their dependancies such as other services, mount points, timers or targets like "network.target". Instead of going into anymore detail about systemd I'll just say that systemd maintains full compatability with sysv scripts but not nessarily its run levels. Plus init can run first or systemd can run and then run all of sysv's scripts. Or your distro could have replaced all your sysv scripts with systemd units. Most distros will have compatability scripts installed so that things like reboot or telinit work through systemd.

jdwolf
  • 5,017
1

In init based OS /sbin/telinit is linked to /sbin/init and in systemed based OS it's linked to /sbin/systemed. So, it's a way to communicate with init or systemed or similar software.

The result of telinit 6 and reboot is same, but the they realize a different thought.

Quoting from http://www.tutorialspoint.com/unix_commands/init.htm

A runlevel is a software configuration of the system which allows only a selected group of processes to exist.

So, changing runlevel is not equivalent to rebooting or power off.

In init based OS runlevel is permanently mentained by /etc/inittab. While starting the system init reads the file to decide which groups of process to start. A runlevel 6 says don't start any process and reboot.

A change in /etc/inittab is permanent. If the file is modified init changes the run-level on reboot and sticks to it until further modification of the file.

Effect of telinit is one time and without reboot. After reboot init again read from /etc/inittab and start run-level accordingly.

Now, run-level 6 says init to kill all child process and reboot. With telinit 6 init kill all child process and reboot. On, issuing reboot command, init do the same. But, it doesn't mean changing run-level is equivalent to rebooting. Because if you change the run-level of your system to 6 permanently using /etc/inittab it'll will keep rebooting.

If you use telinit 6 to reboot your system rather reboot command it'll not make any difference.

Abhik Bose
  • 2,118