Question sent in by Nathan from Quebec in reply to “Restarting Services in Red Hat 7“.

Q: Thanks for you help with understanding how to start and stop services. Now how to I get services to start on boot in Red Hat 7 or CentOS 7?

A: Sorry Nathan, I should have touched on this when I answered your last question about starting services.  You can use systemctl to “enable” or “disable” services.  It may not be so obvious, but enable means the service will start on boot, and disabled means it will not.  This is done separately from starting or stopping a service.  So a started service can be disabled without stopping it, and a stopped service can be enabled without starting it, although it will start on the next boot.

To set a service to start on boot you can use systemctl like so:

systemctl enable sshd.service 

This will create a symbolic link which activates the service or “unit” as it is called in systemd.

Likewise, to make sure a service does not start on boot (or disable), you can use systemctl like so:

systemctl disable sshd.service

You can check if a service is enabled or disable by using the “status” option to systemctl like this:

 systemctl status sshd.service 

Here is an example output of the status:

 
sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
Active: active (running) since Tue 2014-07-22 20:43:21 EDT; 17min ago
Main PID: 4322 (sshd)
CGroup: /system.slice/sshd.service
└─4322 /usr/sbin/sshd -D

Jul 22 20:43:21 bighat systemd[1]: Started OpenSSH server daemon.
Jul 22 20:43:21 bighat [4322]: Server listening on 0.0.0.0 port 22.
Jul 22 20:43:21 bighat [4322]: Server listening on :: port 22.

The first line shows you the service you are querying the status of:

 sshd.service - OpenSSH server daemon 

The second line shows you if it is enabled (will start on boot) or disabled (won’t start on boot):

 Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled) 

The third line shows you if the daemon or service is currently running (and info about when it was started):

 Active: active (running) since Tue 2014-07-22 20:43:21 EDT; 17min ago 

The fourth, fifth and sixth lines show you process ID and related information:

Main PID: 4322 (sshd)
CGroup: /system.slice/sshd.service
└─4322 /usr/sbin/sshd -D

And the following lines are pulled from syslog as yet more information:

Jul 22 20:43:21 bighat systemd[1]: Started OpenSSH server daemon.
Jul 22 20:43:21 bighat [4322]: Server listening on 0.0.0.0 port 22.
Jul 22 20:43:21 bighat [4322]: Server listening on :: port 22.

NOTE: You will have to be root to perform all of the commands listed on this page.  If you prefer you can prepend sudo in front of the commands to use sudo instead of directly accessing the root account. This works in Fedora 18 and above, Red Hat 7 and CentOS 7.