Search This Blog

Monday, January 4, 2016

RHEL7 - Controlling Services and Daemons

Listing unit files with systemctl

1.      Query the state of all unit to verify a system startup.
#systemctl

[root@server1 /]# systemctl
UNIT                                                                                          LOAD   ACTIVE SUB       DESCRIPTION
proc-sys-fs-binfmt_misc.automount                                                             loaded active waiting   Arbitrary Executable File Formats File System Automount Point
sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda1.device           loaded active plugged   VMware_Virtual_S
sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda2.device           loaded active plugged   VMware_Virtual_S
sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda.device                loaded active plugged   VMware_Virtual_S
sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-eno16777736.device                       loaded active plugged   PRO/1000 MT Single Port Adapter
sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device                           loaded active plugged   AudioPCI 64V/128 / Creative Sound Blaster CT4810

2.      Query the state of only the service units.
# systemctl –type=service

[root@server1 /]# systemctl --type=service
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
abrt-ccpp.service                  loaded active exited  Install ABRT coredump hook
abrt-oops.service                  loaded active running ABRT kernel log watcher
abrt-xorg.service                  loaded active running ABRT Xorg log watcher
abrtd.service                      loaded active running ABRT Automated Bug Reporting Tool
accounts-daemon.service            loaded active running Accounts Service
alsa-state.service                 loaded active running Manage Sound Card State (restore and store)
atd.service                        loaded active running Job spooling tools
auditd.service                     loaded active running Security Auditing Service

3.      Investigate any units which are in a failed or maintenance state. Optionally, add the –l option to show the full output.
# systemctl status rngd.service –l

[root@server1 /]# systemctl status rngd.service -l
rngd.service - Hardware RNG Entropy Gatherer Daemon
   Loaded: loaded (/usr/lib/systemd/system/rngd.service; enabled)
   Active: active (running) since Fri 2016-01-01 10:22:18 CET; 2 days ago
 Main PID: 871 (rngd)
   CGroup: /system.slice/rngd.service
           └─871 /sbin/rngd -f

Jan 01 10:22:18 server1.example.com systemd[1]: Started Hardware RNG Entropy Gatherer Daemon.
Jan 01 10:22:19 server1.example.com rngd[871]: Unable to open file: /dev/tpm0

4.      The status argument may also be used to determine if a particular unit is active and show if the unit is enabled to start at boot time. Alternate commands can also easily show the active and enabled states:
# systemctl is-active sshd
# systemctl is-enabled sshd

[root@server1 /]# systemctl is-active sshd
active

[root@server1 /]# systemctl is-enabled sshd
enabled

5.      List the active state of all loaded units. Optionally, limit the type of unit. The –all option will add inactive units.
# sytsemctl list-units –type=service
# systemctl list-units –type=service –all

[root@server1 /]# systemctl list-units --type=service
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
abrt-ccpp.service                  loaded active exited  Install ABRT coredump hook
abrt-oops.service                  loaded active running ABRT kernel log watcher
abrt-xorg.service                  loaded active running ABRT Xorg log watcher
abrtd.service                      loaded active running ABRT Automated Bug Reporting Tool
accounts-daemon.service            loaded active running Accounts Service
alsa-state.service                 loaded active running Manage Sound Card State (restore and store)
atd.service                        loaded active running Job spooling tools
auditd.service                     loaded active running Security Auditing Service
avahi-daemon.service               loaded active running Avahi mDNS/DNS-SD Stack


[root@server1 /]# systemctl list-units --type=service --all
UNIT                                  LOAD   ACTIVE   SUB     DESCRIPTION
abrt-ccpp.service                     loaded active   exited  Install ABRT coredump hook
abrt-oops.service                     loaded active   running ABRT kernel log watcher
abrt-vmcore.service                   loaded inactive dead    Harvest vmcores for ABRT
abrt-xorg.service                     loaded active   running ABRT Xorg log watcher
abrtd.service                         loaded active   running ABRT Automated Bug Reporting Tool
accounts-daemon.service               loaded active   running Accounts Service
alsa-restore.service                  loaded inactive dead    Restore Sound Card State
alsa-state.service                    loaded active   running Manage Sound Card State (restore and st
alsa-store.service                    loaded inactive dead    Store Sound Card State

6.      View the enable and disable settings for all units. Optionally, limit the type of unit.
# systemctl list-unit-files –type=service

[root@server1 /]# systemctl list-unit-files --type=service
UNIT FILE                                   STATE  
abrt-ccpp.service                           enabled
abrt-oops.service                           enabled
abrt-pstoreoops.service                     disabled
abrt-vmcore.service                         enabled
abrt-xorg.service                           enabled
abrtd.service                               enabled
accounts-daemon.service                     enabled
alsa-restore.service                        static 
alsa-state.service                          static 
alsa-store.service                          static 
arp-ethers.service                          disabled

7.      View only failed services
# systemctl –failed –type=service

[root@server1 /]# systemctl --failed --type=service
UNIT          LOAD   ACTIVE SUB    DESCRIPTION
rhnsd.service loaded failed failed LSB: Starts the Spacewalk Daemon

Starting and stopping system daemons on a running system.

1.      View the status of a service.
# systemctl status

2.      Verify that the process is running.
# ps –up PID

3.      Stop the service and verify the status
# systemctl stop sshd.service
# systemctl status sshd.service

[root@server1 /]# systemctl stop sshd.service
[root@server1 /]# systemctl status sshd.service
sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
   Active: inactive (dead) since Mon 2016-01-04 04:44:49 CET; 11s ago
  Process: 1582 ExecStart=/usr/sbin/sshd -D $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 1573 ExecStartPre=/usr/sbin/sshd-keygen (code=exited, status=0/SUCCESS)
 Main PID: 1582 (code=exited, status=0/SUCCESS)

Jan 01 10:22:47 server1.example.com systemd[1]: Started OpenSSH server daemon.
Jan 01 10:22:49 server1.example.com sshd[1582]: Server listening on 0.0.0.0 port 2022.
Jan 01 10:22:49 server1.example.com sshd[1582]: Server listening on :: port 2022.
Jan 04 04:44:49 server1.example.com systemd[1]: Stopping OpenSSH server daemon...
Jan 04 04:44:49 server1.example.com sshd[1582]: Received signal 15; terminating.
Jan 04 04:44:49 server1.example.com systemd[1]: Stopped OpenSSH server daemon.

4.      Start the service and view the status. The process ID has changed
# systemctl start sshd.service
# systemctl status sshd.service

[root@server1 /]# systemctl start sshd.service
[root@server1 /]# systemctl status sshd.service
sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
   Active: active (running) since Mon 2016-01-04 04:47:56 CET; 4s ago
  Process: 15722 ExecStartPre=/usr/sbin/sshd-keygen (code=exited, status=0/SUCCESS)
 Main PID: 15724 (sshd)
   CGroup: /system.slice/sshd.service
           └─15724 /usr/sbin/sshd -D

Jan 04 04:47:56 server1.example.com systemd[1]: Starting OpenSSH server daemon...
Jan 04 04:47:56 server1.example.com systemd[1]: Started OpenSSH server daemon.
Jan 04 04:47:56 server1.example.com sshd[15724]: Server listening on 0.0.0.0 port 2022.
Jan 04 04:47:56 server1.example.com sshd[15724]: Server listening on :: port 2022.

5.      Stop, then start, the service in a single command.
# systemctl restart sshd.service
# systemctl status sshd.service

[root@server1 /]# systemctl restart sshd.service
[root@server1 /]# systemctl status sshd.service
sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
   Active: active (running) since Mon 2016-01-04 04:48:49 CET; 2s ago
  Process: 15738 ExecStartPre=/usr/sbin/sshd-keygen (code=exited, status=0/SUCCESS)
 Main PID: 15739 (sshd)
   CGroup: /system.slice/sshd.service
           └─15739 /usr/sbin/sshd -D

Jan 04 04:48:49 server1.example.com systemd[1]: Starting OpenSSH server daemon...
Jan 04 04:48:49 server1.example.com systemd[1]: Started OpenSSH server daemon.
Jan 04 04:48:49 server1.example.com sshd[15739]: Server listening on 0.0.0.0 port 2022.
Jan 04 04:48:49 server1.example.com sshd[15739]: Server listening on :: port 2022.

6.      Issue instructions for a service to read and reload its configuration file without a complete stop and start. The process ID will not change.
# systemctl reload sshd.service
# systemcl status sshd.service

[root@server1 /]# systemctl reload sshd.service
[root@server1 /]# systemctl status sshd.service
sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
   Active: active (running) since Mon 2016-01-04 04:48:49 CET; 57s ago
  Process: 15754 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
  Process: 15738 ExecStartPre=/usr/sbin/sshd-keygen (code=exited, status=0/SUCCESS)
 Main PID: 15739 (sshd)
   CGroup: /system.slice/sshd.service
           └─15739 /usr/sbin/sshd -D

Jan 04 04:48:49 server1.example.com systemd[1]: Starting OpenSSH server daemon...
Jan 04 04:48:49 server1.example.com systemd[1]: Started OpenSSH server daemon.
Jan 04 04:48:49 server1.example.com sshd[15739]: Server listening on 0.0.0.0 port 2022.
Jan 04 04:48:49 server1.example.com sshd[15739]: Server listening on :: port 2022.
Jan 04 04:49:38 server1.example.com systemd[1]: Reloading OpenSSH server daemon.
Jan 04 04:49:38 server1.example.com sshd[15739]: Received SIGHUP; restarting.
Jan 04 04:49:38 server1.example.com sshd[15739]: Server listening on 0.0.0.0 port 2022.
Jan 04 04:49:38 server1.example.com sshd[15739]: Server listening on :: port 2022.
Jan 04 04:49:38 server1.example.com systemd[1]: Reloaded OpenSSH server daemon.

Masking services
At times, a system may have conflicting services installed. For example, there are multiple methods to manage networks (network and NetworkManager) and firewalls (iptables and firewalld). To prevent and administrator from accidentally starting a service, that service may be masked. Masking will create a link in the configuration directories so that if the service is started nothing will happen.

# systemctl mask network
# systemctl unmask network

[root@server1 /]# systemctl mask network
ln -s '/dev/null' '/etc/systemd/system/network.service'

[root@server1 /]# systemctl unmask network
rm '/etc/systemd/system/network.service'

Enabling system daemons to start or stop at boot

Starting a service on a running system does nto guarantee that the service will be started when the system reboots. Similarly, stopping a service on a running system will not keep it from starting again when the system reboots. Services are started at boot time when links are created in the appropriated systemd configuration directories. These links are created and removed with systemctl commands.

1.      View the status of a service.
# systemctl status sshd.service

[root@server1 /]# systemctl status sshd.service
sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
   Active: active (running) since Mon 2016-01-04 04:48:49 CET; 2min 50s ago
 Main PID: 15739 (sshd)
   CGroup: /system.slice/sshd.service
           └─15739 /usr/sbin/sshd -D

Jan 04 04:48:49 server1.example.com systemd[1]: Starting OpenSSH server daemon...
Jan 04 04:48:49 server1.example.com systemd[1]: Started OpenSSH server daemon.
Jan 04 04:48:49 server1.example.com sshd[15739]: Server listening on 0.0.0.0 port 2022.
Jan 04 04:48:49 server1.example.com sshd[15739]: Server listening on :: port 2022.
Jan 04 04:49:38 server1.example.com systemd[1]: Reloading OpenSSH server daemon.
Jan 04 04:49:38 server1.example.com sshd[15739]: Received SIGHUP; restarting.
Jan 04 04:49:38 server1.example.com sshd[15739]: Server listening on 0.0.0.0 port 2022.
Jan 04 04:49:38 server1.example.com sshd[15739]: Server listening on :: port 2022.
Jan 04 04:49:38 server1.example.com systemd[1]: Reloaded OpenSSH server daemon.

2.      Disable the service and verify the status. Note that disabling a service does not stop the service.
# systemctl disable sshd.service
# systemctl status sshd.service

[root@server1 /]# systemctl disable sshd.service
rm '/etc/systemd/system/multi-user.target.wants/sshd.service'
[root@server1 /]# systemctl status sshd.service
sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled)
   Active: active (running) since Mon 2016-01-04 04:48:49 CET; 3min 44s ago
 Main PID: 15739 (sshd)
   CGroup: /system.slice/sshd.service
           └─15739 /usr/sbin/sshd -D

Jan 04 04:48:49 server1.example.com systemd[1]: Starting OpenSSH server daemon...
Jan 04 04:48:49 server1.example.com systemd[1]: Started OpenSSH server daemon.
Jan 04 04:48:49 server1.example.com sshd[15739]: Server listening on 0.0.0.0 port 2022.
Jan 04 04:48:49 server1.example.com sshd[15739]: Server listening on :: port 2022.
Jan 04 04:49:38 server1.example.com systemd[1]: Reloading OpenSSH server daemon.
Jan 04 04:49:38 server1.example.com sshd[15739]: Received SIGHUP; restarting.
Jan 04 04:49:38 server1.example.com sshd[15739]: Server listening on 0.0.0.0 port 2022.
Jan 04 04:49:38 server1.example.com sshd[15739]: Server listening on :: port 2022.
Jan 04 04:49:38 server1.example.com systemd[1]: Reloaded OpenSSH server daemon.

3.      Enable the service and verify the status.
# systemctl enable sshd.service
# systemctl is-enable sshd.service

[root@server1 /]# systemctl enable sshd.service
ln -s '/usr/lib/systemd/system/sshd.service' '/etc/systemd/system/multi-user.target.wants/sshd.service'
[root@server1 /]# systemctl is-enabled sshd.service
enabled


Summary of systemctl commands:
Tasks
Command:
View detailed information about a unit state
# systemctl status UNIT
Stop a service on a running system
# systemctl stop UNIT
Stop a service on a running system
# systemctl stop UNIT
Start a service on a running system
# systemctl start UNIT
Restart a service on a running system
# systemctl restart UNIT
Reload configuration file of a running service
# systemctl reload UNIT
Completely disable a service from being started, both manually and at boot.
# systemctl mask UNIT
Mask a masked service available
# systemctl unmask UNIT
Configure a service to start at boot time
# systemctl enable UNIT
Disable a service from starting at boot time.
# systemctl disable UNIT
List units which are required and wanted by the specified unit
# systemcl list-dependencies UNIT

Thank you for reading.

For Reading other article, visit to “https://sites.google.com/site/unixwikis/

No comments:

Post a Comment