14:Linux日常管理技巧-3

时间:2022-04-30 08:14:17

Linux日常管理技巧-3

iptables保存、备份和恢复

在此之前提到过一个iptables保存规则的命令:

[root@long0000 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]
##此命令会将当前iptables的规则保存到/etc/sysconfig/iptables文件中
##重启服务后会自动加载这个文件中的配置。

**还有一个方法可以保存iptables的规则:**iptables-save

##iptables-save命令可以将当前iptables的所有规则保存到一个自定义的文件中
    [root@long0000 ~]# iptables-save > iptables.bak

##保存的内容
    [root@long0000 ~]# cat iptables.bak 
    # Generated by iptables-save v1.4.21 on Wed Jun 13 23:01:12 2018
    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [197:19144]
    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    -A INPUT -p icmp -j ACCEPT
    -A INPUT -i lo -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
    -A INPUT -j REJECT --reject-with icmp-host-prohibited
    -A FORWARD -j REJECT --reject-with icmp-host-prohibited
    COMMIT
    # Completed on Wed Jun 13 23:01:12 2018
    # Generated by iptables-save v1.4.21 on Wed Jun 13 23:01:12 2018
    *nat
    :PREROUTING ACCEPT [31:14661]
    :INPUT ACCEPT [0:0]
    :OUTPUT ACCEPT [3:292]
    :POSTROUTING ACCEPT [3:292]
    COMMIT
    # Completed on Wed Jun 13 23:01:12 2018

使用iptables-save命令保存的备份文件恢复规则:iptables-restore

###先清空当前iptables的规则
    [root@long0000 ~]# iptables -F ; iptables -P INPUT ACCEPT
    [root@long0000 ~]# iptables -nvL
    Chain INPUT (policy ACCEPT 38 packets, 2588 bytes)
     pkts bytes target     prot opt in     out     source               destination         

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

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

###使用刚才保存的规则文件iptables.bak来恢复规则。
    [root@long0000 ~]# iptables-restore < iptables.bak 
    [root@long0000 ~]# iptables -nvL
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
        6   428 ACCEPT     all  -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
        0     0 ACCEPT     icmp -- * * 0.0.0.0/0 0.0.0.0/0 
        0     0 ACCEPT     all  -- lo * 0.0.0.0/0 0.0.0.0/0 
        0     0 ACCEPT     tcp  -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
        2  1152 REJECT     all  -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
        0     0 REJECT     all  -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

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

Firewalld

Firewalld简介

firewalld自身并不具备防火墙的功能
而是和iptables一样需要通过内核的netfilter来实现
也就是说firewalld和iptables一样,他们的作用都是用于维护规则
而真正使用规则干活的是内核的netfilter
只不过firewalld和iptables的结构以及使用方法不一样罢了。

关闭iptables服务,开启Firewalld服务

##停止iptables服务
    [root@long0000 ~]# systemctl stop iptables

##关闭iptables服务开机自启动
    [root@long0000 ~]# systemctl disable iptables
    Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.

##开启Firewalld服务开机自启动
    [root@long0000 ~]# 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.

##启动Firewalld服务
    [root@long0000 ~]# systemctl stop firewalld

Firewalld的9个zone

区域(zone):

iptables service 在 /etc/sysconfig/iptables 中储存配置

firewalld 将配置储存在 /usr/lib/firewalld/ 和 /etc/firewalld/ 中的各种 XML 文件里

在/etc/firewalld/的区域设定是一系列可以被快速执行到网络接口的预设定。列表并简要说明如下:

drop(丢弃)
    任何接收的网络数据包都被丢弃,没有任何回复。仅能有发送出去的网络连接。

block(限制)
    任何接收的网络连接都被 IPv4 的 icmp-host-prohibited 信息和 IPv6 的 icmp6-adm-prohibited 信息所拒绝。

public(公共)
    在公共区域内使用,不能相信网络内的其他计算机不会对您的计算机造成危害,只能接收经过选取的连接。

external(外部)
    特别是为路由器启用了伪装功能的外部网。您不能信任来自网络的其他计算,不能相信它们不会对您的计算机造成危害,只能接收经过选择的连接。

dmz(非军事区)
    用于您的非军事区内的电脑,此区域内可公开访问,可以有限地进入您的内部网络,仅仅接收经过选择的连接。

work(工作)
    用于工作区。您可以基本相信网络内的其他电脑不会危害您的电脑。仅仅接收经过选择的连接。

home(家庭)
    用于家庭网络。您可以基本信任网络内的其他计算机不会危害您的计算机。仅仅接收经过选择的连接。

internal(内部)
    用于内部网络。您可以基本上信任网络内的其他计算机不会威胁您的计算机。仅仅接受经过选择的连接。

trusted(信任)
    可接受所有的网络连接。

指定其中一个区域为默认区域是可行的。当接口连接加入了 NetworkManager,它们就被分配为默认区域。
安装时,firewalld 里的默认区域被设定为公共区域。

Firewaald常用命令语法:

命令格式:

firewall-cmd [--zone=zone_name] 动作选项 [--permanent]

注: 如果不指定--zone选项,则为当前所在的默认区域,
    --permanent选项为将改动写入到区域配置文件中,并且需要重新加载防火墙才能生效。 

常用动作选项

--state ##查看防火墙的状态
    [root@long0000 ~]# firewall-cmd --state
    running

--reload ##重新加载防火墙,中断用户的连接,将临时配置清掉,加载配置文件中的永久配置
    [root@long0000 ~]# firewall-cmd --reload
    success

--complete-reload ##重新加载防火墙,不中断用户的连接(防火墙出严重故障时使用)
    [root@long0000 ~]# firewall-cmd --complete-reload
    success

--panic-on ##紧急模式,强制关闭所有网络连接,--panic-off是关闭紧急模式
    [root@long0000 ~]# firewall-cmd --panic-on ; firewall-cmd --panic-off
    success
    success
(注:不要单独使用--panic-on,会导致远程连接断开,必须在系统本地操作才能开启)

动作中的查看操作:

--get-zones ##查看所有区域
    [root@long0000 ~]# firewall-cmd --get-zones
    block dmz drop external home internal public trusted work --get-default-zone ##查看当前的默认区域 [root@long0000 ~]# firewall-cmd --get-default-zone public --get-icmptypes ##查看支持的所有ICMP类型 [root@long0000 ~]# firewall-cmd --get-icmptypes address-unreachable bad-header communication-prohibited (省略部分内容..) --get-active-zones ##查看当前正在使用的区域 [root@long0000 ~]# firewall-cmd --get-active-zones public interfaces: ens33 ens37 --get-services ##查看当前区域支持的服务 [root@long0000 ~]# firewall-cmd --get-services RH-Satellite-6 amanda-client amanda-k5-client bacula (省略部分内容..) --list-services ##查看当前区域开放的服务列表 [root@long0000 ~]# firewall-cmd --list-services ssh dhcpv6-client --list-all ##查看此区域内的所有配置,类似于iptables -nvL [root@long0000 ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens33 ens37 sources: services: ssh dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: 

更改区域的操作:

--set-default-zone=work ##更改默认的区域
    [root@long0000 ~]# firewall-cmd --set-default-zone=work
    success
    [root@long0000 ~]# firewall-cmd --get-active-zones
    work
      interfaces: ens33 ens37

新建–add或删除–remove规则:

--add-interface=ens37 ##将ens37接口添加到指定的区域中
    [root@long0000 ~]# firewall-cmd --zone=public --add-interface=ens37
    The interface is under control of NetworkManager, setting zone to 'public'.
    success
    [root@long0000 ~]# firewall-cmd --get-active-zones
    work
      interfaces: ens33
    public
      interfaces: ens37

--add-port=12222/tcp --permanent ##添加端口到区域开放列表中 /tcp也可以为其他相同层次的协议
    [root@long0000 ~]# firewall-cmd --zone=work --add-port=443/tcp --permanent
    success

--add-port=5000-10000/tcp --permanent ##将端口范围添加到开放列表中;
    [root@long0000 ~]# firewall-cmd --zone=work --add-port=10000-20000/tcp --permanent
    success
    [root@long0000 ~]# firewall-cmd --reload
    success
    [root@long0000 ~]# firewall-cmd --list-ports
    443/tcp 10000-20000/tcp

--add-service=ftp --permanent ##添加服务到区域开放列表中
(注意:服务的名称需要与此区域支持的服务列表中的名称一致)
    [root@long0000 ~]# firewall-cmd --add-service=ftp --permanent
    success
    [root@long0000 ~]# firewall-cmd --reload
    success
    [root@long0000 ~]# firewall-cmd --list-service
    ssh dhcpv6-client ftp

--add-source=192.168.1.1 ##添加源地址到指定区域
    [root@long0000 ~]# firewall-cmd --add-source=10.1.1.169
    success
    [root@long0000 ~]# firewall-cmd --list-sources
    10.1.1.169

--remove-source=192.168.1.1 ##删除指定区域的源地址
    [root@long0000 ~]# firewall-cmd --remove-source=10.1.1.169
    success
    [root@long0000 ~]# firewall-cmd --list-sources
    (空的)

--change-interface=eth1 ##改变指定的接口到其他区域
    [root@long0000 ~]# firewall-cmd --zone=home --change-interface=ens33
    The interface is under control of NetworkManager, setting zone to 'home'.
    success

--remove-service=dhcpv6-client ##将home区域内的dhcpv6-client服务删除。
    [root@long0000 ~]# firewall-cmd --zone=home --list-service
    ssh mdns samba-client dhcpv6-client
    [root@long0000 ~]# firewall-cmd --zone=home --remove-service=dhcpv6-client
    success
    [root@long0000 ~]# firewall-cmd --zone=home --list-service
    ssh mdns samba-client



--add-masquerade ##开启SNAT(源地址转换)
--query-masquerade ##查询SNAT的状态
    [root@long0000 ~]# firewall-cmd --add-masquerade
    success
    [root@long0000 ~]# firewall-cmd --query-masquerade
    yes

--remove-interface=eth0 ##删除指定区域的网络接口。
--query-interface=eth0 ##确定该网卡接口是否存在于指定区域 
    [root@long0000 ~]# firewall-cmd --zone=home --remove-interface=ens33
    The interface is under control of NetworkManager, setting zone to default.
    success
    [root@long0000 ~]# firewall-cmd --zone=home --query-interface=ens33
    no


--add-forward-port=port=端口号:proto=tcp:toport=端口号:toaddr=ip ##端口转发
[root@long0000 ~]# firewall-cmd --add-forward-port=port=22222:proto=tcp:toport=22:toaddr=10.1.1.25
success
##添加这条命令

其他详细语法参考:http://blog.51cto.com/11638832/2092203

修改配置文件增加Firewalld规则

zone的配置文件模板存放在/usr/lib/firewalld/zones/目录下

[root@long0000 ~]# ls /usr/lib/firewalld/zones/ 
block.xml  dmz.xml  drop.xml  external.xml  home.xml  internal.xml  public.xml  trusted.xml  work.xml

添加或删除规则时使用–permanent选项更改配置文件之后会在/etc/firewalld/zones目录下面生成配置文件

并且每次修改都会将修改前的配置文件复制一份文件名以 old结尾的文件作为备份。

[root@long0000 ~]# ls /etc/firewalld/zones
drop.xml  public.xml  public.xml.old  work.xml  work.xml.old

案例:

案例需求:ftp服务自定义端口1121,需要在work zone下面放行ftp

1:复制ftp的service配置模板到Firewalld的service配置目录

[root@long0000 ~]# ls /etc/firewalld/services/
[root@long0000 ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/
[root@long0000 ~]# ls /etc/firewalld/services/
ftp.xml

编辑复制的ftp配置文件把port 21改为port 1121

[root@long0000 ~]# vim /etc/firewalld/services/ftp.xml 
[root@long0000 ~]# grep port !$
grep port /etc/firewalld/services/ftp.xml
  <port protocol="tcp" port="1121"/>

复制work的配置模板到/etc/firewalld/zones/目录下

[root@long0000 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
cp: overwrite ‘/etc/firewalld/zones/work.xml’? y
[root@long0000 ~]# ls /etc/firewalld/zones/
drop.xml  public.xml  public.xml.old  work.xml  work.xml.old

编辑复制的work配置文件,在最后的\标签的上一行添加一行内容

[root@long0000 ~]# vi /etc/firewalld/zones/work.xml ##增加一行<service name="ftp"/> [root@long0000 ~]# grep "<service" /etc/firewalld/zones/work.xml <service name="ssh"/> <service name="dhcpv6-client"/> <service name="ftp"/> (注意:不要遗漏 / 否则添加的内容不会生效)

重载Firewalld配置文件后查看是否添加成功

[root@long0000 ~]# firewall-cmd --zone=work --list-services
ssh dhcpv6-client
[root@long0000 ~]# firewall-cmd --reload
success
[root@long0000 ~]# firewall-cmd --zone=work --list-services
ssh dhcpv6-client ftp

Linux任务计划:crontab

什么是任务计划:

crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似。

当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程。

crond进程每分钟会定期检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务。

crontab常用选项:

-e:编辑用户的任务计划; -l:列出用户的任务计划; -r:删除用户的任务计划; -u<用户名称>:指定要设定任务计划的用户名。不使用-u选项默认为当前用户。

任务计划格式:

查看/etc/crontab文件可以看到任务计划的具体格式。但是不能随意更改这个文件的内容,

[root@long0000 ~]# 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
分 时 日 月 周 执行任务的用户名  需要执行的命令或者脚本的绝对路径 

##在使用crontab -e 命令添加计划任务的时候必须使用这个格式来设置。
分的范围:0-59
时的范围:0-23
日的范围:1-31
月的范围:1-12,
周的范围:0-7,0和7都可以表示周日。
可用格式1-5表示一个范围1到5
可用格式1,2,3表示1或者2或者3
可用格式*/2表示被2整除的数字,比如小时,那就是每隔2小时

案例:

1:添加一个任务,每小时的0分执行命令top -bn1 并保存到一个文件中

##执行crontab -e ,执行这个命令后的操作与vi相同,如果添加的任务格式有问题则不能保存
##编辑正确后会在/var/spool/cron/目录下创建以用户名命名的配置文件
[root@long0000 ~]# crontab -e
[root@long0000 ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@long0000 ~]# cat /var/spool/cron/root 
00 * * * * root /usr/bin/top -bn1 &>> /var/log/top.log

2:每隔2个小时的0分执行一次 free -h &>> /var/log/free.log

0 */2 * * * root /usr/bin/free -h &>>/var/log/free.log

3:每天0点0分执行一次命令df -h $>>/var/log/df.log

0 0 * * * root df -h $>>/var/log/df.log

查看当前用户所有的任务计划。

[root@long0000 ~]# crontab -l
00 * * * * root /usr/bin/top -bn1 &>> /var/log/top.log
0 */2 * * * root /usr/bin/frre -h &>> /var/log/free.log
0 0 * * * root /usr/bin/df -h &>> /var/log/df.log

清除当前用户的所有任务计划

[root@long0000 ~]# crontab -r
[root@long0000 ~]# crontab -l
no crontab for root

为指定用户添加计划任务。

[root@long0000 ~]# crontab -u user1 -e
no crontab for user1 - using an empty one
crontab: installing new crontab
[root@long0000 ~]# crontab -l
no crontab for root
[root@long0000 ~]# crontab -u user1 -l
0 0 * * * user1 /usr/bin/ls -a &>>/dev/null

清除指定用户的任务计划。

[root@long0000 ~]# crontab -u user1 -r
[root@long0000 ~]# crontab -u user1 -l
no crontab for user1

(注意:需要保证crond服务是启动的状态才会执行设置好的计划任务。)

[root@long0000 ~]# systemctl start crond.service
[root@long0000 ~]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2018-06-12 22:51:50 CST; 2 days ago
 Main PID: 567 (crond)
   CGroup: /system.slice/crond.service
           └─567 /usr/sbin/crond -n

Linux系统自启动服务管理:chkconfig

Chkconfig简介:

chkconfig命令检查、设置系统的各种服务。

它可查询操作系统在每一个执行等级中会执行哪些系统服务,其中包括各类常驻服务。

谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接。

Chkconfig常用操作:

命令格式:

chkconfig 选项 参数

常用选项:

--add:     增加所指定的系统服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文件内增加相关数据;

--del:     删除所指定的系统服务,不再由chkconfig指令管理,并同时在系统启动的叙述文件内删除相关数据;

--level     <等级代号>:指定读系统服务要在哪一个执行等级中开启或关毕。

等级代号列表:
    等级0表示:表示关机
    等级1表示:单用户模式
    等级2表示:无网络连接的多用户命令行模式
    等级3表示:有网络连接的多用户命令行模式
    等级4表示:不可用
    等级5表示:带图形界面的多用户模式
    等级6表示:重新启动
CentOS7中的2、3、4的启动等级都是相同的

案例:

1:查看当前chkconfig管理的所有服务

[root@long0000 ~]# chkconfig --list
(…………)
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:offCentOS7中默认只有netconsole和network两个服务由chkconfig所管理。)

2:关闭network服务的开机启动。

[root@long0000 ~]# chkconfig network off
[root@long0000 ~]# chkconfig --list 
(…………)
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:off   3:off   4:off   5:off   6:off

3:将level3级别设置开机启动。

[root@long0000 ~]# chkconfig --level 3 network on
[root@long0000 ~]# chkconfig --list 
(…………)
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:off   3:on    4:off   5:off   6:off

4:删除netconsole服务

[root@long0000 ~]# chkconfig --del netconsole
[root@long0000 ~]# chkconfig --list 
(…………)
network         0:off   1:off   2:off   3:on    4:off   5:off   6:off

5:添加netconsole服务

[root@long0000 ~]# chkconfig --add netconsole
[root@long0000 ~]# chkconfig --list 
(…………)
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:off   3:on    4:off   5:off   6:off

如何增加一个服务:

服务脚本文件:

每个被chkconfig管理的服务需要在对应的init.d下的脚本加上两行或者更多行的注释。

第一行告诉chkconfig缺省启动的运行级以及启动和停止的优先级。如果某服务缺省不在任何运行级启动,那么使用`-`代替运行级。

第二行对服务进行描述,可以用`\`跨行注释。

例如random.init包含三行:

# chkconfig: 2345 20 80
# description: Saves and restores system entropy pool for \
# higher quality random number generation.
1.  服务脚本必须存放在/etc/ini.d/目录下;
2.  chkconfig --add servicename在chkconfig工具服务列表中增加此服务,此时服务会被在/etc/rc.d/rcN.d`中赋予K/S入口;
3.  chkconfig --level 35 mysqld on修改服务的默认启动等级。

关于添加chkconfig管理服务的详细参考:https://blog.csdn.net/lanyang123456/article/details/54695690

Linux系统服务管理-Systemd

Systemd简介:

传统的Linux系统启动过程主要由著名的init进程(也被称为SysV init启动系统)来处理,

而基于init的启动系统被认为有效率不足的问题,

systemd是Linux系统机器的另一种启动方式,

宣称弥补了以传统Linux SysV init为基础的系统的缺点;

Systemd是用来启动守护进程,已成为大多数发行版的标准配置,学完systemd命令后,你会发现systemd很强大。

systemctl命令是系统服务管理器指令,它实际上将 servicechkconfig 这两个命令组合到一起。

几个常用的服务相关的命令

###systemctl list-units --all --type=service //查看systemd所管理的全部服务:
[root@long0000 ~]# systemctl list-units --all --type=service
  UNIT                                  LOAD      ACTIVE   SUB     DESCRIPTION
  auditd.service                        loaded    active   running Security Auditing Service
  brandbot.service                      loaded    inactive dead    Flexible Branding Service
  chronyd.service                       loaded    active   running NTP client/server
  crond.service                         loaded    active   running Command Scheduler
  dbus.service                          loaded    active   running D-Bus System Message Bus
(省略……)

##第一列UNIT表示服务的名称
##第二列LOAD表示服务时候加载。
##第三列ACTIVE表示服务时候激活,
##第四列SUB表示服务的运行状态
##第六列表示服务的说明。

###systemctl disable crond //不让开机启动
###systemctl is-enabled crond //检查服务是否开机启动
    [root@long0000 ~]# systemctl disable crond.service
    [root@long0000 ~]# systemctl is-enabled crond
    disabled

###systemctl enable crond.service //让服务开机启动
    [root@long0000 ~]# systemctl enable crond.service
    Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
    [root@long0000 ~]# systemctl is-enabled crond
    enabled

###systemctl status crond //查看状态
    [root@long0000 ~]# systemctl status crond
    ● crond.service - Command Scheduler
       Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
       Active: active (running) since Tue 2018-06-12 22:51:50 CST; 2 days ago
     Main PID: 567 (crond)
       CGroup: /system.slice/crond.service
               └─567 /usr/sbin/crond -n

###systemctl stop crond //停止服务
    [root@long0000 ~]# systemctl stop crond
    [root@long0000 ~]# systemctl status crond
    ● crond.service - Command Scheduler
       Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
       Active: inactive (dead) since Fri 2018-06-15 02:18:08 CST; 1s ago 
     Main PID: 567 (code=exited, status=0/SUCCESS)
(注意Active的变化。)

###systemctl start crond //启动服务
    [root@long0000 ~]# systemctl start crond
    [root@long0000 ~]# systemctl status crond
    ● crond.service - Command Scheduler
       Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
       Active: active (running) since Fri 2018-06-15 02:19:04 CST; 1s ago
     Main PID: 89087 (crond)
       CGroup: /system.slice/crond.service
               └─89087 /usr/sbin/crond -n

###systemctl restart crond //重启服务
[root@long0000 ~]# systemctl restart crond
[root@long0000 ~]# 

Unit:

Systemd可以管理所有系统资源,不同的资源统称为 Unit(单位)

系统所有unit存放在/usr/lib/systemd/system目录下

[root@long0000 ~]# ls /usr/lib/systemd/system
arp-ethers.service             machines.target                  sysinit.target.wants
auditd.service                 messagebus.service               sys-kernel-config.mount
(省略……)

分为以下类型
    service 系统服务 
    target 多个unit组成的组
    device 硬件设备
    mount 文件系统挂载点
    automount 自动挂载点
    path 文件或路径
    scope 不是由systemd启动的外部进程
    slice 进程组
    snapshot systemd快照
    socket 进程间通信套接字
    swap  swap文件
    timer 定时器

unit相关的命令:

###systemctl list-units //列出正在运行的unit

###systemctl list-units --all //列出所有,包括失败的或者inactive的

###systemctl list-units --all --state=inactive //列出inactive的unit

###systemctl list-units --type=service//列出状态为active的service

###systemctl is-active crond.service //查看某个服务是否为active

Target:

启动计算机的时候,需要启动大量的Unit。

如果每一次启动,都要一一写明本次启动需要哪些Unit,显然非常不方便。

Systemd的解决方案就是Target。

简单说,Target就是一个Unit组,包含许多相关的Unit。

启动某个Target的时候,Systemd就会启动里面所有的Unit。

从这个意义上说,Target这个概念类似于“状态点”,启动某个Target就好比启动到某种状态。

传统的init启动模式里面,有RunLevel的概念,跟Target的作用很类似。

不同的是,RunLevel是互斥的,不可能多个RunLevel同时启动,但是多个Target可以同时启动。

Target相关的命令:

###systemctl list-unit-files --type=target //查看系统所有的target
    [root@long0000 ~]# systemctl list-unit-files --type=target
    UNIT FILE                 STATE   
    basic.target              static  
    bluetooth.target          static  
    cryptsetup-pre.target     static  
    cryptsetup.target         static 
    (省略……)

###systemctl list-dependencies multi-user.target //查看指定target下面有哪些unit
    [root@long0000 ~]# systemctl list-dependencies multi-user.target
    multi-user.target
    ● ├─auditd.service
    ● ├─brandbot.path
    ● ├─chronyd.service
    ● ├─crond.service
    ● ├─dbus.service
   (省略……)

###systemctl get-default //查看系统默认的target
    [root@long0000 ~]# systemctl get-default
    multi-user.target

###systemctl set-default runlevel1.target //设置系统默认的target
    [root@long0000 ~]# systemctl set-default runlevel1.target
    Removed symlink /etc/systemd/system/default.target.
    Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/rescue.target.
    [root@long0000 ~]# systemctl get-default
    rescue.target
    [root@long0000 ~]# systemctl set-default multi-user.target 
(注意:实验完记得改回原来的、)    

###一个service属于一种类型的unit
###多个unit组成了一个target
###一个target里面包含了多个service

###cat /usr/lib/systemd/system/sshd.service //看[install]部分
[root@long0000 ~]# cat /usr/lib/systemd/system/sshd.service
(省略……)
[Install]
WantedBy=multi-user.target

扩展:

提供一个iptables系列文章的博客 https://www.zsythink.net/archives/tag/iptables/page/2/

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

systemd自定义启动脚本 http://www.jb51.net/article/100457.htm