Linux如何在启动时启用和禁用服务
要管理Linux服务的开机启动,使用systemctl命令即可。1. 检查服务状态:systemctl status <service-name> 可查看服务是否运行、启用或禁用。2. 启用服务开机启动:sudo systemctl enable <service-name>,如sudo systemctl enable nginx,若同时启动则用sudo systemctl enable --now nginx。3. 禁用服务开机启动:sudo systemctl disable <service-name>,如sudo systemctl disable cups,若同时停止则用sudo systemctl disable --now cups。4. 查看已启用的服务:systemctl list-enabled,或用systemctl is-enabled <service-name> 检查特定服务状态,返回值为enabled、disabled、static或masked。5. 完全屏蔽服务:sudo systemctl mask <service-name> 可防止服务开机启动和手动启动,如sudo systemctl mask bluetooth,解除屏蔽用sudo systemctl unmask <service-name>。这些命令适用于大多数现代Linux发行版,包括Ubuntu、Debian、CentOS和Fedora,从而实现对开机服务的全面控制。
In Linux, managing services at boot time is typically done using systemd
, the init system used by most modern distributions. You can enable or disable services so they start (or don't start) automatically when the system boots.
Here’s how to enable and disable services at boot using common systemctl
commands.
1. Check the Current Status of a Service
Before enabling or disabling a service, it helps to check its current status:
systemctl status <service-name>
For example:
systemctl status apache2
This shows whether the service is active (running), enabled (starts at boot), or disabled.
2. Enable a Service to Start at Boot
To configure a service to start automatically when the system boots:
sudo systemctl enable <service-name>
Example:
sudo systemctl enable nginx
This creates a symbolic link from the system’s copy of the service file (usually in /etc/systemd/system/
or /usr/lib/systemd/system/
) into the appropriate systemd target (like multi-user.target.wants
), indicating it should be started at boot.
✅ Note: Enabling a service does not start it immediately — it only sets it to start on next boot.
If you want to enable and start it now:
sudo systemctl enable --now nginx
3. Disable a Service from Starting at Boot
To prevent a service from starting automatically at boot:
sudo systemctl disable <service-name>
Example:
sudo systemctl disable cups
This removes the symbolic link that tells systemd to start the service at boot.
⚠️ Disabling a service does not stop it if it's currently running. To also stop it now:
sudo systemctl disable --now cups
4. Check All Enabled Services
To list all services that are currently enabled to start at boot:
systemctl list-enabled
Or filter for a specific service:
systemctl is-enabled <service-name>
This returns enabled
, disabled
, static
(a service that can't be enabled but is used as a dependency), or masked
.
5. Mask a Service (Optional: Prevent Manual Start Too)
If you want to completely block a service from being started — even manually — you can mask it:
sudo systemctl mask <service-name>
Example:
sudo systemctl mask bluetooth
This creates a symlink to /dev/null
, making the service configuration inaccessible.
To unmask it later:
sudo systemctl unmask <service-name>
Summary of Key Commands
Action | Command |
---|---|
Enable at boot | sudo systemctl enable <service></service> |
Enable and start now | sudo systemctl enable --now <service></service> |
Disable at boot | sudo systemctl disable <service></service> |
Disable and stop now | sudo systemctl disable --now <service></service> |
Check if enabled | systemctl is-enabled <service></service> |
Mask service (fully block) | sudo systemctl mask <service></service> |
Unmask service | sudo systemctl unmask <service></service> |
Most distributions like Ubuntu, Debian, CentOS, Fedora, and others use systemd
, so these commands work across them. If you're on an older system using SysVinit, you'd use update-rc.d
(Debian) or chkconfig
(RHEL/CentOS 6), but those are largely outdated.
Basically, with systemctl enable
and disable
, you have full control over what runs at startup.
以上是Linux如何在启动时启用和禁用服务的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undress AI Tool
免费脱衣服图片

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

在Linux系统中,1.使用ipa或hostname-I命令可查看私有IP;2.使用curlifconfig.me或curlipinfo.io/ip可获取公网IP;3.桌面版可通过系统设置查看私有IP,浏览器访问特定网站查看公网IP;4.可将常用命令设为别名以便快速调用。这些方法简单实用,适合不同场景下的IP查看需求。

要让PHP容器支持自动构建,核心在于配置持续集成(CI)流程。1.使用Dockerfile定义PHP环境,包括基础镜像、扩展安装、依赖管理和权限设置;2.配置GitLabCI等CI/CD工具,通过.gitlab-ci.yml文件定义build、test和deploy阶段,实现自动构建、测试和部署;3.集成PHPUnit等测试框架,确保代码变更后自动运行测试;4.使用Kubernetes等自动化部署策略,通过deployment.yaml文件定义部署配置;5.优化Dockerfile,采用多阶段构

LinuxCanrunonModestHardwarewtareWithSpecificminimumRequirentess.A1GHZPROCESER(X86ORX86_64)iSNEDED,withAdual-Corecpurecommondend.r AmshouldBeatLeast512MbForCommand-lineUseor2Gbfordesktopenvironments.diskSpacePacereQuiresaminimumof5-10GB,不过25GBISBISBETTERFORAD

比特币作为数字世界的先驱,其独特的代号和底层技术一直是人们关注的焦点。它的标准代号是 BTC,在某些符合国际标准的平台上也被称为 XBT。从技术角度看,比特币并非单一的代码样式,而是一个庞大且精密的开源软件项目,其核心代码主要由 C 语言编写,并融合了密码学、分布式系统和经济学原理,任何人都可以查看、审查和贡献其代码。

Linux/macOS的shutdown命令可通过参数实现关机、重启、定时操作等。1.立即关机使用sudoshutdownnow或-h/-P参数;2.定时关机用 时间或具体时间点,取消用-c;3.重启使用-r参数,支持定时重启;4.注意需sudo权限,远程操作谨慎,避免数据丢失。

搭建独立PHP任务容器环境可通过Docker实现,具体步骤如下:1.安装Docker与DockerCompose作为基础;2.创建独立目录存放Dockerfile、crontab文件;3.编写Dockerfile定义PHPCLI环境并安装cron及必要扩展;4.编写crontab文件定义定时任务;5.编写docker-compose.yml挂载脚本目录并配置环境变量;6.启动容器并验证日志。相比Web容器内执行定时任务,独立容器具备资源隔离、环境纯粹、稳定性强、便于扩展等优势。为确保日志与错误捕

确认目标硬盘设备名(如/dev/sda),避免误删系统盘;2.使用sudoddif=/dev/zeroof=/dev/sdXbs=1Mstatus=progress全盘覆写零值,适用于大多数场景;3.敏感数据使用sudoshred-v-n3/dev/sdX进行三次随机数据覆写,确保无法恢复;4.可选执行sudobadblocks-wsv/dev/sdX做破坏性写入测试;最后用sudohexdump-C/dev/sdX|head验证是否全为零,完成安全擦除。

在Linux中添加用户常用useradd或adduser命令。1.使用useradd时需手动设置密码和家目录,加-m参数可创建家目录;2.可通过-s、-G、-u等参数指定shell、组和UID;3.adduser是交互式命令,适合新手自动完成配置;4.注意权限、用户名唯一性和家目录权限问题;5.误操作可用userdel删除用户及家目录。掌握这些要点可高效安全地管理用户。
