目錄
2. Enable a Service to Start at Boot
3. Disable a Service from Starting at Boot
4. Check All Enabled Services
5. Mask a Service (Optional: Prevent Manual Start Too)
Summary of Key Commands
首頁 系統教程 Linux Linux如何在啟動時啟用和禁用服務

Linux如何在啟動時啟用和禁用服務

Aug 08, 2025 am 10:23 AM
linux 服務管理

要管理Linux服務的開機啟動,使用systemctl命令即可。 1. 檢查服務狀態:systemctl status 可查看服務是否運行、啟用或禁用。 2. 啟用服務開機啟動:sudo systemctl enable ,如sudo systemctl enable nginx,若同時啟動則用sudo systemctl enable --now nginx。 3. 禁用服務開機啟動:sudo systemctl disable ,如sudo systemctl disable cups,若同時停止則用sudo systemctl disable --now cups。 4. 查看已啟用的服務:systemctl list-enabled,或用systemctl is-enabled 檢查特定服務狀態,返回值為enabled、disabled、static或masked。 5. 完全屏蔽服務:sudo systemctl mask 可防止服務開機啟動和手動啟動,如sudo systemctl mask bluetooth,解除屏蔽用sudo systemctl unmask 。這些命令適用於大多數現代Linux發行版,包括Ubuntu、Debian、CentOS和Fedora,從而實現對開機服務的全面控制。

Linux how to enable and disable services at boot

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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Linux如何在啟動時啟用和禁用服務 Linux如何在啟動時啟用和禁用服務 Aug 08, 2025 am 10:23 AM

要管理Linux服務的開機啟動,使用systemctl命令即可。 1.檢查服務狀態:systemctlstatus可查看服務是否運行、啟用或禁用。 2.啟用服務開機啟動:sudosystemctlenable,如sudosystemctlenablenginx,若同時啟動則用sudosystemctlenable--nownginx。 3.禁用服務開機啟動:sudosystemctldisable,如sudosystemctldisablecups,若同時停止則用sudosystemctldisabl

了解Linux服務器上的RAID配置 了解Linux服務器上的RAID配置 Aug 05, 2025 am 11:50 AM

RAIDimprovesstorageperformanceandreliabilityonLinuxserversthroughvariousconfigurations;RAID0offersspeedbutnoredundancy;RAID1providesmirroringforcriticaldatawith50�pacityloss;RAID5supportssingle-drivefailuretoleranceusingparityandrequiresatleastthre

Linux如何查看文件的內容 Linux如何查看文件的內容 Aug 19, 2025 pm 06:44 PM

ToviewfilecontentsinLinux,usedifferentcommandsbasedonyourneeds:1.Forsmallfiles,usecattodisplaytheentirecontentatonce,withcat-ntoshowlinenumbers.2.Forlargefiles,uselesstoscrollpagebypageorlinebyline,searchwith/search_term,andquitwithq.3.Usemoreforbasi

Linux如何列出所有運行過程 Linux如何列出所有運行過程 Aug 08, 2025 am 06:42 AM

Usepsauxforacompletesnapshotofallrunningprocesses,showingdetailedinformationlikeUSER,PID,CPU,andmemoryusage.2.Usetoporhtopforreal-timemonitoringofprocesseswithdynamicupdates,wherehtopoffersamoreintuitiveinterface.3.UsepgreporpidoftoquicklyfindthePIDs

如何清理Linux系統 如何清理Linux系統 Aug 22, 2025 am 07:42 AM

Removeunusedpackagesanddependencieswithsudoaptautoremove,cleanpackagecacheusingsudoaptcleanorautoclean,andremoveoldkernelsviasudoaptautoremove--purge.2.Clearsystemlogswithsudojournalctl--vacuum-time=7d,deletearchivedlogsin/var/log,andempty/tmpand/var

如何在Linux中創建一個別名 如何在Linux中創建一個別名 Aug 19, 2025 pm 08:13 PM

在Linux中設置alias的步驟如下:1.臨時設置使用alias命令如aliasll='ls-la';2.永久設置需編輯shell配置文件如~/.bashrc添加別名後執行source生效;3.注意避免覆蓋原命令且不同shell配置獨立。別名能簡化複雜命令提升效率但僅在當前shell環境生效關閉終端后失效需合理定義並定期檢查配置。

了解Linux文件系統層次結構標準(FHS) 了解Linux文件系統層次結構標準(FHS) Aug 06, 2025 pm 04:23 PM

/bin和/sbin存放基本命令和系統管理命令;2./usr存放用戶程序及相關資源;3./etc是配置文件目錄;4./var存放可變數據如日誌和緩存;5./home和/root是普通用戶和root用戶的主目錄;6./tmp和/run用於臨時文件和運行時數據;7./dev、/proc、/sys提供設備和系統信息接口;8./lib和/lib64包含系統啟動所需庫文件;9./opt和/srv分別用於第三方軟件和服務數據;FHS通過標準化目錄結構提升系統管理效率,使Linux文件佈局清晰一致,便於維護和

​​192.168.10.1登錄入口(極路由/華碩管理頁面)​​ ​​192.168.10.1登錄入口(極路由/華碩管理頁面)​​ Aug 12, 2025 pm 10:18 PM

首先確認設備已連接目標路由器的Wi-Fi或通過網線連接;2.在瀏覽器地址欄輸入http://192.168.10.1並回車;3.輸入正確的用戶名和密碼(默認常為admin/admin或查看路由器背面);4.若無法打開,檢查是否連錯網絡、確認正確IP地址(可通過ipconfig查看默認網關)、重啟路由器、更換瀏覽器或關閉防火牆;5.登錄後可修改Wi-Fi名稱密碼、設置安全選項、配置端口轉發、啟用訪客網絡、升級固件等;6.若忘記密碼,可通過按住路由器重置孔5-10秒恢復出廠設置,但會清除所有配置,需

See all articles