systemctl enable --now:设置开机自启并立即启动
用一条命令让 systemd 服务开机自启,并立刻把它启动起来。
运行 sudo systemctl enable --now nginx,同时创建开机自启链接并立即启动服务;撤销时用 sudo systemctl disable --now nginx。
01命令
命令
$ sudo systemctl enable --now nginx02作用说明
enable 会创建把单元挂入启动流程的符号链接,但它本身并不会启动服务——刚启用的服务会一直保持停止状态,直到下次重启或手动 start。--now 参数正好解决这个坑:启用之后马上启动。完全相反的操作是 disable --now:移除开机自启链接并停止服务。用 systemctl status <单元> 验证:显示 enabled 表示会开机自启,显示 active (running) 表示正在运行。
03风险
!注意: 启用不等于启动,启动也不等于启用——只执行 enable 或 start 之一而忘了 --now,是服务"当时能用、重启后消失"的最常见原因。
04撤销
撤销
$ sudo systemctl disable --now nginx05来源
- Ubuntu Manpage: systemctl - Control the systemd system and service manager依据: "enable NAME... — Enable one or more units ... This will ... create symlinks ... hooking the specified units into various places"; "--now — When used with enable, the units will also be started. When used with disable ... the units will also be stopped."
- systemctl(1) — Arch Linux manual依据: "--now — When used with enable, the units will also be started. When used with disable or mask, the units will also be stopped." enable "hook[s] the specified units into various places" via symlinks.
最后验证: 2026-09-19 · 分类: 系统与设置