曹戈 發表於 2024-10-21 15:23:31

Linux的systemctl怎么用? systemctl命令一键搞定Linux服务管理的技巧

<p>如果您是 Linux 用户或管理员,如何管理系统中的服务呢?服务是指在后台运行并提供基本功能(例如网络、打印、日志等)的程序。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202410/2024102115110158.jpg" /></p>
<p>要在 Linux 中列出、启动、停止、启用、禁用或重启服务,可以使用 systemctl 命令。该命令是 systemd 的一部分,是 Ubuntu 和 Debian 等现代 Linux 发行版的系统和服务管理器。</p>
<p>如果您是 Linux 初学者,希望使用 systemctl 命令管理 Linux 服务,请继续阅读本文。</p>
<h3>使用 systemctl 命令列出所有 Linux 服务</h3>
<p>要列出 Linux 系统上的所有服务,请在「终端」中运行以下命令:</p>
<div class="jb51code"><pre>sudo systemctl --type=service</pre></div>
<p>这将输出显示包含五列的表格:<strong>UNIT</strong>、<strong>LOAD</strong>、<strong>ACTIVE</strong>、<strong>SUB</strong> 和 <strong>DESCRIPTION</strong>。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202410/2024102115110259.jpg" /></p>
<p style="text-align:center">使用 systemctl 命令列出Linux 服务</p>
<p><strong>UNIT</strong> 列显示服务单元文件的名称,该文件是服务的配置和定义文件。该文件包含有关服务的功能和管理方式的信息。你也可以在 <strong>DESCRIPTION</strong> 列中查看服务的简要描述。</p>
<p><strong>LOAD</strong> 列显示服务单元文件是否加载到内存中。<strong>ACTIVE</strong> 列显示服务是否正在运行。最后,<strong>SUB</strong> 列显示服务的子状态,例如 Running、exited 或 failed。</p>
<p>你也可以使用 <code>-a</code> 或 <code>--all</code> 选项代替 <code>--type service</code> 来列出所有类型的单元,而不仅仅是服务。</p>
<h3>使用 systemctl 命令列出活动服务</h3>
<p>如果只想查看当前 Linux 系统上运行的服务,请在「终端」中运行以下命令:</p>
<div class="jb51code"><pre>sudo systemctl --type=service --state=active</pre></div>
<p>该命令将过滤掉那些不活动(已停止)或失败的服务。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202410/2024102115110260.jpg" /></p>
<p style="text-align:center">使用 systemctl 命令列出活动服务</p>
<h3>使用 systemctl 列出不活动的服务</h3>
<p>同样地,如果您只想查看当前 Linux 系统中停止运行的服务,请在「终端」中运行以下命令:</p>
<div class="jb51code"><pre>sudo systemctl --type=service --state=inactive</pre></div>
<p>这将过滤掉那些正在运行或失败的服务。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202410/2024102115110261.jpg" /></p>
<p style="text-align:center">使用 systemctl 列出不活动的服务</p>
<h3>使用 systemctl 列出失败服务</h3>
<p>如果要查看 Linux 系统上启动或停止失败的服务,请打开「终端」并运行以下命令:</p>
<div class="jb51code"><pre>sudo systemctl --type=service --state=failed</pre></div>
<p>这将过滤掉当前活动(正在运行)或不活动(已停止)的服务。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202410/2024102115110262.jpg" /></p>
<p style="text-align:center">使用 systemctl 命令列出失败服务</p>
<h3>使用 systemctl 列出已启用的服务</h3>
<p>要查看哪些服务会随 Linux 系统启动时自动启动,请在「终端」中运行以下命令:</p>
<div class="jb51code"><pre>systemctl list-unit-files --state=enabled</pre></div>
<p>该命令将输出包含两列的表格:<strong>UNIT FILE</strong> 和 <strong>STATE</strong>。<strong>UNIT FILE</strong> 列显示服务单元文件的名称,<strong>STATE</strong> 列显示服务是否已启用。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202410/2024102115110363.jpg" /></p>
<p style="text-align:center">使用 systemctl 命令列出已启用的服务</p>
<h3>使用 systemctl 列出已禁用的服务</h3>
<p>要查看哪些服务已禁用并且不会随 Linux 系统自动启动,请在「终端」中运行以下命令:</p>
<div class="jb51code"><pre>systemctl list-unit-files --state=disabled</pre></div>
<p>这将过滤掉已启用或屏蔽的服务。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202410/2024102115110364.jpg" /></p>
<p style="text-align:center">使用 systemctl 命令列出已禁用的服务</p>
<h3>使用 systemctl 命令查看服务详细信息</h3>
<p>要查看特定 Linux 服务的更多详细信息,请在「终端」中运行以下命令:</p>
<div class="jb51code"><pre>sudo systemctl status &lt;service-name&gt;</pre></div>
<p>将 <code>&lt;service-name&gt;</code> 替换为要查看的服务单元文件的名称。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202410/2024102115110465.jpg" /></p>
<p style="text-align:center">使用 systemctl 命令查看服务详细信息</p>
<p>这将显示服务的描述、加载状态、活动状态、子状态、进程 ID (PID)、内存使用、CPU 使用情况和最近的服务日志等信息。</p>
<p>
                                                <div class="cupage"><a href='#'>上一页</a><strong>1</strong><a href='https://www.jb51.net/LINUXjishu/957619_2.html'>2</a> <a href='https://www.jb51.net/LINUXjishu/957619_2.html'>下一页</a> <a href='https://www.jb51.net/LINUXjishu/957619_all.html'>阅读全文</a></div>
頁: [1]
查看完整版本: Linux的systemctl怎么用? systemctl命令一键搞定Linux服务管理的技巧