Skip to content

Linux Foundations / Understand the system underneath ยท Step 32 of 38

Lesson: System V ServiceSwitch
โ† Linux Foundations

System administration

System V Service

Start, stop and enable services on a System V system with scripts and helper tools.

Under System V, a service is controlled by a shell script in /etc/init.d. The script accepts an action as its argument, usually start, stop, restart and often status.

Running a script

You can call the script directly:

$ sudo /etc/init.d/ssh status
sshd is running.

The service command does the same job with less typing, and runs the script in a predictable, mostly clean environment:

$ sudo service ssh restart

Service names differ between distributions. The SSH server is ssh on Debian and Ubuntu but sshd on Red Hat style systems. The wording of status output also varies.

To see many services at once:

$ service --status-all
 [ + ]  cron
 [ - ]  rsync
 [ + ]  ssh

This format comes from Debian-based systems. A + means running and - means not running, as reported by each script. A ? means the state could not be determined.

Start now versus start at boot

Starting a service affects only the running system. After a reboot, what starts is decided by the links in the rc directories.

On Red Hat style systems, manage those links with chkconfig:

$ sudo chkconfig sshd on
$ chkconfig --list sshd
sshd  0:off 1:off 2:on 3:on 4:on 5:on 6:off

On Debian-based systems, use update-rc.d:

$ sudo update-rc.d ssh defaults
$ sudo update-rc.d ssh disable

defaults creates the standard links. disable changes the start links into stop links, so the service no longer starts at boot.

What if the system uses systemd?

Many current distributions still accept service ssh restart, but pass the request to systemctl behind the scenes. That is convenient for old habits, but on those systems you should learn systemctl as the primary tool.

Reading a script

Init scripts are plain text. Open one with less /etc/init.d/cron and look for a case statement that handles each action. The comment block between ### BEGIN INIT INFO and ### END INIT INFO, used by many distributions, records dependencies and default runlevels.

Try it

On a test system, run service --status-all or chkconfig --list. Choose a harmless service such as cron, check its status, and read its script. Do not stop the SSH server on a machine you reach through SSH.

Check your understanding

System V Service

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.