Thursday, June 2, 2016

how to use systemctl command in RHEL7 and Centos 7 ?

systemctl command with all possible options rhel7 and centos7

systemctl is a command available in new version of Linux. Systemctl is used to control the systemd and service manager. To control services we have to use lot many options along with systemctl command. It is changed dramatically in new version of Linux. In this article we are going to explore as many possible ways as to use systemctl command in Linux.

Let’s start with checking the service status.

 Service Status Check and show service units
below is the command used to verify the service status
[root@Linuxforfreshers ~]# systemctl status network.service
network.service - LSB: Bring up/down networking
   Loaded: loaded (/etc/rc.d/init.d/network)
   Active: active (exited) since Thu 2016-04-28 13:39:38 IST; 32min ago
  Process: 5239 ExecReload=/etc/rc.d/init.d/network reload (code=killed, signal=TERM)
  Process: 5313 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=0/SUCCESS)
[root@linuxforfreshers ~]# systemctl show crond.service
Id=crond.service
Names=crond.service
Requires=basic.target
Wants=system.slice
Conflicts=shutdown.target
Before=shutdown.target

Verify service is active and enabled

If service is active it means service is running without any issues.  As a example we verify web service is running OR not.
[root@Linuxforfreshers ~]# systemctl is-active httpd.service
active
Enabling service means we are ensuring that service should start when server is rebooted. In older versions chkconfig command.
[root@linuxforfreshers ~]# systemctl enable crond.service
[root@linuxforfreshers ~]# systemctl is-enabled crond.service
enabled
Start and restart service using systemctl
As simple as starting the services and restarting the services is very easy, Here we have little more than that in New version.
[root@linuxforfreshers ~]# systemctl start crond.service
[root@linuxforfreshers ~]# systemctl restart crond.service
as we say it is more than start and restart we have systemctl try-restart now your thinking about what is the difference between restart and try-restart..?
option restart will restart the service if it is in stopped state also.
option try-restart Restart one or more units specified on the command line if the units are running. This does nothing if units are not running. Note that, for compatibility with Red Hat init scripts, condrestart is equivalent to this command.
[root@linuxforfreshers ~]# systemctl try-restart crond.service

 Listing dependencies, jobs, sockets, unit-files and Units
Listing dependencies means what are the services we have to start before starting this required service this before version this feature was not there.
Shows required and wanted units of the specified unit. If no unit is specified, default.target is implied. Target units are recursively expanded. When –all is passed, all other units are recursively expanded as well
[root@linuxforfreshers ~]# systemctl list-dependencies crond.service
crond.service
system.slice
└─basic.target
alsa-restore.service
alsa-state.service
firewalld.service
microcode.service
rhel-autorelabel-mark.service
rhel-autorelabel.service
rhel-configure.service
rhel-dmesg.service
rhel-loadmodules.service
paths.target
slices.target
list-jobs will show what are jobs running currently in background
[root@linuxforfreshers ~]# systemctl list-jobs
No jobs running.
Listing installed unit files
[root@linuxforfreshers ~]# systemctl list-unit-files |grep sshd
anaconda-sshd.service static
sshd-keygen.service static
sshd.service enabled
sshd@.service static
sshd.socket disabled
List all available sockets
[root@linuxforfreshers ~]# systemctl list-sockets
LISTEN UNIT ACTIVATES
/dev/initctl systemd-initctl.socket systemd-initctl.service
/dev/log systemd-journald.socket systemd-journald.service
/run/dmeventd-client dm-event.socket dm-event.service
/run/dmeventd-linuxforfreshers dm-event.socket dm-event.service
/run/lvm/lvmetad.socket lvm2-lvmetad.socket lvm2-lvmetad.service
/run/systemd/journal/socket systemd-journald.socket systemd-journald.service
/run/systemd/journal/stdout systemd-journald.socket systemd-journald.service
/run/systemd/shutdownd systemd-shutdownd.socket systemd-shutdownd.service
/run/udev/control systemd-udevd-control.socket systemd-udevd.service
/var/run/avahi-daemon/socket avahi-daemon.socket avahi-daemon.service
/var/run/cups/cups.sock cups.socket cups.service
/var/run/dbus/system_bus_socket dbus.socket dbus.service
/var/run/rpcbind.sock rpcbind.socket rpcbind.service
@ISCSIADM_ABSTRACT_NAMESPACE iscsid.socket iscsid.service
@ISCSID_UIP_ABSTRACT_NAMESPACE iscsiuio.socket iscsiuio.service
kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service
16 sockets listed.
Pass --all to see loaded but inactive sockets, too.
Note: because the addresses might contains spaces, this output is not suitable for programmatic consumption.
[root@linuxforfreshers ~]# systemctl list-sockets --show-types
LISTEN TYPE UNIT ACTIVATES
/dev/initctl FIFO systemd-initctl.socket systemd-initctl.service
/dev/log Datagram systemd-journald.socket systemd-journald.service
/run/dmeventd-client FIFO dm-event.socket dm-event.service
/run/dmeventd-linuxforfreshers FIFO dm-event.socket dm-event.service
/run/lvm/lvmetad.socket Stream lvm2-lvmetad.socket lvm2-lvmetad.service
/run/systemd/journal/socket Datagram systemd-journald.socket systemd-journald.service
/run/systemd/journal/stdout Stream systemd-journald.socket systemd-journald.service
/run/systemd/shutdownd Datagram systemd-shutdownd.socket systemd-shutdownd.service
/run/udev/control SequentialPacket systemd-udevd-control.socket systemd-udevd.service
/var/run/avahi-daemon/socket Stream avahi-daemon.socket avahi-daemon.service
/var/run/cups/cups.sock Stream cups.socket cups.service
/var/run/dbus/system_bus_socket Stream dbus.socket dbus.service
/var/run/rpcbind.sock Stream rpcbind.socket rpcbind.service
@ISCSIADM_ABSTRACT_NAMESPACE Stream iscsid.socket iscsid.service
@ISCSID_UIP_ABSTRACT_NAMESPACE Stream iscsiuio.socket iscsiuio.service
kobject-uevent 1 Netlink systemd-udevd-kernel.socket systemd-udevd.service
16 sockets listed.
Pass --all to see loaded but inactive sockets, too.
[root@linuxforfreshers ~]# systemctl list-sockets --failed
0 sockets listed.
Pass --all to see loaded but inactive sockets, too.

Setting up default target (Older version Run Level) and getting default target
We have to use set-default to set default run level and we can see default run level using get-default option as shown below example
[root@linuxforfreshers ~]# systemctl set-default multi-user.target
rm '/etc/systemd/system/default.target'
ln -s '/usr/lib/systemd/system/multi-user.target' '/etc/systemd/system/default.target'
[root@linuxforfreshers ~]# systemctl get-default
multi-user.target

Masking service ans unmasking service
What is mean by masking service, there is situation that company will have multiple administrators working together still there are times one administrator will stop the service which is not required but another administrator will start the same service unfortunately Or unknowingly , will lead to lot of problems to avoid this types of situations. We have to disable, stop the service and mask it, when other administrator try to start also the service will never start until unless explicitly unmask.
[root@linuxforfreshers ~]# systemctl disable crond.service
rm '/etc/systemd/system/multi-user.target.wants/crond.service'
[root@linuxforfreshers ~]# systemctl stop crond.service
[root@linuxforfreshers ~]# systemctl mask crond.service
ln -s '/dev/null' '/etc/systemd/system/crond.service'
[root@linuxforfreshers ~]# systemctl status crond.service
crond.service
Loaded: masked (/dev/null)
 Active: inactive (dead)
As shown in above command examples we have stopped the service, disabled the service and masked the service. Now try to start the service.
[root@linuxforfreshers ~]# systemctl start crond.service
Failed to issue method call: Unit crond.service is masked.
now unmask the service and start it will start
[root@linuxforfreshers ~]# systemctl unmask crond.service
rm '/etc/systemd/system/crond.service'
[root@linuxforfreshers ~]# systemctl start crond.service
[root@linuxforfreshers ~]# systemctl status crond.service
crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled)
Active: active (running) since Thu 2016-04-28 22:45:12 IST; 9s ago
Main PID: 7521 (crond)
CGroup: /system.slice/crond.service
└─7521 /usr/sbin/crond -n

Reload and reset service status
Most of the administrator will still have an question that what is the difference between service reload and restart.
Service reload is used whenever we changed something to the service and we would like to push the changes to the service without interrupting the connected users. Reloading the service will never change existing PID (Process Identity)
Service restart is used to restart the service which means stop and start the service, whenever we run restart existing users will disconnect and new PID will be created. It required little downtime to the service changes.
Before reloading the service status
[root@linuxforfreshers ~]# systemctl status sshd.service
sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
Active: active (running) since Thu 2016-04-28 05:56:52 IST; 16h ago
Process: 7228 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
Main PID: 1601 (sshd)
CGroup: /system.slice/sshd.service
└─1601 /usr/sbin/sshd -D

After reloading the service

[root@linuxforfreshers ~]# systemctl reload sshd.service
[root@linuxforfreshers ~]# systemctl status sshd.service
sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
Active: active (running) since Thu 2016-04-28 05:56:52 IST; 16h ago
Process: 7243 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
Main PID: 1601 (sshd)
CGroup: /system.slice/sshd.service
└─1601 /usr/sbin/sshd -D
if observe correctly PID before and after reload not changed.
Reset the “failed” state of the specified units

[root@linuxforfreshers ~]# systemctl reset-failed sshd.service

 Daemon reload option
After deleting the file or directory, you should reload the systemd process so that it no longer attempts to reference these files and reverts back to using the system copies. You can do this by typing:
[root@linuxforfreshers ~]# systemctl daemon-reload

Isolating Targets using systemctl command
It is possible to start all of the units associated with a target and stop all units that are not part of the dependency tree. The command that we need to do this is called, appropriately, isolate. This is similar to changing the runlevel in other init systems.

For instance, if you are operating in a graphical environment with graphical.target active, you can shut down the graphical system and put the system into a multi-user command line state by isolating the multi-user.target. Since graphical.target depends on multi-user.target but not the other way around, all of the graphical units will be stopped.

You may wish to take a look at the dependencies of the target you are isolating before performing this procedure to ensure that you are not stopping vital services:

systemctl list-dependencies multi-user.target

When you are satisfied with the units that will be kept alive, you can isolate the target by typing:
systemctl isolate multi-user.target

Create service snapshot and delete
Create a snapshot. If a snapshot name is specified, the new snapshot will be named after it. If none is specified, an automatic snapshot name is generated. In either case, the snapshot name used is printed to STDOUT, unless –quiet is specified.

A snapshot refers to a saved state of the systemd manager. It is implemented itself as a unit that is generated dynamically with this command and has dependencies on all units active at the time. At a later time, the user may return to this state by using the isolate command on the snapshot unit.
Snapshots are only useful for saving and restoring which units are running or are stopped, they do not save/restore any other state. Snapshots are dynamic and lost on reboot.

[root@linuxforfreshers ~]# systemctl snapshot sshd.service
sshd.service.snapshot
[root@linuxforfreshers ~]# systemctl status sshd.service.snapshot
sshd.service.snapshot
Loaded: loaded
Active: inactive (dead)
[root@linuxforfreshers ~]# systemctl delete sshd.service.snapshot
[root@linuxforfreshers ~]# systemctl status sshd.service.snapshot
sshd.service.snapshot
Loaded: not-found (Reason: No such file or directory)

Active: inactive (dead)

No comments:

Post a Comment