How to use the Linux service command

The Linux service command lets you run System V init scripts. Although many distributions for Linux now use other init systems, Linux service will still work for most cases.

What is the Linux service command?

Older Linux distributions in particular still use the init system V designed to start and stop services properly. Services are programs that run automatically in the background when you boot your computer. This usually concerns system services responsible for system startup and the hardware, and post-installed server services. These services usually work without communication with users and don’t have a graphical user interface. To execute a System V init script independently the Linux service command is used.

How does Linux service work?

To run the service command, you’ll need root privileges or should prepend the Linux sudo command. When you have the necessary privileges, you can use the command to start, stop, reboot, or get the status from individual scripts, among other things. The System V init scripts are located in the folder /etc/init.d/SCRIPT on Linux. Many distributions like Debian have switched to systemd. Linux service is still compatible with these. However, a better alternative for them is the Linux systemctl command. Basically, before you terminate or start a process, ensure it won’t negatively affect your operating system.

What is the syntax of the service command?

The basic syntax of Linux service is:

$ service [Script] [Action] [Option]

If you don’t specify the ‘[Action]’ parameter, you’ll see all allowed actions in the command line.

What options and actions does Linux service have?

Strictly speaking, Linux service only has the default options ‘-h’ or ‘–help’, ‘-v’ or ‘–version’, and ‘-status-all’. However, you can specify the command with various actions. These are the most important ones:

  • start: Starts the script.
  • stop: Stops the script.
  • status: Shows the current status of the script.
  • restart: Ensures that the script is restarted.

Examples of using the Linux service command

To help you understand how the service command is used in practice, we’ll conclude with some examples of its main use.

$ sudo service sshd start

This starts the sshd script.

$ sudo service sshd stop

This forces the sshd script to stop.

$ sudo service sshd restart

This command restarts the corresponding script.

$ sudo service sshd status

This command is used to query the status of the script.