- Linux Services¶
- What is a service?¶
- What is a daemon?¶
- What is the init system?¶
- Units in systemd¶
- .service units in systemd¶
- How to find all the systemd units in the system?¶
- Working with a particular service¶
- Enabling or disabling a service¶
- Shutdown or reboot the system using systemctl¶
- journalctl¶
- Finding the logs of a service¶
- To view only the last N entries¶
- How to Start, Stop, and Restart Services in Linux
- Basic Syntax of systemctl Command
- How To Check If a Service is Running on Linux
- How to Restart a Service
- How to Reload a Service
- How to Start a Service
- How to Stop a Service
- How to Enable the Service at Boot
- How to Disable Service at Boot
- Variations in Service Names
Linux Services¶
This is also a chapter related to the systemd tool.
What is a service?¶
A service is a process or application which is running in the background, either doing some predefined task or waiting for some event. If you remember our process chapter, we learned about systemd for the first time there. It is the first process to run in our system; it then starts all the required processes and services. To know about how the system boots up, read the bootup man page. Click here to read it online.
What is a daemon?¶
Daemon is the actual term for those long-running background processes. A service actually consists of one or more daemons.
Make sure that you don’t get confused between Daemons and Demons 🙂 Here is a gem from Internet:
What is the init system?¶
If you look at Unix/Linux history, you will find the first process which starts up, is also known as init process. This process used to start other processes by using the rc files from /etc/rc.d directory. In the modern Linux systems, systemd has replaced the init system.
Units in systemd¶
Units are a standardized way for the systemd to manage various parts of a system. There are different kinds of units, .service is for system services, .path for path based ones. There is also .socket which are socket based systemd units. There are various other types, we can learn about those later.
.service units in systemd¶
These are service units, which explains how to manage a particular service in the system. In our daily life, we generally only have to work with these unit files.
How to find all the systemd units in the system?¶
$ systemctl . long output -.mount loaded active mounted / boot.mount loaded active mounted /boot dev-hugepages.mount loaded active mounted Huge Pages File System dev-mqueue.mount loaded active mounted POSIX Message Queue File System home.mount loaded active mounted /home proc-fs-nfsd.mount loaded active mounted NFSD configuration filesystem run-user-1000-doc.mount loaded active mounted /run/user/1000/doc run-user-1000-gvfs.mount loaded active mounted /run/user/1000/gvfs run-user-1000.mount loaded active mounted /run/user/1000 run-user-42.mount loaded active mounted /run/user/42 . long output
In the output of the systemctl command, you should be able to see all the different kinds of units in the system. If you want to see only the service units, then use the following command.
Working with a particular service¶
Let us take the sshd.service as an example. The service controls the sshd daemon, which allows us to remotely login to a system using the ssh command.
To know the current status of the service, I execute the following command.
$ sudo systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:sshd(8) man:sshd_config(5) Jun 19 12:07:29 kdas-laptop sshd[19533]: Accepted password for kdas from 192.168.1.101 port 61361 ssh2 Jun 20 17:57:53 kdas-laptop sshd[30291]: Connection closed by 192.168.1.101 port 63345 [preauth] Jun 20 17:58:02 kdas-laptop sshd[30293]: Accepted password for kdas from 192.168.1.101 port 63351 ssh2 Jun 20 18:32:11 kdas-laptop sshd[31990]: Connection closed by 192.168.1.101 port 64352 [preauth] Jun 20 18:32:17 kdas-laptop sshd[32039]: Accepted password for kdas from 192.168.1.101 port 64355 ssh2 Jun 20 18:45:57 kdas-laptop sshd[32700]: Accepted password for kdas from 192.168.1.101 port 64824 ssh2 Jun 21 08:44:39 kdas-laptop sshd[15733]: Accepted password for kdas from 192.168.1.101 port 51574 ssh2 Jun 22 18:17:24 kdas-laptop systemd[1]: Stopping OpenSSH server daemon. Jun 22 18:17:24 kdas-laptop sshd[20932]: Received signal 15; terminating. Jun 22 18:17:24 kdas-laptop systemd[1]: Stopped OpenSSH server daemon.
To start the service, I’ll use the following command, and then I can use the status argument to the systemctl to check the service status once again.
$ sudo systemctl start sshd $ sudo systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled) Active: active (running) since Thu 2017-06-22 18:19:28 IST; 1s ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 3673 (sshd) Tasks: 1 (limit: 4915) CGroup: /system.slice/sshd.service └─3673 /usr/sbin/sshd -D Jun 22 18:19:28 kdas-laptop systemd[1]: Starting OpenSSH server daemon. Jun 22 18:19:28 kdas-laptop sshd[3673]: Server listening on 0.0.0.0 port 22. Jun 22 18:19:28 kdas-laptop sshd[3673]: Server listening on :: port 22. Jun 22 18:19:28 kdas-laptop systemd[1]: Started OpenSSH server daemon.
In the same way, we can use either the stop or restart arguments to the systemctl command.
Enabling or disabling a service¶
Even if you start a service, you’ll find that after you reboot the computer, the service did not start at the time of boot up. To do so, you will have to enable the service, or to stop a service from starting at boot, you will have to disable the service.
$ sudo systemctl enable sshd.service Created symlink /etc/systemd/system/multi-user.target.wants/sshd.service → /usr/lib/systemd/system/sshd.service. $ sudo systemctl disable sshd.service Removed /etc/systemd/system/multi-user.target.wants/sshd.service.
Shutdown or reboot the system using systemctl¶
We can also reboot or shutdown the system using the systemctl command.
$ sudo systemctl reboot $ sudo systemctl shutdown
journalctl¶
systemd runs the systemd-journald.service, which stores logs in the journal from the different services maintained by systemd . We use journalctl command to read these log entries from the journal. If you execute the command without any arguments, it will show you all the log entries starting from the oldest in the journal. One needs to be root to be able to use the journalctl command. Remember that systemd-journald stores all the logs in binary format, means you can not just less the files and read them.
If you want any normal user to execute journalctl command, then add them into systemd-journal group.
Finding the logs of a service¶
We can use the journalctl command to find the log of a given service. The general format is journalctl -u servicename”. Like below is the log for *sshd service.
$ sudo journalctl -u sshd -- Logs begin at Thu 2017-06-22 14:16:45 UTC, end at Fri 2017-06-23 05:21:29 UTC. -- Jun 22 14:17:39 kushal-test.novalocal systemd[1]: Starting OpenSSH server daemon. Jun 22 14:17:39 kushal-test.novalocal systemd[1]: sshd.service: PID file /var/run/sshd.pid not readable (yet?) after start: No such file or directory Jun 22 14:17:39 kushal-test.novalocal sshd[827]: Server listening on 0.0.0.0 port 22. Jun 22 14:17:39 kushal-test.novalocal sshd[827]: Server listening on :: port 22. Jun 22 14:17:39 kushal-test.novalocal systemd[1]: Started OpenSSH server daemon. Jun 22 14:22:08 kushal-test.novalocal sshd[863]: Accepted publickey for fedora from 103.249.881.17 port 56124 ssh2: RSA SHA256:lvn4rIszmfB14PBQwh4k9C Jun 22 14:29:24 kushal-test.novalocal systemd[1]: Stopping OpenSSH server daemon. Jun 22 14:29:24 kushal-test.novalocal sshd[827]: Received signal 15; terminating. Jun 22 14:29:24 kushal-test.novalocal systemd[1]: Stopped OpenSSH server daemon. Jun 22 14:29:24 kushal-test.novalocal systemd[1]: Starting OpenSSH server daemon. Jun 22 14:29:24 kushal-test.novalocal sshd[2164]: Server listening on 0.0.0.0 port 22. Jun 22 14:29:24 kushal-test.novalocal sshd[2164]: Server listening on :: port 22. Jun 22 14:29:24 kushal-test.novalocal systemd[1]: Started OpenSSH server daemon. Jun 22 14:54:26 kushal-test.novalocal sshd[13522]: Invalid user from 139.162.122.110 port 51012 Jun 22 14:54:26 kushal-test.novalocal sshd[13522]: input_userauth_request: invalid user [preauth] Jun 22 14:54:26 kushal-test.novalocal sshd[13522]: Failed none for invalid user from 139.162.122.110 port 51012 ssh2 Jun 22 14:54:26 kushal-test.novalocal sshd[13522]: Connection closed by 139.162.122.110 port 51012 [preauth] Jun 22 15:15:29 kushal-test.novalocal sshd[13541]: Did not receive identification string from 5.153.62.226 port 48677
To view only the last N entries¶
You can use the -n argument to the journalctl command to view only the last N number of entries. For example, to view the last 10 entries.
How to Start, Stop, and Restart Services in Linux
Linux provides fine-grained control over system services through systemd, using the systemctl command. Services can be turned on, turned off, restarted, reloaded, or even enabled or disabled at boot. If you are running Debian 7, CentOS 7, or Ubuntu 15.04 (or later), your system likely uses systemd.
This guide will show you how to use basic commands to start, stop, and restart services in Linux.
- Access to a user account with sudo or root privileges
- Access to a terminal/command line
- The systemctl tool, included in Linux
Basic Syntax of systemctl Command
The basic syntax for using the systemctl command is:
systemctl [command] [service_name]
Typically, you’ll need to run this as a superuser with each command starting with sudo .
How To Check If a Service is Running on Linux
To verify whether a service is active or not, run this command:
sudo systemctl status apache2
Replace apache2 with the desired service. In our case, we checked the status of Apache. The output shows that the service is active (running), as in the image below:
How to Restart a Service
To stop and restart the service in Linux, use the command:
sudo systemctl restart SERVICE_NAME
After this point, your service should be up and running again. You can verify the state with the status command.
sudo systemctl restart apache2
How to Reload a Service
To force the service to reload its configuration files, type in the following command in the terminal:
sudo systemctl reload SERVICE_NAME
After reloading, the service is going to be up and running. Check its state with the status command to confirm.
In our example, we reloaded Apache using:
sudo systemctl reload apache2
How to Start a Service
To start a service in Linux manually, type in the following in the terminal:
sudo systemctl start SERVICE_NAME
For instance, the command to start the Apache service is:
sudo systemctl start apache2
How to Stop a Service
To stop an active service in Linux, use the following command:
sudo systemctl stop SERVICE_NAME
If the service you want to stop is Apache, the command is:
sudo systemctl stop apache2
Check whether the service stopped running with the status command. The output should show the service is inactive (dead).
How to Enable the Service at Boot
To configure a service to start when the system boots, use the command:
sudo systemctl enable SERVICE_NAME
To enable Apache upon booting the system, run the command:
sudo systemctl enable apache2
How to Disable Service at Boot
You can prevent the service from starting at boot with the command:
sudo systemctl disable SERVICE_NAME
sudo systemctl disable apache2
Variations in Service Names
If you work within the same Linux environment, you will learn the names of the services you commonly use.
For example, if you are building a website, you will most likely use systemctl restart apache2 frequently, as you refresh configuration changes to your server.
However, when you move between different Linux variants, it is helpful to know that the same service may have different names in different distributions.
For example, in Ubuntu and other Debian based distributions, the Apache service is named apache2. In CentOS 7 and other RedHat distros, the Apache service is called httpd or httpd.service.
In most modern Linux operating systems, managing a service is quite simple using the commands presented here.
Make sure to know the name of the service for the operating system you are using, and the commands in this article should always work.