<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>服务器安全维护工作室 &#187; 服务器运维工程师。AWS，阿里云等服务器配置&amp;故障排查的技术支持。</title>
	<atom:link href="https://www.fuwuqiok.com/tag/%e6%9c%8d%e5%8a%a1%e5%99%a8%e8%bf%90%e7%bb%b4%e5%b7%a5%e7%a8%8b%e5%b8%88%e3%80%82aws%ef%bc%8c%e9%98%bf%e9%87%8c%e4%ba%91%e7%ad%89%e6%9c%8d%e5%8a%a1%e5%99%a8%e9%85%8d%e7%bd%ae%e6%95%85%e9%9a%9c/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fuwuqiok.com</link>
	<description></description>
	<lastBuildDate>Sun, 01 Mar 2020 07:28:40 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.26</generator>
	<item>
		<title>Systemd服务管理说明</title>
		<link>https://www.fuwuqiok.com/systemd%e6%9c%8d%e5%8a%a1%e7%ae%a1%e7%90%86%e8%af%b4%e6%98%8e/</link>
		<comments>https://www.fuwuqiok.com/systemd%e6%9c%8d%e5%8a%a1%e7%ae%a1%e7%90%86%e8%af%b4%e6%98%8e/#comments</comments>
		<pubDate>Fri, 31 Jul 2015 13:05:37 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[aliyun服务器代维]]></category>
		<category><![CDATA[服务器运维工程师。AWS，阿里云等服务器配置&故障排查的技术支持。]]></category>

		<guid isPermaLink="false">https://www.fuwuqiok.com/?p=2186</guid>
		<description><![CDATA[<p>阿里云和AWS，都公布了CentOS 7镜像。购买云主机，配置系统时，又多了一个选择。 然而，CentOS 7 [&#8230;]</p>
<p><a rel="nofollow" href="https://www.fuwuqiok.com/systemd%e6%9c%8d%e5%8a%a1%e7%ae%a1%e7%90%86%e8%af%b4%e6%98%8e/">Systemd服务管理说明</a>，首发于<a rel="nofollow" href="https://www.fuwuqiok.com">服务器安全维护工作室</a>。</p>
]]></description>
				<content:encoded><![CDATA[<p>阿里云和AWS，都公布了CentOS 7镜像。购买云主机，配置系统时，又多了一个选择。<br />
然而，CentOS 7，已将systemd作为默认的init系统，<br />
随着Systemd的采用，使用systemctl命令而非原来的service命令。因此，对systemd服务的学习研究显得更为迫切了。<br />
本文以阿里云服务器，Centos7系统为例。进行讲解说明Systemd服务的管理。</p>
<h3>1.Systemd服务管理</h3>
<p>Systemd到目前为止，服务启动脚本是通过Unit形式来定义的。服务的管理也就等于Unit的管理。</p>
<h3>2.有效的Unit一览表(list-units)</h3>
<p>Systemctl命令默认的选项为【systemctl list-units】因此，即使不添加选项，list-units的结果也会显示出来。<br />
另外，Systemctl命令，添加–no-pager选项的话，结果页面会全部显示出来。</p>
<pre># systemctl list-units
UNIT                        LOAD   ACTIVE SUB       DESCRIPTION
proc-sys...t_misc.automount loaded active running   Arbitrary Executable File Fo
sys-devi...tty-ttyS0.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS1.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS2.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS3.device loaded active plugged   /sys/devices/platform/serial
sys-devi...vdb-xvdb1.device loaded active plugged   /sys/devices/vbd-51728/block
sys-devi...lock-xvdb.device loaded active plugged   /sys/devices/vbd-51728/block
sys-devi...vda-xvda1.device loaded active plugged   /sys/devices/vbd-768/block/x
sys-devi...lock-xvda.device loaded active plugged   /sys/devices/vbd-768/block/x
sys-devi...-net-eth0.device loaded active plugged   /sys/devices/vif-0/net/eth0
sys-devi...-net-eth1.device loaded active plugged   /sys/devices/vif-1/net/eth1
sys-module-configfs.device  loaded active plugged   /sys/module/configfs
（省略）
LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

88 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
</pre>
<h3>3.安装的Unit文件一览表(list-unit-files)</h3>
<pre>[root@iZ2516ti6utZ ~]# systemctl list-unit-files
UNIT FILE                              STATE
proc-sys-fs-binfmt_misc.automount      static
dev-hugepages.mount                    static
dev-mqueue.mount                       static
proc-sys-fs-binfmt_misc.mount          static
sys-fs-fuse-connections.mount          static
sys-kernel-config.mount                static
sys-kernel-debug.mount                 static
tmp.mount                              disabled
brandbot.path                          disabled
cups.path                              disabled
（省略）
239 unit files listed.</pre>
<p>例如，用yum install httpd安装Apache的话，会记载如下所示的Unit文件。</p>
<pre># systemctl list-unit-files | grep httpd
httpd.service                          disabled
</pre>
<h3>4.Unit的有效化(enable)</h3>
<p>有效的话，如下，系统启动时，会作为启动服务显示出来。systemctl enable httpd和chkonfig httpd on是一样的。</p>
<pre># systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
</pre>
<p>另外，所谓的multi-user和runlevel 2 or 3 or 4相同，作用如下。</p>
<pre>runlevel0 -&gt; poweroff
runlevel1 -&gt; rescue
runlevel2 -&gt; multi-user
runlevel3 -&gt; multi-user
runlevel4 -&gt; multi-user
runlevel5 -&gt; graphical
runlevel6 -&gt; reboot
</pre>
<p>有效的话，安装的后为disable的服务会变成enable。</p>
<pre># systemctl list-unit-files --no-pager | grep httpd
httpd.service                          enabled
</pre>
<h3>5.Unit无效化 (disable)</h3>
<p>无效之后，如下，系统启动时，将从启动的服务对象中删除。systemctl disable httpd和chkonfig httpd off是一样的。</p>
<pre># systemctl disable httpd
rm '/etc/systemd/system/multi-user.target.wants/httpd.service'
</pre>
<p>无效化之后，可用list-unit-files确认，已为disable。</p>
<pre># systemctl list-unit-files | grep httpd
httpd.service                          disabled
</pre>
<h3>6.Unit有效/无效的确认(is-enable)</h3>
<p>有效的话为enabled，无效的话为disabled。</p>
<pre># systemctl is-enabled httpd
disabled
# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service' 
# systemctl is-enabled httpd
enabled
</pre>
<h3>7.Unit的再有效化 (reenable)</h3>
<p>若再有效化，会由disable变为enable，启动脚本的软链接会执行重新粘贴动作。</p>
<pre># systemctl reenable httpd
rm '/etc/systemd/system/multi-user.target.wants/httpd.service'
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
</pre>
<h3>8.Unit的启动 (start)</h3>
<p>如果是service命令的话，会显示启动经过。但是systemctl的话即使启动，也不会特意显示信息出来。</p>
<pre># systemctl start httpd
# ps aux | grep httpd
root     28023  0.0  0.4 209696  4784 ?        Ss   15:19   0:00 /usr/sbin/httpd -DFOREGROUND
apache   28024  0.0  0.2 211780  2964 ?        S    15:19   0:00 /usr/sbin/httpd -DFOREGROUND
apache   28025  0.0  0.2 211780  2964 ?        S    15:19   0:00 /usr/sbin/httpd -DFOREGROUND
apache   28026  0.0  0.2 211780  2964 ?        S    15:19   0:00 /usr/sbin/httpd -DFOREGROUND
apache   28027  0.0  0.2 211780  2964 ?        S    15:19   0:00 /usr/sbin/httpd -DFOREGROUND
apache   28028  0.0  0.2 211780  2964 ?        S    15:19   0:00 /usr/sbin/httpd -DFOREGROUND
root     28035  0.0  0.0 112640   964 pts/0    S+   15:20   0:00 grep --color=auto httpd
</pre>
<h3>9.Unit启动状态的确 (status)</h3>
<p>&nbsp;</p>
<h3>10.Unit详细信息确认(show)</h3>
<p>确认Unit的详细信息，仅仅用眼查看，还是status为好，但是脚本处理的话，show倒会更好。</p>
<pre># systemctl show httpd
Id=httpd.service
Names=httpd.service
Requires=basic.target
Wants=system.slice
Conflicts=shutdown.target
Before=shutdown.target
After=network.target remote-fs.target nss-lookup.target systemd-journald.socket basic.target system.slice
Description=The Apache HTTP Server
LoadState=loaded
ActiveState=active
SubState=running
……
……
</pre>
<h3>11.Unit的停止(stop)</h3>
<p>Unit的停止也是一样的，不会特意显示信息。</p>
<pre># systemctl stop httpd
# ps aux | grep httpd
root     28054  0.0  0.0 112640   964 pts/0    S+   15:29   0:00 grep --color=auto httpd
</pre>
<h3>12.重装Unit的配置文件(reload)</h3>
<p>能不能重新加载，是由Unit决定的。</p>
<pre># systemctl reload httpd
</pre>
<h3>13.Unit的重起(restart)</h3>
<p>和start，stop一样，不会显示信息。</p>
<pre># systemctl restart httpd
</pre>
<p><strong>13-1.试行Unit的重起(try-restart)</strong><br />
Unit启动中的话，进行重启，Unit停止中的话，不进行任何操作。</p>
<pre># systemctl try-restart httpd
</pre>
<h3>14.Unit重装或重起(reload-or -restart)</h3>
<p>Unit如果可以重装的话，就reload，不能重装的话就执行restart。或者Unit停止的话进行启动。</p>
<pre># systemctl reload-or-restart httpd
</pre>
<p>或Unit停止</p>
<pre># systemctl stop httpd
# systemctl reload-or-restart httpd
# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: active (running) since Mon 2015-03-23 15:44:18 CST; 19s ago
  Process: 28113 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
  Process: 28081 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
 Main PID: 28121 (httpd)
……
……
</pre>
<p><strong>14-1.试行Unit重装或重起(reload-or-try-restart)</strong><br />
和reload-or-restart一样，但是Unit如果停止中的话不启动。</p>
<pre># systemctl reload-or-try-restart httpd
# systemctl stop httpd
# systemctl reload-or-try-restart httpd
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalc
</pre>
<h3>15.Unit的強制关闭(kill)</h3>
<pre># systemctl kill httpd
</pre>
<h3>16.Unit的屏蔽(mask/unmask)</h3>
<p>即使Unit无效（disable）的话，服务仍有可能启动。mask的话服务自身就不能启动了。</p>
<pre># systemctl mask httpd
ln -s '/dev/null' '/etc/systemd/system/httpd.service'
# systemctl start httpd
Failed to issue method call: Unit httpd.service is masked.
</pre>
<p>屏蔽的情况下，is-enabled结果会返回masked。</p>
<pre># systemctl is-enabled httpd
masked
</pre>
<p>解除屏蔽的话，执行unmask。</p>
<pre># systemctl unmask httpd
rm '/etc/systemd/system/httpd.service'
</pre>
<h3>17.确认Unit的启动状态 (is-active)</h3>
<pre>is-active的话，若unit启动中的话，就会返回active。
# systemctl start httpd
# systemctl is-active httpd
active
</pre>
<p>如果没有启动，就会返回unknown。</p>
<pre># systemctl stop httpd
# systemctl is-active httpd
Unknown
</pre>
<h3>18.确认Unit异常状况（is-failed）</h3>
<pre>is-failed在unit正常启动的话会返回active。
# systemctl is-failed httpd
Active
Unit不能正常启动，出现异常的话，显示failed结果。
# mv /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
# systemctl restart httpd
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.
# systemctl is-failed httpd
failed
</pre>
<h3>19.Unit异常状态的重启。</h3>
<p>重启识别的systemd异常状态。</p>
<pre># systemctl is-failed httpd
failed
# systemctl reset-failed httpd
# systemctl is-failed httpd
unknown
</pre>
<h3>20.Unit依赖关系的确认(list-dependencies)</h3>
<p>该unit所依赖的unit一览表。</p>
<div class="alert alert-info">Systemd是别具特色且相当有趣的，但是掌握起来还是要花费不少功夫和时间。可以预见，Systemd终将会成为主流。愿本文对大家在Systemd的学习方面有所帮助。</div>
<p><a rel="nofollow" href="https://www.fuwuqiok.com/systemd%e6%9c%8d%e5%8a%a1%e7%ae%a1%e7%90%86%e8%af%b4%e6%98%8e/">Systemd服务管理说明</a>，首发于<a rel="nofollow" href="https://www.fuwuqiok.com">服务器安全维护工作室</a>。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.fuwuqiok.com/systemd%e6%9c%8d%e5%8a%a1%e7%ae%a1%e7%90%86%e8%af%b4%e6%98%8e/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
