网站搜索

如何将网络服务配置为开机自动启动


将基本网络服务配置为在启动时自动启动通常是一个好主意。这可以省去您在重新启动时手动启动它们的麻烦,而且如果您忘记这样做也会造成严重破坏。一些关键的网络服务包括SSHNTPhttpd

您可以通过运行以下命令来确认您的系统服务管理器是什么。

ps --pid 1

根据上述命令的输出,您将使用以下命令之一来配置每个服务是否应在启动时自动启动:

基于systemd
----------- Enable Service to Start at Boot -----------
systemctl enable [service]
----------- Prevent Service from Starting at Boot -----------
systemctl disable [service] # prevent [service] from starting at boot
基于 sysvinit 的
----------- Start Service at Boot in Runlevels A and B -----------
chkconfig --level AB [service] on 
-----------  Don’t Start Service at boot in Runlevels C and D -----------
chkconfig --level CD service off 

systemd 系统(如 CentOS 8RHEL 8Fedora 30+)上,systemctl Strong>命令用于管理服务。例如,要查看禁用的服务,请运行以下命令:

sudo systemctl list-unit-files --state=disabled
sudo chkconfig --list     [On sysvinit-based]

下面的输出打印出所有禁用的服务,如您所见,列出了 httpd 服务,这意味着它未配置为在启动时启动。

要使服务在启动时启动,请使用以下语法:

sudo systemctl enable service-name
sudo chkconfig service_name on     [On sysvinit-based] 

例如,在启动执行时启用 httpd 服务。

sudo systemctl enable httpd
sudo chkconfig httpd on     [On sysvinit-based] 

要确认 httpd 服务已启用,请通过执行以下命令列出所有已启用的服务:

sudo systemctl list-unit-files --state=enabled
sudo chkconfig --list | grep 3:on     [On sysvinit-based] 

从上面的输出中,我们可以清楚地看到 httpd 服务现在出现在启用的服务列表中。

要了解有关 systemctlchkconfig 命令的更多信息,请阅读以下文章:

  • 如何在 Linux 中使用“Systemctl”管理“Systemd”服务和单元
  • Linux 中的基本 chkconfig 命令示例