如何控制远程Linux服务器上的Systemd服务?如何控制远程Linux服务器上的Systemd服务?如何控制远程Linux服务器上的Systemd服务?如何控制远程Linux服务器上的Systemd服务?
  • 业务
  • 目标
  • 支持
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容

如何控制远程Linux服务器上的Systemd服务?

发表 admin at 2025年2月28日
类别
  • 未分类
标签

作为 Linux 用户和管理员,我们负责管理远程 Linux 服务器上的服务,其中可能包括 Apache 或 Nginx 等 Web 服务器,或 MySQL 或 PostgreSQL 等数据库服务器。为了有效地管理这些服务,我们需要对Linux的底层系统和服务管理器——systemd有深入的了解。

Systemd 是一个强大且功能丰富的系统和服务管理器,专为基于 Linux 的操作系统而设计。它负责管理服务的初始化和终止、跟踪系统进程并监督系统资源。它被广泛用作许多现代 Linux 发行版(例如 Ubuntu、Fedora、CentOS 和 Debian)中的默认系统和服务管理器。控制远程 Linux 服务器上的 systemd 服务对您来说至关重要。这涉及启动、停止、重新启动、启用、禁用和监视系统服务等任务。虽然管理 systemd 服务的方法有多种,但使用 systemctl、journalctl 等命令是最有效、最可靠的方法。

本文提供了有关利用 systemd 命令管理远程 Linux 服务器上的服务的综合教程。我们将介绍诸如启动和停止服务、检查其状态、在启动期间启用和禁用服务以及查看系统日志等命令。每个命令都附有其输出,以及相应的示例以增强理解。

第 1 步:连接到远程服务器

要使用 SSH 连接到远程服务器,请在本地计算机上打开一个终端,然后键入以下命令,然后将“username”替换为您自己的用户名,将“remote.server.com”替换为远程服务器使用的主机名或 IP 地址此命令−

ssh username@remote.server.com

输出可能如下所示 -

The authenticity of host 'remote.server.com (192.168.1.1)' can't be established.
RSA key fingerprint is 11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'remote.server.com,192.168.1.1' (RSA) to the list of known hosts.
username@remote.server.com's password:

使用正确的密码成功进行身份验证后,您将能够访问远程服务器并显示该特定服务器的命令终端或提示符。

第 2 步:检查服务的状态

一旦与远程服务器建立连接,就需要使用系统中的 systemctl status 命令检查任何服务的状态。要了解任何服务的状态,您只需在 Linux 终端或提示符中编写此命令 −

sudo systemctl status apache2

命令“sudo systemctl status apache2”将为您提供有关 Apache 服务当前状态的简要详细信息。

apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-03-15 14:30:00 UTC; 5min ago
   Main PID: 12345 (apache2)
      Tasks: 55 (limit: 2288)
     Memory: 67.0M
     CGroup: /system.slice/apache2.service
             ├─12345 /usr/sbin/apache2 -k start
             ├─12346 /usr/sbin/apache2 -k start
             └─12347 /usr/sbin/apache2 -k start
Mar 15 14:30:00 example.com systemd[1]: Started The Apache HTTP Server.

第三步:启动服务

要启动特定服务,请将“service_name”替换为该服务的实际名称。举例来说,如果您打算在远程服务器上启动 Apache Web 服务器,您可以在下面编写此输入命令 -

sudo systemctl start apache2

执行该命令后,将在远程服务器上启动Apache服务并显示相应的输出。

[sudo] password for <username>: 

注意:这是输入密码的提示。输入您的密码并按 Enter 键。如果密码正确,输出可能如下所示:−

Job for apache2.service started successfully

第 4 步:停止服务

如果服务正在运行并且您需要停止它,可以使用 systemctl stop 命令来执行此操作。此停止命令的语法如下 -

sudo systemctl stop apache2

输出可能如下所示 -

[sudo] password for user: 
[user@hostname ~]$ sudo systemctl stop apache2
[user@hostname ~]$

这将停止远程服务器上的 Apache 服务。

第5步:重新启动服务

如果您对服务进行了更改并需要重新启动它以使这些更改生效,则可以使用 systemctl restart 命令来执行此操作。此输入命令的语法如下:−

sudo systemctl restart apache2

它将允许您输入该用户的密码 -

[sudo] password for user: 

使用“sudo”命令以 root 用户身份以提升的权限运行“systemctl”命令时,终端会请求用户密码。这是重新启动 Apache Web 服务器所必需的,这需要管理访问权限。输出将如下所示 -

Restarting Apache2 web server... 

该空行表示 Apache Web 服务器正在重新启动。

该行是一个空行,它将输出与命令提示符分隔开。

请注意,实际输出可能会有所不同,具体取决于所使用的特定 Linux 发行版和版本。此外,如果重新启动 Apache Web 服务器时出现任何错误或问题,可能会显示其他输出以指示出了什么问题。

第 6 步:启用服务

如果希望某个服务在系统启动时自动启动,可以使用systemctl enable命令来启用它。此输入命令的语法如下:−

sudo systemctl enable apache2

此命令的输出将取决于 Apache2 服务的当前状态。如果该服务已启用,您可能会看到如下消息 -

Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.Executing: /lib/systemd/systemd-sysv-install enable apache2

如果服务尚未启用,输出可能如下所示 -

Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.

无论哪种方式,systemctl enable 命令都可以确保 Apache2 服务在系统启动时自动启动。

步骤7:检查服务日志

如果服务无法正常工作或者您遇到问题,可以使用journalctl 命令检查其日志。

这是输入命令的示例 -

sudo journalctl -u apache2

Journalctl 命令的输出 -

-- Logs begin at Mon 2023-03-13 00:00:00 UTC, end at Mon 2023-03-13 23:59:59 UTC. --
Mar 13 08:30:01 server systemd[1]: Starting The Apache HTTP Server...
Mar 13 08:30:01 server apachectl[12345]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
Mar 13 08:30:01 server systemd[1]: Started The Apache HTTP Server.
Mar 13 08:30:01 server systemd[1]: apache2.service: Main process exited, code=exited, status=0/SUCCESS
Mar 13 08:30:01 server systemd[1]: apache2.service: Succeeded.

第8步:退出远程服务器

在远程服务器上完成任务后,您可以通过键入以下命令退出 SSH 会话 -

exit

以下是终端输出的示例 −

me@local-machine:~$ ssh remote-server
Welcome to remote-server!
me@remote-server:~$ ls
file1.txt  file2.txt  file3.txt
me@remote-server:~$ exit
logout
Connection to remote-server closed.
me@local-machine:~$

这将关闭 SSH 连接并返回到本地计算机的命令提示符。

结论

总之,我们学习了如何使用 systemctl status、systemctl start、systemctl stop、systemctl enable 和 systemctl disable 等命令来管理远程 Linux 服务器上的 systemd 服务。这些命令可帮助您检查服务的状态、启动和停止它们以及启用/禁用启动时的自动启动。

通过以下步骤,我们可以高效地管理远程Linux服务器上的systemd服务并确保其顺利运行。此外,自动化服务启动和关闭可确保其持续可用性。总的来说,掌握控制远程 Linux 服务器上的 systemd 服务的能力是任何 Linux 系统用户的一项基本技能。

©2015-2025 Norria support@norria.com