Linux服務(wù)器Systemctl命令完全使用指南
以下是 systemctl 使用指南,涵蓋服務(wù)管理、單元操作、運(yùn)行級別控制、電源管理及常用示例,幫助您高效管理 Linux 系統(tǒng)服務(wù)和進(jìn)程。
1. 基本語法
systemctl [選項(xiàng)...] 命令 [服務(wù)名|.target|.mount等]
- 選項(xiàng):可選參數(shù),如
--quiet(靜默模式)、--user(用戶級服務(wù))。 - 命令:操作類型(如
start、stop、enable)。 - 服務(wù)名:目標(biāo)服務(wù)名稱(如
nginx.service)。
2. 基礎(chǔ)命令速查表
| 命令 | 作用 | 示例 |
|---|---|---|
| 啟動指定服務(wù) |
|
| 停止指定服務(wù) |
|
| 重啟服務(wù)(先停止再啟動) |
|
| 重新加載配置(不中斷服務(wù),更輕量) |
|
| 僅當(dāng)服務(wù)運(yùn)行時才重啟,否則不操作 |
|
systemctl status | 查看服務(wù)詳細(xì)狀態(tài) | systemctl status nginx |
| 配置服務(wù)開機(jī)自啟 |
|
| 禁用開機(jī)自啟(仍可手動啟動) |
|
| 立即啟動并配置開機(jī)自啟(合并操作) |
|
systemctl is-active <服務(wù)名> | 僅檢查服務(wù)是否活躍 | systemctl is-active nginx |
systemctl is-enabled <服務(wù)名> | 檢查服務(wù)是否開機(jī)自啟 | systemctl is-enabled nginx |
| 徹底禁用服務(wù)(禁止手動 / 自動啟動) |
|
| 取消徹底禁用 |
|
| systemctl list-units | 列出當(dāng)前活躍的所有單元(默認(rèn)) | |
| systemctl list-units --all | 列出所有單元(包括未運(yùn)行、失敗的) | |
| systemctl list-units --type=service | 僅顯示服務(wù)單元 | |
| systemctl list-units --type=timer | 僅顯示定時器單元 | |
| systemctl list-unit-files | 列出所有單元文件 | |
systemctl cat <服務(wù)名> | 查看單元文件內(nèi)容(如服務(wù)的配置腳本) | systemctl cat nginx.service |
systemctl show <服務(wù)名> | 查看單元的詳細(xì)屬性(配置、依賴等) | systemctl show sshd.service |
systemctl list-dependencies <服務(wù)名> | 查看<服務(wù)名>依賴的服務(wù) | systemctl list-dependencies nginx.service |
systemctl list-dependencies --reverse <服務(wù)名> | 查看依賴<服務(wù)名>的服務(wù) | systemctl list-dependencies --reverse nginx.service |
systemctl reboot | 重啟系統(tǒng) | |
systemctl poweroff | 關(guān)機(jī)并切斷電源 | |
systemctl halt | 關(guān)閉系統(tǒng)(與 poweroff 功能類似) | |
systemctl suspend | 進(jìn)入休眠(數(shù)據(jù)存內(nèi)存,需持續(xù)供電) | |
systemctl hibernate | 進(jìn)入休眠(數(shù)據(jù)存硬盤,可斷電) | |
systemctl rescue | 進(jìn)入救援模式(單用戶,用于修復(fù)) | |
systemctl emergency | 進(jìn)入緊急模式(最小環(huán)境,極端修復(fù)) | |
| systemctl get-default | 查看當(dāng)前系統(tǒng)目標(biāo) | |
| systemctl isolate <目標(biāo)名>.target | 切換系統(tǒng)目標(biāo) | (切換到圖形界面)
|
| systemctl set-default <目標(biāo)名>.target | 設(shè)置默認(rèn)啟動目標(biāo) | (設(shè)置為多用戶模式) systemctl set-default multi-user.target |
PS:
# 查看系統(tǒng)啟動耗時 systemd-analyze # 查看每個服務(wù)啟動的耗時 systemd-analyze blame
3. 常用示例
3.1 部署新服務(wù)后,設(shè)置開機(jī)自啟并啟動
systemctl enable myservice --now
3.2 檢查系統(tǒng)中所有失敗的服務(wù)并嘗試修復(fù)
systemctl list-units --failed --type=service systemctl reset-failed # 重置失敗狀態(tài) systemctl restart 失敗的服務(wù)名
3.3 查看系統(tǒng)中所有開機(jī)自啟的服務(wù)
systemctl list-unit-files --type=service | grep enabled
4. 總結(jié)
systemctl 是 Linux 系統(tǒng)管理的核心工具,掌握以下技能將極大提升您的效率:
- 服務(wù)管理:啟動、停止、重啟、設(shè)置開機(jī)自啟。
- 單元操作:管理服務(wù)、掛載點(diǎn)、定時任務(wù)等。
- 運(yùn)行級別控制:切換
target實(shí)現(xiàn)多用戶/圖形界面切換。 - 電源管理:關(guān)機(jī)、重啟、掛起等操作。
- 故障排查:日志查看、服務(wù)調(diào)試、自定義服務(wù)配置。
到此這篇關(guān)于Linux服務(wù)器Systemctl命令完全使用指南的文章就介紹到這了,更多相關(guān)Linux服務(wù)器Systemctl命令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- linux中systemctl詳細(xì)理解及常用命令解析
- 使用Systemctl列出Linux中所有服務(wù)的操作步驟
- Linux?命令?systemctl?基本介紹和常用選項(xiàng)
- linux?系統(tǒng)進(jìn)程管理工具systemd詳解(systemctl命令、創(chuàng)建自己的systemd服務(wù))
- 詳解Linux 服務(wù)管理兩種方式service和systemctl
- linux &、nohup與Systemctl的使用
- 使用 chkconfig 和 systemctl 命令啟用或禁用 Linux 服務(wù)的方法
- linux systemctl命令詳解
- Linux系統(tǒng)中systemctl命令詳解
相關(guān)文章
Linux系統(tǒng)中sudo命令的十個技巧總結(jié)
Linux 下使用Sudo 命令,可以讓普通用戶也能執(zhí)行一些或者全部的root命令,下面這篇文章主要給大家介紹了關(guān)于Linux系統(tǒng)中sudo命令的十個技巧,需要的朋友可以參考借鑒,下面來一起看看吧。2018-04-04
Linux系統(tǒng)網(wǎng)卡設(shè)置教程
這篇文章主要介紹了Linux系統(tǒng)網(wǎng)卡的設(shè)置教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
Linux系統(tǒng)下安裝rz/sz命令及使用方法說明
這篇文章主要介紹了Linux系統(tǒng)下安裝rz/sz命令及使用方法說明,需要的朋友可以參考下2016-10-10
Linux網(wǎng)絡(luò)配置和監(jiān)控命令總結(jié)
本文介紹了Linux系統(tǒng)中常用的網(wǎng)絡(luò)配置和監(jiān)控命令,包括ifconfig、hostname、route、netstat、ss、lsof、ping、traceroute、nslookup等命令的使用方法和作用,適用于系統(tǒng)管理員在日常管理和故障排查中的網(wǎng)絡(luò)操作2024-09-09

