linux系統(tǒng)服務(wù)(systemctl)的使用
點擊「閱讀原文」查看良許原創(chuàng)精品視頻。
服務(wù):常駐系統(tǒng)內(nèi)存中的進程且可以提供一些系統(tǒng)和網(wǎng)絡(luò)功能;現(xiàn)在最新的Linux系統(tǒng)都統(tǒng)一使用systemctl進行服務(wù)的管理
1.運行級別的分類 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)閉并重啟2.管理服務(wù) systemctl (root權(quán)限)
>?systemctl?[opt]?xxx.service
>?status??查看當前服務(wù)狀態(tài)
>?start??啟動服務(wù)
>?stop??關(guān)閉服務(wù)
>?restart?重啟服務(wù)
>?enable?設(shè)置開機啟動
>?disable?設(shè)置開機不啟動
>?reload??后面不接具體服務(wù)名,重新加載配置文件
>?mask?注銷服務(wù)
>?unmask?取消注銷3.一些常用命令 systemctl
查看當前已經(jīng)啟動的服務(wù) systemctl list-units 查看所有服務(wù) systemctl list-unit-files 查看服務(wù)有哪些依賴 systemctl list-dependencies xx.service 查看服務(wù)有哪些依賴(反向) systemctl list-dependencies --reverse xx.service
4.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ù)配置目錄5.結(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_config2.到 /etc/ssh/ 下,將 sshd_config 復(fù)制到 sshd2_config,并修改端口那一行
?Port?88883.運行命令 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/ 下也是沒有問題的,看個人選擇了
6.我們再來舉個例子,做一個自己的服務(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.target3.運行命令 systemctl reload
4.運行命令 systemctl start fancy_test.service
5.此時你會看到在 /root/log/ 目錄下創(chuàng)建了一個日志文件
注意,我們這個是最簡單的服務(wù),執(zhí)行幾個命令而已,所以沒有配置文件,也不會常駐內(nèi)存,運行一次就結(jié)束
推薦閱讀:
優(yōu)麒麟20.10終極預(yù)告,界面比較養(yǎng)眼
5T技術(shù)資源大放送!包括但不限于:C/C++,Linux,Python,Java,PHP,人工智能,單片機,樹莓派,等等。在公眾號內(nèi)回復(fù)「1024」,即可免費獲?。?!


