Skip to main content
Engineering LibreTexts

08-C.8.3: Managing Linux Services - SysVinit

  • Page ID
    35227
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    SysVinit Runlevels compared to systemd Targets

    The following table is a comparison of the SysVinit run levels (we will talk about them in a moment) and the systemd targets that we talked about previously.

    SysV Runlevel systemd Target Notes
    0 runlevel0.target, poweroff.target Halt the system.
    1, s, single runlevel1.target, rescue.target Single user mode.
    2, 4 runlevel2.target, runlevel4.target, multi-user.target User-defined/Site-specific runlevels. By default, identical to 3.
    3 runlevel3.target, multi-user.target Multi-user, non-graphical. Users can usually login via multiple consoles or via the network.
    5 runlevel5.target, graphical.target Multi-user, graphical. Usually has all the services of runlevel 3 plus a graphical login.
    6 runlevel6.target, reboot.target Reboot
    emergency emergency.target Emergency shell

    What is Single User Mode

    An important run level is single user mode (run level 1), in which only the system administrator is using the machine and as few system services as possible, including logins, are running. Single user mode is necessary for a few administrative tasks such as running fsck on a /usr partition, since this requires that the partition be unmounted, and that can't happen unless just about all system services are killed.

    A running system can be taken to single user mode by using telinit to request run level 1. At bootup, it can be entered by giving the word single or emergency on the kernel command line: the kernel gives the command line to init as well, and init understands from that word that it shouldn't use the default run level. (The kernel command line is entered in a way that depends on how you boot the system.)

    Booting into single user mode is sometimes necessary so that one can run fsck by hand, before anything mounts or otherwise touches a broken /usr partition (any activity on a broken filesystem is likely to break it more, so fsck should be run as soon as possible).

    The bootup scripts init runs will automatically enter single user mode, if the automatic fsck at bootup fails. This is an attempt to prevent the system from using a filesystem that is so broken that fsck can't fix it automatically. Such breakage is relatively rare, and usually involves a broken hard disk or an experimental kernel release, but it's good to be prepared.

    init

    When initializing a Linux system with sysVinit, init is the last step of the kernel boot sequence. It looks for the file /etc/inittab to see if there is an entry for initdefault. It is used to determine initial run-level of the system.

    The telinit and runlevel Commands

    init is the program responsible for altering the run level which can be called using telinit command.

    For example, to change a runlevel from 3 to runlevel 5, which will actually allow the GUI to be started in multi-user mode, the telinit command can be used as:

    # using telinit to change
    # runlevel from 3 to 5
    
    pbmac@pbmac-server # telinit 5
    

    NOTE : The changing of runlevels is a task for the super user and not the normal user; that’s why it is necessary to be logged in as super user for the successful execution of the above telinit command, or you can use sudo command as:

    # using sudo to execute telinit
    pbmac@pbmac-server # sudo telinit 5
    

    The default runlevel for a system is specified in /etc/initab file which will have an entry id : 5 : initdefault if the default runlevel is set to 5 or will have an entry id : 3 : initdefault if the default runlevel is set to 3.

    Need for Changing the Runlevel

    • There can be a situation when you may find trouble logging in if you don’t remember the password or because of the corrupted /etc/passwd file (have all the user names and passwords); in this case the problem can be solved by booting into a single user mode i.e runlevel 1.
    • You can easily halt the system by changing the runlevel to 0 by using telinit 0.

    Now verify the currently running target. It should be the same as the default target. You can still use the old method, which displays the old SystemV runlevels. Note that the previous runlevel is on the left; it is N (which means None), indicating that the runlevel has not changed since the host was booted. The number 5 indicates the current target, as defined in the old SystemV terminology:

    pbmac@pbmac-server # runlevel
    N 5
    

    Note that the runlevel man page indicates that runlevels are obsolete and provides a conversion table.

    Adapted from:
    "9.5. Booting in single user mode" by Alex Weeks, Joanna Oja, Lars Wirzenius, Stephen Stafford, The Linux Documentation Project is in the Public Domain, CC0
    "Run Levels in Linux" by Dimpy Varshni, Geeks for Geeks is in the Public Domain, CC0


    08-C.8.3: Managing Linux Services - SysVinit is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?