運維平常

1、iptables規則備份和恢復

一、備份和恢復

[root@ma-1 ~]#service iptables save  (保存規則)
/etc/sysconfig/iptables (默認保存的位置)
[root@ma-1 ~]# iptables -t nat -nvL  (nat現有的規則)
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target prot opt in out source   destination 
0 0 DNAT   tcp  --  *  *   0.0.0.0/0192.168.133.130  tcp dpt:1122 to:192.168.100.100:22

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target prot opt in out source   destination 

Chain OUTPUT (policy ACCEPT 6 packets, 456 bytes)
 pkts bytes target prot opt in out source   destination 

Chain POSTROUTING (policy ACCEPT 6 packets, 456 bytes)
 pkts bytes target prot opt in out source   destination 
0 0 SNAT   all  --  *  *   192.168.100.100  0.0.0.0/0to:192.168.133.130
[root@ma-1 ~]# iptables-save > /tmp/ipt.txt  (用此命令能夠將規則保存到想要的路徑下)
[root@ma-1 ~]# cat /tmp/ipt.txt 
# Generated by iptables-save v1.4.21 on Sun Jun 10 13:57:22 2018
*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [6:456]
:POSTROUTING ACCEPT [6:456]
-A PREROUTING -d 192.168.133.130/32 -p tcp -m tcp --dport 1122 -j DNAT --to-destination 192.168.100.100:22
-A POSTROUTING -s 192.168.100.100/32 -j SNAT --to-source 192.168.133.130
COMMIT
# Completed on Sun Jun 10 13:57:22 2018
# Generated by iptables-save v1.4.21 on Sun Jun 10 13:57:22 2018
*filter
:INPUT ACCEPT [66:4912]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [48:5272]
COMMIT
# Completed on Sun Jun 10 13:57:22 2018
[root@ma-1 ~]# iptables -t nat -F  (清空以前的規則)
[root@ma-1 ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target prot opt in out source   destination 

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target prot opt in out source   destination 

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target prot opt in out source   destination 

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target prot opt in out source   destination 
[root@ma-1 ~]# iptables-restore < /tmp/ipt.txt  (恢復以前保存的規則)
[root@ma-1 ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target prot opt in out source   destination 
0 0 DNAT   tcp  --  *  *   0.0.0.0/0192.168.133.130  tcp dpt:1122 to:192.168.100.100:22

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target prot opt in out source   destination 

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target prot opt in out source   destination 

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target prot opt in out source   destination 
0 0 SNAT   all  --  *  *   192.168.100.100  0.0.0.0/0to:192.168.133.130

想要啓動服務器是啓動規則,就須要將規則存放在默認路徑下mysql

2、firewalled的9個zone

一、9個zone

首先須要打開firewalled  (前四步是轉換啓動firewalled)
[root@ma-1 ~]# systemctl disable iptables
Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.
[root@ma-1 ~]# systemctl stop iptables
[root@ma-1 ~]# 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@ma-1 ~]# systemctl start firewalld
[root@ma-1 ~]# firewall-cmd --get-zones  (查看全部zone)
block dmz drop external home internal public trusted work  (firewalld默認有9個zone,並且默認zone爲public)
[root@ma-1 ~]# firewall-cmd --get-default-zone (查看默認zone)
public


關於9個zone的解釋
drop:    (丟棄),任何接受的網絡數據包都被丟棄,沒有任何回覆。僅能有發送出去的網絡鏈接。

block:   (限制)拒絕全部外部鏈接,容許內部發起的鏈接,任何接受的網絡鏈接都被IPV4的icmp-host-prohibited信息和ipv6的icmp6-adm-prohibited信息所拒絕。

public:  (公共)在公共區域內使用,不能相信網絡內的其餘計算機不會對你的計算形成危害,只能接受通過選取的鏈接。

external:(外部)特別是爲路由器啓用了假裝功能的外部網。你不能信任來自網絡的其餘計算,不嫩更相信他們不會對你的計算機形成危害,只能接受通過選擇的鏈接。

dmz:     (非軍事區)用於你的非軍事區內的電腦,此區域內可公開訪問,能夠有限地進入你的內部網絡,僅僅接受通過選擇的鏈接。

work:    (工做)用於工做區。你能夠基本相信網絡內的其餘電腦不會危害你的電腦。僅僅接受通過選擇的鏈接。

home:    (家庭)用於家庭網絡。你能夠基本相信網絡內的其餘計算機不會危害你的計算機。僅僅接受通過選擇的鏈接。

internal:(內部)用於內部網絡,你能夠基本信任網絡內的其餘計算機不會威脅你的計算機,僅僅接受通過選擇的鏈接。

trusted: (信任)可接受全部的網絡鏈接

二、firewalled關於zone的操做

[root@ma-1 ~]# firewall-cmd --get-default-zone  (查看默認的zone)
public
[root@ma-1 ~]# firewall-cmd --set-default-zone=work   (設定默認zone)
success)
[root@ma-1 ~]# firewall-cmd --get-default-zone (已更改默認的zone)
work
[root@ma-1 ~]# firewall-cmd --get-zone-of-interface=ens33  (查看指定網卡的zone)
work
[root@ma-1 ~]# firewall-cmd --get-zone-of-interface=ens37  (查詢到沒有指定的zone,就要進行設置)
no zone  
[root@ma-1 ~]# firewall-cmd --get-zone-of-interface=lo
no zone
[root@ma-1 ~]# cd /etc/sysconfig/network-scripts/
[root@ma-1 network-scripts]# ls
ifcfg-ens33ifdown   ifdown-ippp  ifdown-postifdown-sit   ifdown-tunnel  ifup-bnep  ifup-ipv6  ifup-plusb  ifup-routes  ifup-TeamPort  init.ipv6-global
ifcfg-ens33:1  ifdown-bnep  ifdown-ipv6  ifdown-ppp ifdown-Team  ifup   ifup-eth   ifup-isdn  ifup-post   ifup-sit ifup-tunnelnetwork-functions
ifcfg-lo   ifdown-eth   ifdown-isdn  ifdown-routes  ifdown-TeamPort  ifup-aliases   ifup-ippp  ifup-plip  ifup-pppifup-Teamifup-wireless  network-functions-ipv6
[root@ma-1 network-scripts]# cp ifcfg-ens33 ifcfg-ens37
[root@ma-1 network-scripts]# ls
ifcfg-ens33ifdown   ifdown-ipv6  ifdown-routesifdown-tunnel  ifup-eth   ifup-plip   ifup-routesifup-tunnelnetwork-functions-ipv6
ifcfg-ens33:1  ifdown-bnep  ifdown-isdn  ifdown-sit   ifup   ifup-ippp  ifup-plusb  ifup-sit   ifup-wireless
ifcfg-ens37ifdown-eth   ifdown-post  ifdown-Team  ifup-aliases   ifup-ipv6  ifup-post   ifup-Team  init.ipv6-global
ifcfg-lo   ifdown-ippp  ifdown-ppp   ifdown-TeamPort  ifup-bnep  ifup-isdn  ifup-pppifup-TeamPort  network-functions
[root@ma-1 network-scripts]# vi ifcfg-ens37
[root@ma-1 network-scripts]# systemctl restart network.service
[root@ma-1 network-scripts]# systemctl restart firewalld
[root@ma-1 network-scripts]# firewall-cmd --get-zone-of-interface=ens37
work
[root@ma-1 network-scripts]# cd ~
[root@ma-1 ~]# firewall-cmd --zone=dmz --add-interface=ens37   (給指定網卡設置zone )
The interface is under control of NetworkManager, setting zone to 'dmz'.
success
[root@ma-1 ~]# firewall-cmd --get-zone-of-interface=ens37
dmz
[root@ma-1 ~]# firewall-cmd --zone=public --change-interface=ens37  (針對網卡更改zone)
The interface is under control of NetworkManager, setting zone to 'public'.
success
[root@ma-1 ~]# firewall-cmd --get-zone-of-interface=ens37
public
[root@ma-1 ~]# firewall-cmd --zone=public  --remove-interface=ens37  (針對網卡刪除zone ) 
The interface is under control of NetworkManager, setting zone to default.
success
[root@ma-1 ~]# firewall-cmd --get-zone-of-interface=ens37  (刪除zone以後,會顯示原有默認的zone)
work
[root@ma-1 ~]# firewall-cmd --get-active-zones  (查看系統全部網卡所在的zone) 
work
  interfaces: ens33 ens37
[root@ma-1 ~]# firewall-cmd --zone=dmz --add-interface=lo 
success
[root@ma-1 ~]# firewall-cmd --get-active-zones  
dmz
  interfaces: lo
work
  interfaces: ens33 ens37

firewalled關於service的操做

[root@ma-1 ~]# firewall-cmd --get-services   (查看全部的servies)
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kibana klogin kpasswd kshell ldap ldaps libvirt libvirt-tls managesieve mdns mosh mountd ms-wbt mssql mysql nfs nrpe ntp open*** ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server
[root@ma-1 ~]# firewall-cmd --get-default-zone  (查看當前的zone)
work
[root@ma-1 ~]# firewall-cmd --list-services  (查看當前的zone下的service)  
ssh dhcpv6-client
[root@ma-1 ~]# firewall-cmd --zone=public --list-services (查看public的zone下的service)  
ssh dhcpv6-client
[root@ma-1 ~]# firewall-cmd --zone=trusted --list-services  

[root@ma-1 ~]# firewall-cmd --zone=public --add-service=http  (把http增長到public zone下面) 
success
[root@ma-1 ~]# firewall-cmd --zone=public --list-services  
ssh dhcpv6-client http
[root@ma-1 ~]# firewall-cmd --zone=public --add-service=https  (把https增長到public zone下面)
success
[root@ma-1 ~]# firewall-cmd --zone=public --list-services  (添加的這些從新啓動就會消失,須要修改配置文件)
ssh dhcpv6-client http https 
[root@ma-1 ~]# firewall-cmd --zone=public --add-service=http --permanent  (更改配置文件) 
success
[root@ma-1 ~]# ls /etc/firewalld/zones  (/etc/firewalld/zones是配置文件的路徑)
public.xml  public.xml.old  (有兩個,是由於在未保存以前的會自動生成一個備份文件)
[root@ma-1 ~]# cat /etc/firewalld/zones/public.xml.old  (最開始未保存配置文件的)
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
</zone>
[root@ma-1 ~]# cat /etc/firewalld/zones/public.xml  (保存配置文件以後的,出現http)
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  <service name="http"/>
</zone>


[root@ma-1 ~]# ls /usr/lib/firewalld/zones/  (zone的配置文件模板)
block.xml  dmz.xml  drop.xml  external.xml  home.xml  internal.xml  public.xml  trusted.xml  work.xml  
[root@ma-1 ~]# ls /usr/lib/firewalld/services/  (services的配置文件模板)
amanda-client.xmldhcpv6.xml   https.xml ldaps.xmlopen***.xml   pulseaudio.xml   smtps.xml  tor-socks.xml
......(中間省略)
dhcpv6-client.xmlhigh-availability.xmlkshell.xmlntp.xml  ptp.xml   smtp-submission.xml  tinc.xml

需求:ftp服務自定義端口1121,須要在work zone下面放行ftp

[root@ma-1 ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
[root@ma-1 ~]# vi /etc/firewalld/services/ftp.xml   (把21改成1121)

<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>FTP</short>
  <description>FTP is a protocol used for remote file transfer. If you plan to make your FTP server publicly available, enable this option. You need the vsftpd package installed for this option to be useful.</description>
  <port protocol="tcp" port="1121"/>
  <module name="nf_conntrack_ftp"/>
</service>
[root@ma-1 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
[root@ma-1 ~]# vi /etc/firewalld/zones/work.xml  (增長一行,<service name="ftp"/>)

<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Work</short>
  <description>For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  <service name="ftp"/>
</zone>
[root@ma-1 ~]# firewall-cmd --reload   (從新加載此服務)
success
[root@ma-1 ~]# firewall-cmd --zone=work --list-services
ssh dhcpv6-client ftp

3、Linux任務計劃cron

一、cron的基本配置

[root@ma-1 ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)  
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed 

*   *  *  *  * user-name command to be executed
  分鐘(0-59) 小時(0-23) 日期(1-31) 月份(1-12) 星期(0-6,0表明星期天)  命令
  第1列表示分鐘1~59 每分鐘用*或者 */1表示
  第2列表示小時1~23(0表示0點)
  第3列表示日期1~31
  第4列表示月份1~12(*/2表示被2整除的數字)
  第5列標識號星期0~6(0表示星期天)
  第6列用戶名稱 (在不添加的狀況下默認爲root)
   第7列要運行的命令

二、操做命令

crond服務操做命令
  /sbin/service crond start   (啓動服務)  
  /sbin/service crond stop    (關閉服務)  
  /sbin/service crond restart (重啓服務)  
  /sbin/service crond reload  (從新載入配置)


crontab -l          查看當前用戶的cron配置
crontab -e          編輯當前用戶的cron配置
crontab -r          刪除當前用戶的cron配置
crontab -u          設定某個用戶的cron服務,通常root用戶在執行這個命令的時候須要此參數
crontab -u root -l  root查看本身的cron設置
crontab -u fred -r  root想刪除fred的cron設置
crontab -u root -e  在編輯cron服務時,編輯的內容有一些格式和約定
crontab -l          列出當前的crontab任務
crontab -d          刪除當前的crontab任務
crontab filename    以filename作爲crontab的任務列表文件並載入


[root@ma-1 ~]# ps aux | grep cron  (查看cron是否運行)
root   589  0.0  0.1 126276  1620 ?Ss   13:37   0:00 /usr/sbin/crond -n
root  2983  0.0  0.0 112720   984 pts/0S+   16:13   0:00 grep --color=auto cron


命令:0 4 1 jan * /etc/init.d/smb restart (一月一號的4點重啓smb)

知識點

1)若是任務腳本沒有執行,須要檢查命令的路徑是否爲絕對路徑

2)對執行的任務腳本能夠追加到日誌裏,能夠有據查詢

3)拷貝任務計劃,cat /var/spool/cron/root在此路徑下會以用戶名稱爲命名的文件,記錄任務命令,拷貝此文件便可備份

4、Linux系統服務管理—chkconfig工具

chkconfig命令用於檢查,設置系統的各類服務web

一、chkconfig [--add] [--del] [--list] [系統服務]

[root@ma-1 ~]# chkconfig --list  (查看服務列表)

注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。 

  要列出 systemd 服務,請執行 'systemctl list-unit-files'。
  查看在具體 target 啓用的服務請執行
  'systemctl list-dependencies [target]'。

netconsole  0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
[root@ma-1 ~]# chkconfig --level 3 network off   (關掉network服務的3級別)
[root@ma-1 ~]# chkconfig --list

注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。 

  要列出 systemd 服務,請執行 'systemctl list-unit-files'。
  查看在具體 target 啓用的服務請執行
  'systemctl list-dependencies [target]'。

netconsole  0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:關 4:開 5:開 6:關
[root@ma-1 ~]# chkconfig --level 35 network off (關掉network服務的3和5級別)
[root@ma-1 ~]# chkconfig --list

注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。 

  要列出 systemd 服務,請執行 'systemctl list-unit-files'。
  查看在具體 target 啓用的服務請執行
  'systemctl list-dependencies [target]'。

netconsole  0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:關 4:開 5:關 6:關
[root@ma-1 ~]# chkconfig --level 345 network on  (打開network服務的三、四、5級別)
[root@ma-1 ~]# chkconfig --list

注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。 

  要列出 systemd 服務,請執行 'systemctl list-unit-files'。
  查看在具體 target 啓用的服務請執行
  'systemctl list-dependencies [target]'。

netconsole  0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關

2

[root@ma-1 ~]# cd /etc/init.d
[root@ma-1 init.d]# ls
functions  netconsole  network  README
[root@ma-1 init.d]# cp network ma  (建立一個自定義腳本)
[root@ma-1 init.d]# ll
總用量 48
-rw-r--r--. 1 root root 17500 5月   3 2017 functions
-rwxr-xr-x  1 root root  7293 6月  10 16:40 ma
-rwxr-xr-x. 1 root root  4334 5月   3 2017 netconsole
-rwxr-xr-x. 1 root root  7293 5月   3 2017 network
-rw-r--r--. 1 root root  1160 8月   5 2017 README
[root@ma-1 init.d]# chkconfig --list  (查看還未添加到服務列表)

注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。 

  要列出 systemd 服務,請執行 'systemctl list-unit-files'。
  查看在具體 target 啓用的服務請執行
  'systemctl list-dependencies [target]'。

netconsole  0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
[root@ma-1 init.d]# chkconfig --add ma  (增長該服務到服務列表)
[root@ma-1 init.d]# chkconfig --list

注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。 

  要列出 systemd 服務,請執行 'systemctl list-unit-files'。
  查看在具體 target 啓用的服務請執行
  'systemctl list-dependencies [target]'。

ma  0:關 1:關 2:開 3:開 4:開 5:開 6:關
netconsole  0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
[root@ma-1 init.d]# chkconfig --del ma  (刪除該服務)
[root@ma-1 init.d]# chkconfig --list

注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。 

  要列出 systemd 服務,請執行 'systemctl list-unit-files'。
  查看在具體 target 啓用的服務請執行
  'systemctl list-dependencies [target]'。

netconsole  0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關

知識點

1)能夠在/etc/inittab中更改運行級別

5、systemd管理服務

一、管理服務

[root@ma-1 ~]# systemctl list-unit-files  (查看全部服務)
UNIT FILE STATE   
proc-sys-fs-binfmt_misc.automount static  
dev-hugepages.mount   static
......(中間省略)
chrony-dn***v@.timer  disabled
fstrim.timer  disabled
systemd-readahead-done.timer  indirect
systemd-tmpfiles-clean.timer  static  

231 unit files listed.
lines 201-234/234 (END)
[root@ma-1 ~]# systemctl list-units --all --type=service  (查看文件類型爲service的服務;加--all會將全部和service有關的都列出來)
  UNIT  LOAD  ACTIVE   SUB DESCRIPTION
  auditd.serviceloadedactive   running Security Auditing Service
......(中間省略)
  vgauthd.service   loadedactive   running VGAuth Service for open-vm-tools
  vmtoolsd.service  loadedactive   running Service for virtual machines hosted on VMware

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB= The low-level unit activation state, values depend on unit type.

86 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.
lines 61-94/94 (END)
[root@ma-1 ~]# systemctl list-units  --type=service   (不加--all,會列出少數的service)
  UNIT   LOAD   ACTIVE SUB DESCRIPTION
  auditd.service loaded active running Security Auditing Service
  chronyd.serviceloaded active running NTP client/server
......(中間省略)
LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB= The low-level unit activation state, values depend on unit type.

36 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
lines 11-44/44 (END)

二、經常使用命令

 systemctl enable crond.service  (讓服務開機啓動)
 systemctl disable crond         (不讓開機啓動)
 systemctl status crond          (查看狀態)
 systemctl stop crond            (中止服務)
 systemctl start crond           (啓動服務)
 systemctl restart crond         (重啓服務)
 systemctl is-enabled crond      (檢查服務是否開機啓動)

經常使用操做

[root@ma-1 ~]# systemctl is-enabled crond   (檢查服務是否開機啓動)
enabled
[root@ma-1 ~]# systemctl disable crond 
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@ma-1 ~]# systemctl is-enabled crond 
disabled
[root@ma-1 ~]# systemctl enable crond 
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[root@ma-1 ~]# systemctl is-enabled crond 
enabled
[root@ma-1 ~]# cat /etc/systemd/system/multi-user.target.wants/crond.service  (查看配置文件)
[Unit]
Description=Command Scheduler
After=auditd.service systemd-user-sessions.service time-sync.target

[Service]
EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process

[Install]
WantedBy=multi-user.target

[root@ma-1 ~]# ll !$  (能夠查看,而且是軟連接,原目錄是:/usr/lib/systemd/system/crond.service.
[root@ma-1 ~]# systemctl is-enabled crond )
ll /etc/systemd/system/multi-user.target.wants/crond.service
lrwxrwxrwx 1 root root 37 6月  10 17:08 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service
[root@ma-1 ~]# systemctl disable crond  
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@ma-1 ~]# ll /etc/systemd/system/multi-user.target.wants/  (當開機不啓動時,就沒法查看該目錄)crond.service
ls: 沒法訪問/etc/systemd/system/multi-user.target.wants/crond.service: 沒有那個文件或目錄

6、unit介紹

[root@ma-1 ~]# ls /usr/lib/systemd/system/  (查看enable狀態下原目錄的內容,系統全部unit,分爲如下類型)
arp-ethers.service  initrd-switch-root.service  rhel-autorelabel.service   systemd-hwdb-update.service
......(中間省略)
initrd-root-fs.target   rhel-autorelabel-mark.service   systemd-hostnamed.service  
[root@ma-1 ~]# cd !$
cd /usr/lib/systemd/system/
[root@ma-1 system]# ll runlevel*
lrwxrwxrwx. 1 root root 15 6月   1 22:38 runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 6月   1 22:38 runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 6月   1 22:38 runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 6月   1 22:38 runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 6月   1 22:38 runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 6月   1 22:38 runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 6月   1 22:38 runlevel6.target -> reboot.target


 service           系統服務 
 target            多個unit組成的組
 device            硬件設備
 mount             文件系統掛載點
 automount         自動掛載點
 path              文件或路徑
 scope             不是由systemd啓動的外部進程
 slice             進程組
 snapshot systemd  快照
 socket            進程間通訊套接字
 swap              swap文件
 timer             定時器

一、unit相關的命令

[root@ma-1 system]# systemctl list-units (列出正在運行的unit)
  UNIT   LOAD   ACTIVE SUB   DESCRIPTION
  proc-sys-fs-binfmt_misc.automount
.......(中間省略)
97 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
lines 72-105/105 (END)
[root@ma-1 system]# systemctl list-units --all  (列出全部,包括失敗的或者inactive的)
  UNIT  LOAD  ACTIVE   SUB   DESCRIPTION
  proc-sys-fs-binfmt_misc.automount
.......(中間省略)
190 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.
lines 165-198/198 (END)
[root@ma-1 system]# systemctl list-units --all --state=inactive  (列出inactive的unit)
  UNIT  LOAD  ACTIVE   SUB  DESCRIPTION
  proc-sys-fs-binfmt_misc.mount loadedinactive dead Arbitrary Executable File Formats File System
  sys-fs-fuse-connections.mount loadedinactive dead FUSE Control File System
.......(中間省略)
67 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.
lines 42-75/75 (END)
[root@ma-1 system]# systemctl list-units --type=service  (列出狀態爲active的service)
  UNIT   LOAD   ACTIVE SUB DESCRIPTION
  auditd.service loaded active running Security Auditing Service
  chronyd.serviceloaded active running NTP client/server
.......(中間省略)
36 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
lines 11-44/44 (END)
[root@ma-1 system]# systemctl is-active crond.service (查看某個服務是否爲active)
active

7、target介紹

target是由unit組成的,方便於管理sql

[root@ma-1 system]# systemctl list-unit-files --type=target  (查看全部unit)
UNIT FILE STATE   
basic.target  static  
bluetooth.target  static 
.......(中間省略)
umount.target static  

57 unit files listed.
lines 27-60/60 (END)
[root@ma-1 system]# systemctl list-dependencies multi-user.target  (查看指定target下面有哪些unit)
multi-user.target
● ├─auditd.service
● ├─brandbot.path
● ├─chronyd.service
● ├─dbus.service
● ├─firewalld.service
● ├─irqbalance.service
● ├─kdump.service
● ├─network.service
● ├─NetworkManager.service
[root@ma-1 system]# systemctl get-default (查看系統默認的target)
multi-user.target
[root@ma-1 system]# systemctl set-default multi-user.target (指定系統默認的target)
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
[root@ma-1 system]# cat /usr/lib/systemd/system/sshd.service 
[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]   (看[install]部分肯定unit屬於哪一個target)
WantedBy=multi-user.target

知識點

1)一個service屬於一種類型的unit

2)多個unit組成了一個target

3)一個target裏面包含了多個service

4)systemd自定義啓動腳本

http://www.jb51.net/article/100457.htm

5)anacron的補充

https://www.jianshu.com/p/3009a9b7d024?from=timelinedocker

6)提供一個iptables系列文章的博客

https://www.zsythink.net/archives/tag/iptables/
相關文章
相關標籤/搜索