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

RAIDimprovesstorageperformanceandreliabilityonLinuxserversthroughvariousconfigurations;RAID0offersspeedbutnoredundancy;RAID1providesmirroringforcriticaldatawith50�pacityloss;RAID5supportssingle-drivefailuretoleranceusingparityandrequiresatleastthre

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

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

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

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

/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文件佈局清晰一致,便於維護和

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