新聞中心
systemd是Linux系統(tǒng)最新的初始化系統(tǒng)(init),作用是提高系統(tǒng)的啟動速度,盡可能啟動較少的進程,盡可能更多進程并發(fā)啟動,下面為大家詳細講解一下linux中systemctl使用方法。

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的裕民網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
一、運行級別的分類 runlevel
-
運行級別0:系統(tǒng)停機狀態(tài)
-
運行級別1:單用戶工作狀態(tài),root權(quán)限,用于系統(tǒng)維護,禁止遠程登陸
-
運行級別2:多用戶狀態(tài)(沒有NFS)
-
運行級別3:完全的多用戶狀態(tài)(有NFS)
-
運行級別4:系統(tǒng)未使用,保留
-
運行級別5:X11控制臺
-
運行級別6:系統(tǒng)正常關(guān)閉并重啟
二、管理服務(wù) systemctl (root權(quán)限)
-
systemctl [opt] xxx.service
-
status 查看當(dāng)前服務(wù)狀態(tài)
-
start 啟動服務(wù)
-
stop 關(guān)閉服務(wù)
-
restart 重啟服務(wù)
-
enable 設(shè)置開機啟動
-
disable 設(shè)置開機不啟動
-
reload 后面不接具體服務(wù)名,重新加載配置文件
-
mask 注銷服務(wù)
-
unmask 取消注銷
三.一些常用命令 systemctl
-
查看當(dāng)前已經(jīng)啟動的服務(wù) systemctl list-units
-
查看所有服務(wù) systemctl list-unit-files
-
查看服務(wù)有哪些依賴 systemctl list-dependencies xx.service
-
查看服務(wù)有哪些依賴(反向) systemctl list-dependencies –reverse xx.service
四.system 服務(wù)相關(guān)的一些目錄( Centos 環(huán)境,Debian 類的環(huán)境可能會有稍許不同)
-
/usr/lib/systemd/system/ 系統(tǒng)安裝的軟件默認啟動腳本目錄
-
/etc/systemd/system/ 用戶根據(jù)自己需要建立的啟動腳本目錄
-
/etc/sysconfig/ 服務(wù)初始化選項目錄
-
/var/lib/ 服務(wù)運行時產(chǎn)生的數(shù)據(jù)存儲目錄
-
/etc/xxx/ 各服務(wù)配置目錄
五.結(jié)合一個例子來具體講解,一臺機開啟兩個ssh服務(wù)
我們最常使用的ssh服務(wù),系統(tǒng)默認ssh服務(wù)22端口,我現(xiàn)在想再開一個ssh服務(wù),端口8888
1.系統(tǒng)服務(wù)啟動腳本 /usr/lib/systemd/system/sshd.service,將其復(fù)制到 /etc/systemd/system/ 下,并改名為 sshd2.service,文件內(nèi)容如下:
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
因為要重啟一個新的服務(wù),所以要修改一下ExecStart這一行,讀取新的配置文件 sshd2_config,改為
ExecStart=/usr/sbin/sshd -D $OPTIONS -f /etc/ssh/sshd2_config
2.到 /etc/ssh/ 下,將 sshd_config 復(fù)制到 sshd2_config,并修改端口那一行
Port 8888
3.運行命令 systemctl reload 重新加載一下配置
4.運行命令 systemctl status sshd2.service 查看狀態(tài)
5 運行命令 systemctl start sshd2.service 開啟服務(wù)
6.運行命令 systemctl enable sshd2.service 設(shè)置開機啟動
7.在另一臺機器上登錄 ssh fancy@ip -p8888 就可以登錄了
-
注意1,防火墻要打開8888端口
-
注意2,官方建議用戶自己新建的服務(wù)腳本最好存放在 /etc/systemd/system/ 目錄下,實際情況下存放在系統(tǒng)服務(wù)目錄 /usr/lib/systemd/system/ 下也是沒有問題的,看個人選擇了
六、我們再來舉個例子,做一個自己的服務(wù)
1.在 /root/bin/ 下創(chuàng)建一個shell腳本 fancy_test.sh,并修改其權(quán)限,chmod u+x fancy_test.sh,內(nèi)容如下
#!/bin/bash
logdate=$(date +%s)
logdir="/root/log/"
logname=fancy.${logdate}.log
#echo $logname
touch ${logdir}${logname}
意思是,運行該服務(wù)時,在 /root/log/ 目錄下創(chuàng)建一個日志文件
2.在 /etc/systemd/system/ 下創(chuàng)建啟動腳本 fancy_test.service,輸入一下內(nèi)容
[Unit]
Description=fancy_test server daemon
[Service]
Type=simple
ExecStart=/root/bin/fancy_test.sh
[Install]
WantedBy=multi-user.target
3.運行命令 systemctl reload
4.運行命令 systemctl start fancy_test.service
5.此時你會看到在 /root/log/ 目錄下創(chuàng)建了一個日志文件
-
注意,我們這個是最簡單的服務(wù),執(zhí)行幾個命令而已,所以沒有配置文件,也不會常駐內(nèi)存,運行一次就結(jié)束
分享標題:Linux中systemctl使用方法
本文URL:http://www.fisionsoft.com.cn/article/djcpdhg.html


咨詢
建站咨詢
