"systemctl start"和"systemctl enable"之间有什么区别?

我在我的机器上安装了MariaDB服务器。在设置过程中,我遇到了一个问题,即是否应该始终启用它,因为我遵循的文档建议采取以下步骤:
sudo yum install mariadb mariadb-server 
sudo systemctl start mariadb.service  
sudo systemctl enable mariadb.service

8你还可以使用enable子命令的--now开关来同时启用和启动服务,例如:systemctl enable sshd --now。同样,disable命令也可用于停止服务。 - user359096
3个回答

systemctl startsystemctl enable 执行不同的操作。

enable 会将指定的单元关联到相关位置,使其在启动时自动启动,或在插入相关硬件时启动,或根据单元文件中指定的其他情况进行启动。

start 立即启动该单元。

disablestop 分别是这些操作的相反操作。

这意味着当您首次安装 MariaDB 时,您可能希望运行 systemctl enable mariadb.service 以启用它,使其在启动时自动启动。您还可以运行 systemctl start mariadb.service 或者只需重新启动来启动 MariaDB。要停止 MariaDB,请运行 systemctl stop mariadb.service(它将在下次启动时或手动启动时再次启动)。要禁用它以使其不再在启动时启动,请运行 systemctl disable mariadb.service

更新:

如gerardw的回答中所述,从2015年5月发布的版本220开始,enabledisable都开始接受可选的--now开关,以便根据使用的命令启动或停止单元。
要使用同一命令同时禁用和停止一个单元,请使用systemctl disable mariadb.service --now。同样地,要同时启用和启动一个单元,请使用systemctl enable mariadb.service --now
来源:systemctl手册页

13请提供您要翻译的具体文本。 - sunleo
那么我们什么时候应该输入systemctl enable呢?每次我们想要运行systemd服务的时候吗? - JobHunter69
2不,当你想让它自动启动时。如果你想停止它自动启动,那么运行systemctl disable - vurp0
这意味着服务是以root身份启动的吗?我如何在启动时以其他用户身份启动服务? - Chaminda Bandara
@ChamindaBandara 使用--user选项。 - Samuel

来自 systemctl 手册

enable NAME...
   Enable one or more unit files or unit file instances, as specified
   on the command line. This will create a number of symlinks as
   encoded in the "[Install]" sections of the unit files. After the
   symlinks have been created, the systemd configuration is reloaded
   (in a way that is equivalent to daemon-reload) to ensure the
   changes are taken into account immediately. Note that this does not
   have the effect of also starting any of the units being enabled. If
   this is desired, either --now should be used together with this
   command, or an additional start command must be invoked for the
   unit.
   ...
   Enabling units should not be confused with starting (activating)
   units, as done by the start command. Enabling and starting units is
   orthogonal: units may be enabled without being started and started
   without being enabled. Enabling simply hooks the unit into various
   suggested places (for example, so that the unit is automatically
   started on boot or when a particular kind of hardware is plugged
   in). Starting actually spawns the daemon process (in case of
   service units), or binds the socket (in case of socket units), and
   so on.

基本上,enable 标记了服务在启动时启动,而 start 实际上立即启动了该服务。

我们应该在什么时候启用?我们只需要做一次吗? - JobHunter69
@Goldname 第一次需要启动时,您可以将其设置为在下次启动时自动启用。只需一次即可。 - muru
当我们编辑它时怎么办?我们需要重新启用吗? - JobHunter69
@Goldname 你不需要重新启用它。 - muru
谢谢,如果文件被移动到不同的目录和/或系统重新启动,情况也是一样的吗? - JobHunter69

从systemctl版本220开始,enabledisable支持--now开关,可以在启用/禁用服务的同时并行启动/停止服务。
例如:systemctl --now enable foobar.service 使用systemctl --version检查您安装的版本。

1我刚刚在CentOS 7上的systemd 219中成功地使用了这个。 - mwfearnley
systemd的github存储库显示为220,但也许CentOS已经反向移植了更新。https://github.com/systemd/systemd/blob/dd95b381b26369c02a3ca03585184cb2ea17295b/NEWS#L3043-L3046 - gerardw