一.service和systemctl的區(qū)別
Linux服務(wù)管理的兩種方式:service 和 systemctl
service命令其實(shí)是去/etc/init.d目錄下,去執(zhí)行相關(guān)程序,init.d目錄包含許多系統(tǒng)各種服務(wù)的啟動(dòng)和停止腳本。當(dāng)Linux啟動(dòng)時(shí),會(huì)尋找這些目錄中的服務(wù)腳本,并根據(jù)腳本的run level確定不同的啟動(dòng)級(jí)別。
這種方法有兩個(gè)缺點(diǎn):
- 一是啟動(dòng)時(shí)間長(zhǎng)。init 進(jìn)程是串行啟動(dòng),只有前一個(gè)進(jìn)程啟動(dòng)完,才會(huì)啟動(dòng)下一個(gè)進(jìn)程。
- 二是啟動(dòng)腳本復(fù)雜。init 進(jìn)程只是執(zhí)行啟動(dòng)腳本,不管其他事情。腳本需要自己處理各種情況,這往往使得腳本變得很長(zhǎng)
Systemd 就是為了解決上面問題而誕生的。它的設(shè)計(jì)目標(biāo)是,為系統(tǒng)的啟動(dòng)和管理提供一套完整的解決方案。根據(jù) Linux 慣例,字母 d 是守護(hù)進(jìn)程(daemon)的縮寫。 Systemd 這個(gè)名字的含義,就是它要守護(hù)整個(gè)系統(tǒng)。使用了 Systemd,就不需要再用 init 了。Systemd 取代了 initd,成為系統(tǒng)的第一個(gè)進(jìn)程(PID 等于 1),其他進(jìn)程都是它的子進(jìn)程。
[root@xieshan system]# pwd
/usr/lib/systemd/system ? ? ? ?
systemctl可以管理/usr/lib/systemd/system這個(gè)目錄下所有以.service結(jié)尾的服務(wù),例如Network.service,nginx.service
本質(zhì)上systemctl還是執(zhí)行一個(gè)可執(zhí)行文件來管理那個(gè)服務(wù),只是這樣比較方便,通過systemctl來管理,不需要我們?nèi)フ业侥莻€(gè)文件再執(zhí)行
[root@xieshan system]# cat NetworkManager.service?
ExecStart=/usr/sbin/NetworkManager --no-daemon???????? #例如systemctl start NetworkManager就是到/usr/sbin/NetworkManager去執(zhí)行那個(gè)文件
systemctl也就是創(chuàng)建一個(gè)鏈接,鏈接到的位置是/etc/systemd/system/multi-user.target.wants
[root@xieshan ~]# systemctl enable firewalld
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
[root@xieshan ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@xieshan ~]# cd /etc/systemd/system/multi-user.target.wants
[root@xieshan multi-user.target.wants]# ls
auditd.service kdump.service rhel-configure.service tuned.service
chronyd.service mariadb.service rpcbind.service vmtoolsd.service
crond.service nfs-client.target rsyslog.service
ipmi.service postfix.service sshd.service
irqbalance.service remote-fs.target sysstat.service
systemctl ?disable ?firewalld ?設(shè)置firewalld服務(wù)開機(jī)不啟動(dòng) --》下一次開機(jī)的問題
systemctl ?enable ?firewalld ?設(shè)置firewalld服務(wù)開機(jī)啟動(dòng)
systemctl ?start ?firewalld ?立馬啟動(dòng)firewalld的服務(wù) ? --》本次
systemctl ?stop ?firewalld ? 立馬關(guān)閉firewalld的服務(wù)
service ? sshd ?start ?---> systemctl start sshd?
? ?舊式啟動(dòng)服務(wù)的方式 ? ? ? ? 新式啟動(dòng)服務(wù)的方式?
systemctl list-unit-files ? ? ? ? ? ? ? ?查看當(dāng)前哪些服務(wù)開機(jī)自啟,哪些服務(wù)開機(jī)不自啟,哪些服務(wù)是其他服務(wù)的依賴部分
????????
二.修改PATH環(huán)境變量的方法
1.臨時(shí)修改的方法
export PATH=變量路徑:$PATH或者直接PATH=變量路徑:$PATH
2.永久修改
修改 /root/.bashrc文件中的PATH變量
3.永久修改
修改/etc/profile文件,添加PATH=/sc:$PATH,在第一行添加就行。對(duì)所有的用戶都生效,都知道你的程序安裝的路徑了文章來源:http://www.zghlxwxcb.cn/news/detail-453878.html
三.編寫一個(gè)腳本,實(shí)現(xiàn)通過一條命令可以同時(shí)實(shí)現(xiàn)查看IP地址、網(wǎng)關(guān)、DNS服務(wù)器,并且設(shè)置為在哪里輸入這個(gè)命令都可以直接使用
[root@xieshan shell-test]# pwd
/lianxi/shell-test
[root@xieshan shell-test]# cat ip.sh?
#!/bin/bash
#獲得IP地址
ip=`ip add|awk -F' ' 'NR==9 {print $2}'`?
#獲得網(wǎng)關(guān)
route=`ip route|awk 'NR==1 {print $3}'`
#獲得DNS服務(wù)器
dns=`cat /etc/resolv.conf|awk 'NR==3 {print $2}'`
echo "IP地址為:"$ip,"GATEWAY為:"$route,"DNS服務(wù)器為:"$dns
[root@xieshan shell-test]# bash ip.sh?
IP地址為:192.168.2.43/24,GATEWAY為:192.168.2.1,DNS服務(wù)器為:114.114.114.114
以上這個(gè)簡(jiǎn)單功能就已經(jīng)實(shí)現(xiàn)了,但是只能在/lianxi/shell-test這個(gè)指定位置實(shí)現(xiàn),其他位置不能實(shí)現(xiàn)。接下來我們添加別名和修改PATH變量,達(dá)到在系統(tǒng)中的任意位置輸入ipa這個(gè)命令便可得得到以上效果文章來源地址http://www.zghlxwxcb.cn/news/detail-453878.html
[root@xieshan shell-test]# vim /root/.bashrc
[root@xieshan shell-test]# cat /root/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ipa=`bash /lianxi/shell-test/ip.sh` #給剛剛的腳本設(shè)置一個(gè)別名,輸入ipa可以得到同等效果
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
PATH=/lianxi/shell-test/:/usr/local/sclilin99/sbin:/lianxi: #修改path變量,增加自己腳本所在的路徑
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@xieshan shell-test]# ipa?
IP地址為:192.168.2.43/24,GATEWAY為:192.168.2.1,DNS服務(wù)器為:114.114.114.114
[root@xieshan shell-test]# cd ~
[root@xieshan ~]# ipa #現(xiàn)在在任意位置都可以實(shí)現(xiàn)以上功能了
IP地址為:192.168.2.43/24,GATEWAY為:192.168.2.1,DNS服務(wù)器為:114.114.114.114
到了這里,關(guān)于service和systemctl的區(qū)別/修改PATH的方法/一條命令查看IP地址和網(wǎng)關(guān)以及DNS服務(wù)器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!