如何在远程 Linux 服务器上控制 Systemd 服务?
作为 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.
步骤 3:启动服务
要启动特定服务,请将"service_name"替换为该服务的实际名称。例如,如果您要在远程服务器上启动 Apache Web 服务器,则可以输入以下命令:
sudo systemctl start apache2
执行此命令后,Apache 服务将在远程服务器上启动,并显示相应的输出。
[sudo] password for <username>:
注意:这是提示您输入密码。输入您的密码并按 Enter 键。如果密码正确,输出可能如下所示:−
apache2.service 的作业已成功启动
步骤 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 服务器是必要的,因为重启 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 系统用户必备的技能。
相关文章
有用资源
linux 参考教程 - 该教程包含有关 linux 的更多信息:https://www.cainiaomax.com/linux/

