{"id":803,"date":"2014-10-09T10:44:57","date_gmt":"2014-10-09T13:44:57","guid":{"rendered":"http:\/\/blog.abratel.com.br\/?p=803"},"modified":"2014-10-09T11:15:04","modified_gmt":"2014-10-09T14:15:04","slug":"restarting-services-in-red-hat-7-or-centos-7-using-systemctl","status":"publish","type":"post","link":"https:\/\/blog.abratel.com.br\/?p=803","title":{"rendered":"Restarting Services in Red Hat 7 or CentOS 7 using systemctl (init.d &#8211;> systemctl)"},"content":{"rendered":"<p># systemctl -t service <\/p>\n<p>Para parar um servi\u00e7o em execu\u00e7\u00e3o no sistema: <\/p>\n<p># systemctl stop iptables.service <\/p>\n<p>Para iniciar um servi\u00e7o no sistema: <\/p>\n<p># systemctl start httpd.service <\/p>\n<p>Para desativar um servi\u00e7o no sistema: <\/p>\n<p># systemctl disable sendmail.service <\/p>\n<p>Para ativar um servi\u00e7o no sistema: <\/p>\n<p># systemctl enable crond.service <\/p>\n<p>Conferir se servi\u00e7os est\u00e3o ativos ou n\u00e3o: <\/p>\n<p># systemctl is-enabled iptables.service <\/p>\n<p>Qualquer d\u00favida: <\/p>\n<p># man systemctl <\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>Q: I just recently installed CentOS 7 and am so confused.  How do I restart services like sshd and crond?<\/p>\n<p>A: Red Hat 7 and CentOS 7 have now moved to systemd as their default system management daemon.  Systemd is different from the old default init system in too many ways to describe here.  But let&#8217;s get to the meat of your question.<\/p>\n<p>The new way to control system daemons is with the systemctl command.  If you are used to the old init scripts (e.x. \/etc\/init.d\/sshd) the new syntax can be slightly confusing.  For example, this is how we used to restart SSHD with the old upstart init scripts:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n\/etc\/init.d\/sshd restart \r\n<\/pre>\n<p>or<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nservice sshd restart\r\n<\/pre>\n<p>In systemd (Fedora 18 or above, RHEL 7, and CentOS 7) we need to use the systemctl command, like so:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsystemctl restart sshd.service\r\n<\/pre>\n<p>To stop the sshd service, you would use:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsystemctl stop sshd.service\r\n<\/pre>\n<p>To start the sshd service, you would use:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsystemctl start sshd.service\r\n<\/pre>\n<p>To see the status of the sshd service, you would use:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">systemctl status sshd.service<\/pre>\n<p>The old \/etc\/init.d\/ scripts are still available for some services for legacy support and backward compatibility.  But you should get into the habit of using the new systemctl command because they can be removed in future updates or releases.<\/p>\n<p>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.<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>One of the major changes in RHEL  \/ CentOS 7.0 is the swtich to systemd, a system and service manager, that replaces SysV and Upstart used in previous releases of Red Hat Enterprise Linux. systemd is compatible with SysV and Linux Standard Base init scripts.<\/p>\n<p>Systemd is a system and service manager for Linux operating systems. It is designed to be backwards compatible with SysV init scripts, and provides a number of features such as parallel startup of system services at boot time, on-demand activation of daemons, support for system state snapshots, or dependency-based service control logic.<\/p>\n<p>Previous versions of Red Hat Enterprise Linux, which were distributed with SysV init or Upstart, used init scripts written in bash located in the \/etc\/rc.d\/init.d\/ directory. In RHEL 7 \/ CentOS 7, these init scripts have been replaced with service units. Service units end with the .service file extension and serve a similar purpose as init scripts. To view, start, stop, restart, enable, or disable system services you will use the systemctl instead of the old service command.<br \/>\nNote: for backwards compatibility the old service command is still available in CentOS 7 and it will redirect any command to the new systemctl utility.<br \/>\nStart\/Stop\/Restart Services with systemctl<\/p>\n<p>To start a service with systemctl you will need to use the command like this:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">    # systemctl start httpd.service <\/pre>\n<p>This will start the httpd service, in our case Apache HTTP Server.<\/p>\n<p>To stop it use this command as root:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">  \r\n    # systemctl stop httpd.service <\/pre>\n<p>To restart you can use either the restart options, it will restart the service if it&#8217;s running or start it if it&#8217;s not running. You can also use the try-restart option that will restart the service only if it&#8217;s already running. Also you have the reload option that will reload the configuration files.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">  \r\n    # systemctl restart httpd.service\r\n    # systemctl try-restart httpd.service\r\n    # systemctl reload httpd.service\r\n<\/pre>\n<p>The commands in our example look like this:<\/p>\n<p><a href=\"http:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-enable.png\"><img loading=\"lazy\" src=\"http:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-enable-300x16.png\" alt=\"systemctl-enable\" width=\"300\" height=\"16\" class=\"alignnone size-medium wp-image-816\" srcset=\"https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-enable-300x16.png 300w, https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-enable-1024x55.png 1024w, https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-enable-600x32.png 600w, https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-enable.png 1196w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nChecking the status of a service<\/p>\n<p>To check the status of a service you can use the status option like this:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">  \r\n    # systemctl status httpd.service <\/pre>\n<p>And the output should look like this:<br \/>\n<a href=\"http:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-status.png\"><img loading=\"lazy\" src=\"http:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-status-300x90.png\" alt=\"systemctl-status\" width=\"300\" height=\"90\" class=\"alignnone size-medium wp-image-815\" srcset=\"https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-status-300x90.png 300w, https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-status-1024x308.png 1024w, https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-status-600x180.png 600w, https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-status.png 1083w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Informing you of various aspects of the running service.<br \/>\nEnable \/ Disable services to run at boot time<\/p>\n<p>You can also use the enable \/ disable options to make a service run at boot time, using the command like this:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">  \r\n    # systemctl enable httpd.service\r\n    # systemctl disable httpd.service\r\n<\/pre>\n<p>Although the adoption of systemd has been very controversial in the last few years, slowly most of the major Linux distributions have either adopted or are planning to have it in the next point release, so it&#8217;s a useful tool to get used to.<br \/>\n&#8211; See more at: http:\/\/linoxide.com\/linux-command\/start-stop-services-systemd\/#sthash.qiGQNlM4.dpuf<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>Say hello to systemctl command<\/p>\n<p>Use this command to control the systemd system and act as a service manager.<br \/>\nCentOS 7 \/ RHEL 7 get status of network service<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo systemctl status network.service <\/pre>\n<p>OR<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo systemctl status network <\/pre>\n<p>Sample outputs:<br \/>\nFig.01: CentOS \/ RHEL 7 Networking Service Status Command<\/p>\n<p>Fig.01: CentOS \/ RHEL 7 Networking Service Status Command<\/p>\n<p>CentOS 7 \/ RHEL 7 restart network service<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo systemctl restart network.service\r\n<\/pre>\n<p>OR<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> sudo systemctl restart network <\/pre>\n<p>CentOS 7 \/ RHEL 7 start network service<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> sudo systemctl start network.service <\/pre>\n<p>OR<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> sudo systemctl start network <\/pre>\n<p>CentOS 7 \/ RHEL 7 stop network service<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo systemctl stop network.service\r\n<\/pre>\n<p>OR<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo systemctl stop network <\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>With Red Hat Enterprise Linux 7 released and CentOS version 7 newly unveiled, now is a good time to cover systemd, the replacement for legacy System V (SysV) startup scripts and runlevels. Red Hat-based distributions are migrating to systemd because it provides more efficient ways of managing services and quicker startup times. With systemd there are fewer files to edit, and all the services are compartmentalized and stand separate from each other. This means that should you screw up one config file, it won&#8217;t automatically take out other services.<\/p>\n<p>Systemd has been the default system and services manager in Red Hat Fedora since the release of Fedora 15, so it is extensively field-tested. It provides more consistency and troubleshooting ability than SysV \u2013 for instance, it will report if a service has failed, is suspended, or is in error. Perhaps the biggest reason for the move to systemd is that it allows multiple services to start up at the same time, in parallel, making machine boot times quicker than they would be with legacy runlevels.<\/p>\n<p>Under systemd, services are now defined in what are termed unit files, which are text files that contain all the configuration information a service needs to start, including its dependencies. Service files are located in \/usr\/lib\/systemd\/system\/. Many but not all files in that directory will end in .service; systemd also manages sockets and devices.<\/p>\n<p>No longer do you directly modify scripts to configure runlevels. Within systemd, runlevels have been replaced by the concept of states. States can be described as &#8220;best efforts&#8221; to get a host into a desired configuration, whether it be single-user mode, networking non-graphical mode, or something else. Systemd has some predefined states created to coincide with legacy runlevels. They are essentially aliases, designed to mimic runlevels by using systemd.<\/p>\n<p>States require additional components above and beyond services. Therefore, systemd uses unit files not only to configure services, but also mounts, sockets, and devices. These units&#8217; names end in .sockets, .devices, and so on.<\/p>\n<p>Targets, meanwhile, are logical groups of units that provide a set of services. Think of a target as a wrapper in which you can place multiple units, making a tidy bundle to work with.<\/p>\n<p>Unit files are built from several configurable sections, including unit descriptions and dependencies. Systemd also allows administrators to explicitly define a service&#8217;s dependencies and load them before the given service starts by editing the unit files. Each unit file has a line that starts After= that can be used to define what service is required before the current service can start. WantedBy=lines specify that a target requires a given unit.<\/p>\n<p>Targets have more meaningful names than those used in SysV-based systems. A name like graphical.target gives admins an idea of what a file will provide! To see the current target at which the system is residing, use the command systemctl get-default. To set the default target, use the command systemctl set-default targetname.target. targetname can be, among others:<\/p>\n<p>    rescue.target<br \/>\n    multi-user.target<br \/>\n    graphical.target<br \/>\n    reboot.target<\/p>\n<p>Looking at the above it becomes obvious that although there is no direct mapping between runlevels and targets, systemd provides what could loosely be termed equivalent levels.<\/p>\n<p>Another important feature systemd implements is cgroups, short for control groups, which provide security and manageability for the resources a system can use and control. With cgroups, services that use the same range of underlying operating system calls are grouped together. These control groups then manage the resources they control. This grouping performs two functions: it allows administrators to manage the amount of resources a group of services gets, and it provides additional security in that a service in a certain cgroup can&#8217;t jump outside of cgroups control, preventing it for example from getting access to other resources controlled by other cgroups.<\/p>\n<p>Cgroups existed in the old SysV model, but were not really implemented well. systemd attempts to fix this issue.<br \/>\nFirst steps in systemd<\/p>\n<p>Under systemd you can still use the service and chkconfig commands to manage those additional legacy services, such as Apache, that have not yet been moved over to systemd management. You can also use service command to manage systemd-enabled services. However, several monitoring and logging services, including cron and syslog, have been rewritten to use the functionality that is available in systemd, in part because scheduling and some of the cron functionality is now provided by systemd.<\/p>\n<p>You can also manage systemd with a GUI management tool called systemd System Manger, though it is not usually installed by default. To install it, as root, run yum -y install systemd-ui.<\/p>\n<p>How can you start managing systemd services? Now that Centos 7 is out of the starting gate we can start to experiment with systemd and understand its operation. To begin, as the root user in a terminal, type chkconfig. The output shows all the legacy services running. As you can see by the big disclaimer, most of the other services that one would expect to be present are absent, because they have been migrated to systemd management.<\/p>\n<p>Red Hat-based OSes no longer use the old \/etc\/initab file, but instead use a system.default configuration file. You can symlink a desired target to the system.default in order to have that target start up when the system boots. To configure the target to start a typical multi-user system, for example, run the command below:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nln -sf \/lib\/systemd\/system\/multi-user.target \/etc\/systemd\/system\/default.target\r\n<\/pre>\n<p>After you make the symlink, run systemctl, the replacement for chkconfig. Several pages of output display, listing all the services available:<br \/>\nsystemctl<\/p>\n<p><a href=\"http:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-resized-600.png\"><img loading=\"lazy\" src=\"http:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-resized-600-300x214.png\" alt=\"systemctl-resized-600\" width=\"300\" height=\"214\" class=\"alignnone size-medium wp-image-806\" srcset=\"https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-resized-600-300x214.png 300w, https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/systemctl-resized-600.png 600w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>    Unit \u2013 the service name<br \/>\n    Load \u2013 gives status of the service (such as Loaded, Failed, etc.)<br \/>\n    Active \u2013 indicates whether the status of the service is Active<br \/>\n    Description \u2013 textual description of the unit<\/p>\n<p>The key commands and arguments in systemctl are similar to the legacy ones found in chkconfig \u2013 for example, systemctl start postfix.service.<\/p>\n<p>In the same vein, use systemctl stop and systemctl status to stop services or view information. This syntax similarity to chkconfig arguments is by design, to make the transition to systemd as smooth as possible.<\/p>\n<p>To see all the services you can start using systemctl and their statuses, use the command<\/p>\n<p>systemctl list-unit-files &#8211;type=service<\/p>\n<p>services<br \/>\n<a href=\"http:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/services-resized-600.png\"><img loading=\"lazy\" src=\"http:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/services-resized-600-300x204.png\" alt=\"services-resized-600\" width=\"300\" height=\"204\" class=\"alignnone size-medium wp-image-807\" srcset=\"https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/services-resized-600-300x204.png 300w, https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2014\/10\/services-resized-600.png 600w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>While you can no longer enable a runlevel for a service using chkconfig &#8211;level, under systemd you can enable or disable a service when it boots. Use systemctl enable service to enable a service, and systemctl disable service to keep it from starting at boot. Get a service&#8217;s current status (enabled or disabled) with the command systemctl is-enabled service.<br \/>\nFinal thoughts on systemd<\/p>\n<p>It may take you some time to get used to systemd, but you should plan to use it now before it becomes a requirement and management through legacy tools is no longer available. You should find that systemd makes managing services easier than it used to be with SysV.<\/p>\n","protected":false},"excerpt":{"rendered":"<p># systemctl -t service Para parar um servi\u00e7o em execu\u00e7\u00e3o no sistema: # systemctl stop iptables.service Para iniciar um servi\u00e7o no sistema: # systemctl start httpd.service Para desativar um servi\u00e7o no sistema: # systemctl disable sendmail.service Para ativar um servi\u00e7o no sistema: # systemctl enable&#8230;<\/p>\n","protected":false},"author":1,"featured_media":808,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts\/803"}],"collection":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=803"}],"version-history":[{"count":6,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts\/803\/revisions"}],"predecessor-version":[{"id":818,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts\/803\/revisions\/818"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/media\/808"}],"wp:attachment":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}