Skip to main content
Engineering LibreTexts

08-C.8.4: Managing Linux Services - inittab & init.d

  • Page ID
    35229
  • The /etc/inittab File

    When init starts up, it reads the /etc/inittab configuration file. While the system is running, it will re-read it, if sent the HUP signal (kill -HUP 1); this feature makes it unnecessary to boot the system in order to make changes to the init configuration take effect.

    The /etc/inittab file is a bit complicated. We'll start with the simple case of configuring getty lines. Lines in /etc/inittab consist of four colon-delimited fields:

    id:runlevels:action:process

    The fields are described below. In addition, /etc/inittab can contain empty lines, and lines that begin with a number sign (`#'); these are both ignored.

    id - This identifies the line in the file. For getty lines, it specifies the terminal it runs on (the characters after /dev/tty in the device file name). For other lines, it doesn't matter (except for length restrictions), but it should be unique.

    runlevels - The run levels the line should be considered for. The run levels are given as single digits, without delimiters. (Run levels are described in the next section.)

    action - What action should be taken by the line, e.g., respawn to run the command in the next field again, when it exits, or once to run it just once.

    process - The Linux command to run.

    What is init.d?

    All these services work on several scripts and these scripts are stored in /etc/init.d. init is a daemon which is the first process of the Linux system. Then other processes, services, and daemons, are started by init. So init.d is a configuration database for the init process.

    There are numerous files in the /etc/init.d directory. Each of these files represent a service, that is a daemon, that may or may not be running on the system. The files are simply Linux shell scripts that can be run either by init, or manually from the command line. The scripts all have some portions that are the same. If the service is to be started the "start" function is called, if it is to be stopped, then the "stop" function is called. So, each script has several functions to respond appropriately to the various sub-commands of service: start, stop, status, etc.

    The /etc/init.d Directory

    In Linux there are several services that can be started and stopped manually in the system. Some of these services are ssh, HTTP, tor, apache, etc. To start and run these services we used to simply type service "service name" start/stop/status/restart.

    Example:

    service ssh start

    And to check if this service is running we type the command:

    service ssh status

    In this simple manner, we are using service management in Linux but what actually happens and how it actually works is in the background.

    Some documentation sources suggest that administrators run the scripts directly as in:

    /etc/init.d/ssh start

    However, it is better to use the service command, since it knows where the scripts are located, and it is less typing.

    The /etc/rc.local Directory

    There is also a directory /etc/rc.local. It starts to get confusing as to what are all these directories and files for.

    • init.d is the directory that stores services control scripts, which control the starting and stopping of services such as httpd, sshd, etc.
    • rc.local is a service that allows running of arbitrary scripts as part of the system startup process.

    So, if you have local scripts that you as the administrator want to run when the system is started, you can place them in the /etc/rc.local directory and they get run automatically.

    The service Command

    The service command is used to run a System V init script. Usually all system V init scripts are stored in /etc/init.d directory and service commands can be used to start, stop, and restart the daemons and other services under Linux. All scripts in /etc/init.d accept and support at least the start, stop, and restart commands.

    Syntax:

    service SCRIPT COMMAND [ OPTIONS ]
    

    The SCRIPT is the name of one the scripts that exists in /etc/init.d. The allowable COMMAND parameters depends on the SCRIPT, but the scripts should all at least have a "start" and a "stop" capability. The COMMAND and OPTION parameters are passed directly to the script itself, where they are interpreted. In the following table each row shows the service command, "name," which is the service that is being queried (such as httpd orsshd), and then the sub-command.

    Sub-commands Meaning
    service name start Starts a service
    service name stop Stops a service
    service name restart Restarts a service
    service name reload Reloads a configuration
    service name status Checks whether a service is running
    service –status- all Displays the status of all services

    The chkconfig Command

    The chkconfig command is quite similar to the service command, and is used to list all available services and view or update their run level settings. It lists current startup information of services or any particular service, updating runlevel settings of service and adding or removing service from management.

    Syntax:

    chkconfig [ OPTIONS ] [ SERVICE ]  sub-command

    In the following table the only OPTION used is --list, though there are several other options - consult your chkcofig man page for additional information. In these examples, replace SERVICE with the appropriate service name.

    Sub-command Meaning
    chkconfig SERVICE on Enables a service
    chkconfig SERVICE off Disables a service
    chkconfig –list SERVICE Checks to see if a service is enabled
    chkconfig –list List all services and check if they are enabled
    chkconfig SERVICE reset Restore the service’s startup priority and other information to the recommended settings
    chkconfig –level 5 SERVICE on Enable the specified service at the specified runlevel (in this case runlevel 5)
    chkconfig –level 5 SERVICE off Disable the specified service at the specified runlevel (in this case runlevel 5)

    Adapted from:
    "What is init.d in Linux Service Management?" by Madhusudan_Soni, Geeks for Geeks is licensed under CC BY-SA 4.0
    "Service command" by Multiple ContributorsnixCraft is licensed under CC BY-NC-SA 3.0

    • Was this article helpful?