亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
What Are Systemd Services?
Common Service Management Commands
Anatomy of a Service File
What Are Systemd Targets?
Managing and Switching Targets
Understanding Target Dependencies
Practical Example: Creating a Custom Service
Key Takeaways
首頁(yè) 系統(tǒng)教程 Linux 了解Linux SystemD服務(wù)和目標(biāo)

了解Linux SystemD服務(wù)和目標(biāo)

Jul 26, 2025 am 12:59 AM

systemd服務(wù)和目標(biāo)是Linux系統(tǒng)管理的核心,服務(wù)通過(guò).service文件管理進(jìn)程,使用systemctl命令控制啟停、啟用禁用,如systemctl start sshd;目標(biāo)代表系統(tǒng)運(yùn)行狀態(tài),如multi-user.target或graphical.target,通過(guò)systemctl set-default和isolate切換,默認(rèn)目標(biāo)可設(shè)置,服務(wù)文件包含[Unit]、[Service]、[Install]三部分,WantedBy指定目標(biāo)依賴,自定義服務(wù)需創(chuàng)建.service文件並執(zhí)行systemctl daemon-reload使配置生效。

Understanding Linux Systemd Services and Targets

Linux systemd is the init system and service manager used by most modern Linux distributions. It controls how services start, stop, and behave during system boot and runtime. Understanding services and targets in systemd is essential for managing system behavior effectively.

Understanding Linux Systemd Services and Targets

What Are Systemd Services?

A service in systemd represents a unit that manages a specific process or daemon—like a web server ( httpd ), SSH server ( sshd ), or database ( mariadb ). Services define how and when a program should run.

Service files (ending in .service ) are usually located in:

Understanding Linux Systemd Services and Targets
  • /usr/lib/systemd/system/ – for default system-provided services
  • /etc/systemd/system/ – for user-defined or overridden services

Common Service Management Commands

You can control services using the systemctl command:

 # Start a service immediately
sudo systemctl start sshd.service

# Stop a running service
sudo systemctl stop sshd.service

# Enable a service to start at boot
sudo systemctl enable sshd.service

# Disable a service from starting at boot
sudo systemctl disable sshd.service

# Check the status of a service
systemctl status sshd.service

# Restart or reload a service
sudo systemctl restart sshd.service
sudo systemctl reload sshd.service # if supported

Note: The .service suffix is optional. You can just type sshd instead of sshd.service .

Understanding Linux Systemd Services and Targets

Anatomy of a Service File

A typical .service file looks like this:

 [Unit]
Description=OpenSSH Server
After=network.target

[Service]
Type=notify
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target

Key sections:

  • [Unit] : Defines metadata and dependencies (eg, what must start before this).
  • [Service] : Specifies how the service should run (start command, restart policy, etc.).
  • [Install] : Controls how the service is enabled/disabled (used with enable / disable ).

What Are Systemd Targets?

Targets in systemd are like runlevels in the old SysV init system, but more flexible. They represent a state or mode the system should be in (eg, multi-user, graphical, rescue).

Common targets include:

Target Description
poweroff.target Shuts down the system
rescue.target Single-user mode with minimal services
multi-user.target Multi-user text mode (no GUI)
graphical.target Multi-user with GUI (desktop environment)
reboot.target Reboots the system

Managing and Switching Targets

To see the current target:

 systemctl get-default

To change the default boot target:

 # Set system to boot into text mode by default
sudo systemctl set-default multi-user.target

# Set system to boot into GUI by default
sudo systemctl set-default graphical.target

To switch to a target immediately (without rebooting):

 # Switch to single-user/rescue mode
sudo systemctl isolate rescue.target

# Switch to multi-user mode
sudo systemctl isolate multi-user.target

isolate stops services not part of the target and starts those that are—similar to switching runlevels.

Understanding Target Dependencies

Targets use .target files to group other units. For example, graphical.target typically pulls in multi-user.target and display manager services.

You can see what's included in a target:

 systemctl list-dependencies graphical.target

And check which target a service belongs to via the [Install] section ( WantedBy= ).


Practical Example: Creating a Custom Service

Let's say you want a script to run at boot.

  1. Create a script:

     sudo nano /opt/myscript.sh
     #!/bin/bash
    echo "Custom service running at $(date)" >> /var/log/myscript.log
  2. Make it executable:

     sudo chmod x /opt/myscript.sh
  3. Create a service file:

     sudo nano /etc/systemd/system/myscript.service
     [Unit]
    Description=My Custom Script
    After=network.target
    
    [Service]
    Type=oneshot
    ExecStart=/opt/myscript.sh
    RemainAfterExit=yes
    
    [Install]
    WantedBy=multi-user.target
  4. Reload systemd and enable the service:

     sudo systemctl daemon-reexec
    sudo systemctl enable myscript.service

    Now it will run at every boot.


    Key Takeaways

    • Services manage daemons and processes; use systemctl to control them.
    • Targets define system states, replacing old runlevels.
    • Use WantedBy= in service files to tie services to specific targets.
    • Custom services can be created easily in /etc/systemd/system/ .
    • Always run systemctl daemon-reload after creating or editing service files.

    Basically, systemd makes service and boot management more consistent and dependency-aware than older init systems. Once you understand services and targets, you're well on your way to mastering Linux system administration.

    以上是了解Linux SystemD服務(wù)和目標(biāo)的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

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

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開(kāi)發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

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

熱門(mén)話題

Laravel 教程
1597
29
PHP教程
1488
72
在RHEL,Rocky和Almalinux中安裝LXC(Linux容器) 在RHEL,Rocky和Almalinux中安裝LXC(Linux容器) Jul 05, 2025 am 09:25 AM

LXD被描述為下一代容器和虛擬機(jī)管理器,它為在容器內(nèi)部或虛擬機(jī)中運(yùn)行的Linux系統(tǒng)提供了沉浸式的。 它為有支持的Linux分佈數(shù)量提供圖像

如何在Linux機(jī)器上解決DNS問(wèn)題? 如何在Linux機(jī)器上解決DNS問(wèn)題? Jul 07, 2025 am 12:35 AM

遇到DNS問(wèn)題時(shí)首先要檢查/etc/resolv.conf文件,查看是否配置了正確的nameserver;其次可手動(dòng)添加如8.8.8.8等公共DNS進(jìn)行測(cè)試;接著使用nslookup和dig命令驗(yàn)證DNS解析是否正常,若未安裝這些工具可先安裝dnsutils或bind-utils包;再檢查systemd-resolved服務(wù)狀態(tài)及其配置文件/etc/systemd/resolved.conf,並根據(jù)需要設(shè)置DNS和FallbackDNS後重啟服務(wù);最後排查網(wǎng)絡(luò)接口狀態(tài)與防火牆規(guī)則,確認(rèn)53端口未

您將如何調(diào)試速度慢或使用高內(nèi)存使用量的服務(wù)器? 您將如何調(diào)試速度慢或使用高內(nèi)存使用量的服務(wù)器? Jul 06, 2025 am 12:02 AM

發(fā)現(xiàn)服務(wù)器運(yùn)行緩慢或內(nèi)存佔(zhàn)用過(guò)高時(shí),應(yīng)先排查原因再操作。首先要查看系統(tǒng)資源使用情況,用top、htop、free-h、iostat、ss-antp等命令檢查CPU、內(nèi)存、磁盤(pán)I/O和網(wǎng)絡(luò)連接;其次分析具體進(jìn)程問(wèn)題,通過(guò)ps、jstack、strace等工具追蹤高佔(zhàn)用進(jìn)程的行為;接著檢查日誌和監(jiān)控?cái)?shù)據(jù),查看OOM記錄、異常請(qǐng)求、慢查詢等線索;最後根據(jù)常見(jiàn)原因如內(nèi)存洩漏、連接池耗盡、緩存失效風(fēng)暴、定時(shí)任務(wù)衝突進(jìn)行針對(duì)性處理,優(yōu)化代碼邏輯,設(shè)置超時(shí)重試機(jī)制,加限流熔斷,並定期壓測(cè)評(píng)估資源。

在Ubuntu中安裝用於遠(yuǎn)程Linux/Windows訪問(wèn)的鱷梨調(diào)味醬 在Ubuntu中安裝用於遠(yuǎn)程Linux/Windows訪問(wèn)的鱷梨調(diào)味醬 Jul 08, 2025 am 09:58 AM

作為系統(tǒng)管理員,您可能會(huì)發(fā)現(xiàn)自己(今天或?qū)?lái))在Windows和Linux並存的環(huán)境中工作。 有些大公司更喜歡(或必須)在Windows Box上運(yùn)行其一些生產(chǎn)服務(wù)已不是什麼秘密

如何使用Brasero在Linux中燃燒CD/DVD 如何使用Brasero在Linux中燃燒CD/DVD Jul 05, 2025 am 09:26 AM

坦率地說(shuō),我不記得上一次使用CD/DVD驅(qū)動(dòng)器的PC。這要?dú)w功於不斷發(fā)展的科技行業(yè),該行業(yè)已被USB驅(qū)動(dòng)器和其他較小且緊湊的存儲(chǔ)媒體所取代,這些磁盤(pán)可提供更多存儲(chǔ)

如何在Linux中找到我的私人和公共IP地址? 如何在Linux中找到我的私人和公共IP地址? Jul 09, 2025 am 12:37 AM

在Linux系統(tǒng)中,1.使用ipa或hostname-I命令可查看私有IP;2.使用curlifconfig.me或curlipinfo.io/ip可獲取公網(wǎng)IP;3.桌面版可通過(guò)系統(tǒng)設(shè)置查看私有IP,瀏覽器訪問(wèn)特定網(wǎng)站查看公網(wǎng)IP;4.可將常用命令設(shè)為別名以便快速調(diào)用。這些方法簡(jiǎn)單實(shí)用,適合不同場(chǎng)景下的IP查看需求。

如何在Rocky Linux 8上安裝Nodejs 14/16&npm 如何在Rocky Linux 8上安裝Nodejs 14/16&npm Jul 13, 2025 am 09:09 AM

Node.js建立在Chrome的V8引擎上,是一種開(kāi)源的,由事件驅(qū)動(dòng)的JavaScript運(yùn)行時(shí)環(huán)境,用於構(gòu)建可擴(kuò)展應(yīng)用程序和後端API。 Nodejs因其非阻滯I/O模型而聞名輕巧有效,並且

如何在RHEL,Rocky和Almalinux中設(shè)置MySQL複製 如何在RHEL,Rocky和Almalinux中設(shè)置MySQL複製 Jul 05, 2025 am 09:27 AM

數(shù)據(jù)複製是將數(shù)據(jù)複製到多個(gè)服務(wù)器中以提高數(shù)據(jù)可用性並增強(qiáng)應(yīng)用程序的可靠性和性能的過(guò)程。在mySQL複製中,數(shù)據(jù)從主服務(wù)器的數(shù)據(jù)庫(kù)複製到OT

See all articles