iptables规则备份和恢复 ,firewalld9个zone和zone、service的操作,linux任务计划cron,chkconfig, systemd,unit, target

时间:2021-12-15 14:22:45

一、iptables规则的备份和恢复

保存和备份iptables规则
service iptables save //会把规则保存到/etc/sysconfig/iptables
把iptables规则备份到my.ipt文件中
iptables-save > my.ipt
恢复刚才备份的规则
iptables-restore < my.ipt


将iptables规则保存到其他文件中

service iptables save 这个命令会把规则保存到配置文件中/etc/sysconfig/iptables
需求:
若不想保存到这个配置文件中,把规则保存保存到另一个文件中
使用命令iptables-save将文件重定向到 /tmp/ipt.txt 文件中——>文件名称随便起

[root@han01 ~]# iptables-save > /tmp/ipt.txt
[root@han01 ~]# cat /tmp/ipt.txt
# Generated by iptables-save v1.4.21 on Fri Jun 15 18:36:30 2018
*nat
:PREROUTING ACCEPT [54:3910]
:INPUT ACCEPT [1:52]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:OUTPUT_direct - [0:0]

将iptables恢复刚备份的规则

  • 先将之前备份的规则清空下iptables -t nat -F
  • 再用iptables -t nat -nvL查看到规则都被清空了
  • 再恢复之前备份的规则,命令iptables-restore < /tmp/ipt.txt
  • 在使用命令iptables -t nat -nvL 会查看到清空的规则又回来了
  • 备份的规则,只有恢复的时候会用到,若是服务器一重启,就想要加载一些规则,那最好将规则放
  • 到/etc/sysconfig/iptables文件中去

二、firewalld的9个zone

Linux防火墙-firewalld**

  • 打开firewalld
  • -systemctl disable iptables
  • systemctl stop iptables
  • systemctl enable firewalld
  • systemctl start firewalld
  • firewalld默认有9个zone
  • 默认zone为public
  • firewall-cmd –get-zones //查看所有zone
  • firewall-cmd –get-default-zone//查看默认zone

firewalld防火墙机制**

因为之前禁掉了firewalld,打开了iptables,所以现在需要 打开firewalld,禁掉iptables

[root@han01 ~]# systemctl disable iptables
[root@han01 ~]# systemctl stop iptables
[root@han01 ~]# systemctl enable firewalld
[root@han01 ~]# systemctl start firewalld

这时用iptables -nvL和iptables -t nat -nvL查看规则,会看到增加了很多的链

firewalld默认有9个zone,zone是firewalld的一个单位,默认使用public zone——>每个zone就好比一个规则集

规则集就是zone里面自带一些规则,比如:这个zone放行了80端口,放行了22端口,关闭了某某端口,这就是一个规则集


firewalld中查看所有zone

firewall-cmd –get-zones 查看所有zone

[root@han01 ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work [root@han01 ~]# 

firewalld中查询默认的zone

firewall-cmd –get-default-zone查看默认zone

[root@han01 ~]# firewall-cmd --get-default-zone
public
[root@han01 ~]# 


三、firewalld关于zone的操作

Linux防火墙-firewalld

firewall-cmd –set-default-zone=work //设定默认zone
firewall-cmd –get-zone-of-interface=ens33 //查指定网卡
firewall-cmd –zone=public –add-interface=lo //给指定网卡设置zone
firewall-cmd –zone=dmz –change-interface=lo //针对网卡更改zone
firewall-cmd –zone=dmz –remove-interface=lo //针对网卡删除zone
firewall-cmd –get-active-zones //查看系统所有网卡所在的zone

firewall-cmd设定默认zone**

firewall-cmd –set-default-zone=work 设定默认的zone

[root@han01 ~]# firewall-cmd --set-default-zone=work
success
[root@han01 ~]# firewall-cmd --get-default-zone
work
[root@han01 ~]# 

firewall-cmd查看指定网卡

firewall-cmd –get-zone-of-interface=ens16777736 查指定网卡

[root@han01 ~]# firewall-cmd --get-zone-of-interface=eno16777736
work
[root@han01 ~]# firewall-cmd --get-zone-of-interface=lo
no zone
[root@han01 ~]# 

若是后续添加的网卡ens36,显示no zone,就需要把eno16777736的网卡配置环境复制一份,命令为ens36,并修改配置文件,最后重启网络服务,在重新加载firewalld服务(systemctl restart firewalld),在来查看ens36的zone
若还是没有zone,我们就去增加给ens36增加一个zone

firewall-cmd –zone=public –add-interface=ens36 给指定网卡设置zone

[root@han01 ~]# firewall-cmd --get-zone-of-interface=ens36
no zone
[root@han01 ~]# cd /etc/sysconfig/network-scripts/
[root@han01 network-scripts]# ls
ifcfg-eno16777736    ifdown-post      ifup-bnep   ifup-routes
ifcfg-eno16777736:0  ifdown-ppp       ifup-eth    ifup-sit
ifcfg-lo             ifdown-routes    ifup-ippp   ifup-Team
ifdown               ifdown-sit       ifup-ipv6   ifup-TeamPort
ifdown-bnep          ifdown-Team      ifup-isdn   ifup-tunnel
ifdown-eth           ifdown-TeamPort  ifup-plip   ifup-wireless
ifdown-ippp          ifdown-tunnel    ifup-plusb  init.ipv6-global
ifdown-ipv6          ifup             ifup-post   network-functions
ifdown-isdn          ifup-aliases     ifup-ppp    network-functions-ipv6
[root@hf network-scripts]# cp /etc/sysconfig/network-scripts/ifcfg-eno16777736 /etc/sysconfig/network-scripts/ens36
[root@hf network-scripts]# vi !$ #编辑配置文件
vi /etc/sysconfig/network-scripts/ens36
[root@han01 network-scripts]# systemctl restart network.service #重启网络服务
[root@han01 network-scripts]# systemctl restart firewalld #重新加载firewalld服务
[root@han01 network-scripts]# firewall-cmd --get-zone-of-interface=ens36 #查看ens36网卡的zone
no zone
[root@han01 network-scripts]# firewall-cmd --zone=work --add-interface=ens36 #给ens36网卡设置zone
success
[root@han01 network-scripts]# firewall-cmd --get-zone-of-interface=ens36 #查看ens36网卡的zone
work

firewall-cmd给指定网卡设置zone

firewall-cmd –zone=public –add-interface=lo 给指定网卡设置zone

[root@han01 network-scripts]# firewall-cmd --zone=public --add-interface=lo    给lo网卡设置zone 
success
[root@han01 network-scripts]# firewall-cmd --get-zone-of-interface=lo
public

firewall-cmd给指定网卡设置zone

firewall-cmd –zone=dmz –change-interface=lo 针对网卡更改zone

[root@han01 network-scripts]# firewall-cmd --get-zone-of-interface=lo
public
[root@han01 network-scripts]# firewall-cmd --zone=dmz --change-interface=lo    //针对网卡更改zone
success
[root@han01 network-scripts]# firewall-cmd --get-zone-of-interface=lo
dmz

firewall-cmd针对网卡删除zone

firewall-cmd –zone=block –remove-interface=ens37针对网卡删除zone

[root@han01 network-scripts]# firewall-cmd --zone=block --change-interface=ens36    给ens36网卡设置zone 
success
[root@han01 network-scripts]# firewall-cmd --zone=block  --remove-interface=ens36    //针对ens36网卡删除zone 
success
[root@han01 network-scripts]# firewall-cmd --get-zone-of-interface=ens36
no zone

在remove删除zone后,恢复默认的zone——>自己在删除后,就显示no zone,而并不是恢复默认的zone

firewall-cmd查看系统所有网卡所在的zone

firewall-cmd –get-active-zones 查看系统所有网卡所在的zone

[root@han01 network-scripts]# firewall-cmd --get-active-zones //查看系统所有网卡所在的zone
dmz
  interfaces: lo
work
  interfaces: eno16777736


四、 firewalld关于service的操作

Linux防火墙-firewalled

  • firewall-cmd –get-services 查看所有的servies

  • firewall-cmd –list-services //查看当前zone下有哪些service

  • firewall-cmd –zone=public –add-service=http //把http增加到public zone下面

  • firewall-cmd –zone=public –remove-service=http

  • ls /usr/lib/firewalld/zones/ //zone的配置文件模板

  • firewall-cmd –zone=public –add-service=http –permanent //更改配置文件,之后会在/etc/firewalld/zones目录下面生成配置文件

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

  • cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services

  • vi /etc/firewalld/services/ftp.xml //把21改为1121

  • cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/

  • vi /etc/firewalld/zones/work.xml //增加一行

  • firewall-cmd –reload //重新加载

  • firewall-cmd –zone=work –list-services

firewall-cmd查看所有的servies

firewall-cmd –get-services 查看所有的servies(这里的 s 可省略)

servies,就是zone下面的一个子单元,可理解为它是一个指定的端口

防火墙就是针对一些端口做出一些限制,比如:http操作的是80端口,https操作的是43端口,ssh操作的是22端口

[root@han01 ~]# firewall-cmd --get-services //列出系统中所有的services
amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns ftp high-availability http https imaps ipp ipp-client ipsec kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind samba samba-client smtp ssh telnet tftp tftp-client transmission-client vnc-server wbem-https

firewall-cmd查看当前zone下有哪些service(service可加 s,也可不加 s)

firewall-cmd –list-services 查看当前zone下有哪些service

[root@han01 ~]# firewall-cmd --get-default-zone    //查看当前的zone
work
[root@han01 ~]# firewall-cmd --list-services    //查看当前zone下有哪些service
dhcpv6-client ipp-client ssh

指定对应的zone,有哪些services

[root@han01 ~]# firewall-cmd --zone=public --list-services
dhcpv6-client ssh

firewall-cmd将http服务增加到public zone下面

firewall-cmd –zone=public –add-service=http 把http增加到public zone下面

[root@han01 ~]# firewall-cmd --zone=public --add-service=http
success
[root@han01 ~]# firewall-cmd --zone=public --list-service
dhcpv6-client http ssh
[root@han01 ~]# firewall-cmd --zone=public --add-service=ftp
success
[root@han01 ~]# firewall-cmd --zone=public --list-service
dhcpv6-client ftp http ssh

现在仅仅是内存里面zone增加了一些service,若想将这些配置保存到配置文件中去,只需在后面在增加–permanent,来更改配置文件

firewall-cmd –zone=public –add-service=http –permanent //更改配置文件,之后会在/etc/firewalld/zones目录下面生成配置文件

[root@han01 ~]# firewall-cmd --zone=public --add-service=http --permanent
success
[root@han01 ~]# ls /etc/firewalld/zones/    //每次改完配置文件,就会生成一个旧的作为备份,后缀名为.old
public.xml   public.xml.old
[root@han01 ~]# cat /etc/firewalld/zones/public.xml    //查看更改后的配置文件
<?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="dhcpv6-client"/>
  <service name="http"/>
  <service name="ssh"/>
</zone>

zone的配置文件模板

ls /usr/lib/firewalld/zones/ //zone的配置文件模板

能查看到有9个模板

[root@han01 ~]# ls /usr/lib/firewalld/zones/
block.xml  drop.xml      home.xml      public.xml   work.xml
dmz.xml    external.xml  internal.xml  trusted.xml
[root@han01 ~]# ls /usr/lib/firewalld/services/
amanda-client.xml      ipp-client.xml   mysql.xml       rpc-bind.xml
bacula-client.xml      ipp.xml          nfs.xml         samba-client.xml
bacula.xml             ipsec.xml        ntp.xml         samba.xml
dhcpv6-client.xml      kerberos.xml     openvpn.xml     smtp.xml
dhcpv6.xml             kpasswd.xml      pmcd.xml        ssh.xml
dhcp.xml               ldaps.xml        pmproxy.xml     telnet.xml
dns.xml                ldap.xml         pmwebapis.xml   tftp-client.xml
ftp.xml                libvirt-tls.xml  pmwebapi.xml    tftp.xml
high-availability.xml  libvirt.xml      pop3s.xml       transmission-client.xml
https.xml              mdns.xml         postgresql.xml  vnc-server.xml
http.xml               mountd.xml       proxy-dhcp.xml  wbem-https.xml
imaps.xml              ms-wbt.xml       radius.xml

firewall-cmd将public zone下面的http服务删除

firewall-cmd –zone=public –remove-service=http

firewalled案例

需求

将ftp服务自定义端口1121,需要在work zone下面放行ftp

实现

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

将内容中的21端口改为1121端口

[root@han01 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
[root@han01 ~]# vi /etc/firewalld/zones/work.xml

增加一行,内容为 <service name="ftp"/>

[root@han01 ~]# firewall-cmd --reload //重新加载
success
[root@han01 ~]# firewall-cmd --zone=work --list-services
dhcpv6-client ftp ipp-client ssh

firewall-cmd –reload重新加载

五、Linux任务计划

Linux任务计划

crontab -u-e-l-r
格式:分 时 日 月 周 user command
文件/var/spool/cron/username
分范围0-59,时范围0-23,日范围1-31,月范围1-12,周1-7
可用格式1-5表示一个范围15
可用格式1,2,3表示1或者2或者3
可用格式*/2表示被2整除的数字,比如小时,那就是每隔2小时
要保证服务是启动状态
systemctl start crond.service

crontab命令

crontab -e //编辑
crontab -l //列出 -crontab -r //删除
crontab -u username -l //指定用户

任务计划

crontab -e //编辑
在linux系统中,系统计划是必不可少的,比如备份数据,重启服务等
操作过程,可能是一个脚本,有可能是一个单独的命令,在特定的时间去执行它,所以任务计划是不可缺少的
在windows中都是使用的个人电脑,所以任务计划很少见,几乎用不到

linux中计划的配置文件

cat /etc/crontab //查看任务计划的配置文件
文件中会定义几个变量
SHELL=/bin/bash
PATH环境变量,它命令的路径
MAILTO发邮件给谁
[root@han01 ~]# 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 #星期,0或7都表示周日,也可以写成英文的简写
# | | | | |
# * * * * * user-name command to be executed #用户,不写用户就是root 最后一列,是你要执行的命令

crontab -e 进入到crontab的配置文件中,用法和vim一样

按 i 进入编辑模式

分钟,小时,日,月,星期,然后后面跟具体的命令

凌晨3点去执行,* 表示所有的意思
每天凌晨三点,执行123.sh脚本文件,正确的和错误的日志都输出到123.log文件中

0 3 * * *  /bin/bash  /usr/local/sbin/123.sh >/tmp/123.log 2>/tmp/123.log

因为是每天三点执行脚本,所以可以写成追加,每天都去记录日志

0 3 * * *  /bin/bash  /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

若想1-10号,双月去执行该脚本,后面就不在执行了——>只要 被2 整除,就符合条件

0 3 1-10 */2 * /bin/bash  /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

只要周2和周5执行该文件

0 3 1-10 */2 2,5 /bin/bash  /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

为什么没有年份?

用星期确定你的唯一性,比如说今年的6月18号和明年的6月18号的星期肯定是不同的,这样就可以确定某一天的唯一性

启动crond服务

若想要这个任务正常使用,还需要去启动crond服务
systemctl start crond.service //启动crond服务

若想检查服务是否成功启动,

方法一:可使用ps aux |grep cron 命令查看
若有这个进程,说明这个服务已经启动了
方法二:使用systemctl start crond 查看状态
若是 绿色 ,则表示该服务已经启动了
若是该服务已经停掉了,则不会有颜色

任务计划不执行的原因分析

在写了一个计划,放入到配置文件中,但就是不执行
不执行的原因很有可能是你写的脚本里面,没有使用 绝对路径 的原因导致不执行
因为很有可能,你在使用的命令不在PATH里面,所以要么将命令写一个绝对路径,要么将命令的路径加入到PATH变量里面去
建议:在写一个脚本的时候,都要写追加一个日志,这样可以保证这个任务有据可查,再不执行的时候,查看错误日志即可

任务计划备份

crontab -l //列出

crontab文件存在位置/var/spool/cron/username
在需要备份的时候,直接把这个文件,或者目录拷贝下即可


六、Linux系统服务管理-chkconfig

chkconfig --list
chkconfig --level 3 network off
chkconfig --level 345 network off
chkconfig --del network
chkconfig --add network

chkconfig工具

crond、iptables、firewalld、nginx、httpd、mysql等等,都属于服务。

chkconfig工具,在centos6和之前的版本中,控制服务的启动;但在centos7中很少使用了,但为了兼容之前的版本,依然可以使用,但在未来的趋势中, 有可能就会被遗弃了,现在就是过度的作用。

chkconfig –list列出所有的系统服务

表示chkconfig工具在centos6或之前的版本中,使用的服务的管理的机制叫 SysV,而centos7的版本中,使用的是 systemd 服务

[root@han01 ~]# 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:

chkconfig命令

服务的脚本存放在 /etc/init.d/ 下面

启动脚本存放该目录下

[root@han01 ~]# ls /etc/init.d/
functions  netconsole  network  README

chkconfig –list //列出所有的服务

chkconfig network off //将network服务关闭

[root@han01 ~]# chkconfig network off
[root@han01 ~]# chkconfig --list //会看到2,3,4级别关闭了

注意:该输出结果只显示 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@han01 ~]# chkconfig network on
[root@han01 ~]# chkconfig --list //会看到2,3,4级别又开启了

注意:该输出结果只显示 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:

在系统中有七个级别等级列表:

  • 等级0表示:表示关机
  • 等级1表示:单用户模式
  • 等级2表示:多用户模式,少nfs服务
  • 等级3表示:多用户模式,不带图形
  • 等级4表示:是一种保留的级别
  • 等级5表示:带图形界面的多用户模式
  • 等级6表示:重新启动

在centos6中的 /etc/inittab 中定义开机的级别

在centos7中,已经没有用了,不需要定义开机的级别了

chkconfig命令,指定某一级别开启/关闭

chkconfig –level 3 network off //指定network中的3级别关闭

[root@han01 ~]# chkconfig --level 3 network off //指定network中的3级别关闭
[root@han01 ~]# 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:

chkconfig –level 345 network on //指定network中的3,4,5级别开启

[root@han01 ~]# chkconfig --level 345 network on //指定network中的3,4,5级别开启
[root@han01 ~]# 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:

0和1和6级别不能设置成开

0级别在关机状态是不可能开启的

1级别是单用户模式,服务是不可能开启的

6级别在重启的时候,是不可能开启的——>重启相当于先关闭在启动(重启的那一刻是先关闭才对)

将一个脚本加入到服务列表中

  • 首先将启动脚本放入到 /etc/init.d 这个目录下——>只有在这个目录下,才可以添加到服务列表中去

  • 文件名称无所谓,但内容有格式要求:

    首先是是一个shell脚本

    然后chkconfig指定运行级别启动顺序,第10位启动,第90位关闭

    下面代码为它的固定格式,必须要有的!!!

# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
# start at boot time.

例子:

[root@han01 ~]# cd /etc/init.d
[root@han01 init.d]# ls
functions  netconsole  network  README
[root@han01 init.d]# cp network 123
[root@han01 init.d]# ls -l
总用量 40
-rwxr-xr-x  1 root root  7293 125 05:27 123
-rw-r--r--. 1 root root 17500 53 2017 functions
-rwxr-xr-x. 1 root root  4334 53 2017 netconsole
-rwxr-xr-x. 1 root root  7293 53 2017 network
-rw-r--r--. 1 root root  1160 1020 11:07 README
[root@han01 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@han01 init.d]# chkconfig --add 123 #将123加入到服务列表中
[root@han01 init.d]# chkconfig --list

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 
      如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
      欲查看对特定 target 启用的服务请执行
      'systemctl list-dependencies [target]'123             0:1:2:3:4:5:6:关
netconsole      0:1:2:3:4:5:6:关
network         0:1:2:3:4:5:6:关
[root@han01 init.d]# chkconfig --del 123 #删除服务列表中的脚本
[root@han01 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:

chkconfig –del network //删除服务列表中的脚本

chkconfig –add network //增加服务列表中的脚本

七、Linux系统服务管理-systemd

systemctl list-units --all --type=service
几个常用的服务相关的命令
systemctl enable crond.service //让服务开机启动
systemctl disable crond //不让开机启动
systemctl status crond //查看状态
systemctl stop crond //停止服务
systemctl start crond //启动服务
systemctl restart crond //重启服务
systemctl is-enabled crond //检查服务是否开机启动

systemd工具

systemd是centos7管理的一个服务机制,在centos6或之前的版本中可以使用chkconfig工具去管理系统的服务,在centos7中,也可以使用,但会提示使用 systemctl list-unit-files ,用它来查看所有的服务。

systemctl list-unit-files 查看所有的服务

[root@han01 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@han01 init.d]# systemctl list-unit-files    //查看所有的服务,里面不仅有service,还有socket,还有target
UNIT FILE                                   STATE   
proc-sys-fs-binfmt_misc.automount           static  
dev-hugepages.mount                         static  
dev-mqueue.mount                            static  
proc-sys-fs-binfmt_misc.mount               static  
sys-fs-fuse-connections.mount               static  
sys-kernel-config.mount                     static  
sys-kernel-debug.mount                      static  
tmp.mount                                   disabled
brandbot.path                               disabled

systemd相关的命令

systemctl list-units –all –type=service列出所有的service

会列出所有的service
列出描述信息,是否是loaded,是否是active
按 空格 往下翻
若是不加 –all ,则就不会列出 未激活的active

[root@han01 ~]# systemctl list-units --all --type=service    //列出所有的service
  UNIT                           LOAD      ACTIVE   SUB     DESCRIPTION
  auditd.service                 loaded    active   running Security Auditing Service
  avahi-daemon.service           loaded    active   running Avahi mDNS/DNS-SD Stack
  brandbot.service               loaded    inactive dead    Flexible Branding Service
  cpupower.service               loaded    inactive dead    Configure CPU power related 
  crond.service                  loaded    active   running Command Scheduler

等等等,只截取了一部分
并在最下面,会告诉你 LOAD,ACTIVE,SUB是什么意思
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.

还会提醒,若想列出所有的 unit files,请使用 systemctl list-unit-files 命令

systemctl enable crond.service 让服务开机启动——>service可省略
systemctl disable crond 不让开机启动

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

systemctl status crond 查看状态

[root@han01 ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 二 2017-12-05 01:37:49 CST; 5h 15min ago
 Main PID: 574 (crond)
   CGroup: /system.slice/crond.service
           └─574 /usr/sbin/crond -n

1205 01:37:49 hf-01 systemd[1]: Started Command Scheduler.
1205 01:37:49 hf-01 systemd[1]: Starting Command Scheduler...
1205 01:37:50 hf-01 crond[574]: (CRON) INFO (RANDOM_DELAY will be scaled with ...d.)
1205 01:37:50 hf-01 crond[574]: (CRON) INFO (running with inotify support)
Hint: Some lines were ellipsized, use -l to show in full.

systemctl stop crond 停止服务
systemctl start crond 启动服务
systemctl restart crond 重启服务
systemctl is-enabled crond 检查服务是否开机启动

[root@han01 ~]# systemctl is-enabled crond
enabled
[root@han01 ~]# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@han01 ~]# systemctl is-enabled crond
disabled
[root@han01 ~]# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.

并且可以通过输出信息,在 /etc/systemd/system/multi-user.target.wants/crond.service 获得service的配置文件内容

[root@han01 ~]# cat /etc/systemd/system/multi-user.target.wants/crond.service 获得service的配置文件内容
[Unit]
Description=Command Scheduler
After=syslog.target auditd.service systemd-user-sessions.service time-sync.target

[Service]
EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGS
KillMode=process

[Install]
WantedBy=multi-user.target

[root@han01 ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service #是一个软连接,从软链接的右边到左边
lrwxrwxrwx 1 root root 37 125 06:55 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service
[root@han01 ~]# ls -l /usr/lib/systemd/system/crond.service #这里才是文件真正的路径
-rw-r--r--. 1 root root 263 610 2014 /usr/lib/systemd/system/crond.service

八、unit介绍

ls /usr/lib/systemd/system //系统所有unit,分为以下类型:

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

unit相关的命令

systemctl list-units    #列出正在运行的unit
并会提示,若要列出所有的units,则需要加 --all

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

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

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

其中failed是一个特例,也会列出来

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

九、target介绍

系统为了方便管理target来管理unit

  • systemctl list-unit-files –type=target //列出系统中所有的target
  • systemctl list-dependencies multi-user.target //查看指定target下面有哪些unit
  • systemctl get-default //查看系统默认的target
  • systemctl set-default multi-user.target
  • 一个service属于一种类型的unit
  • 多个unit组成了一个target
  • 一个target里面包含了多个service
  • cat /usr/lib/systemd/system/sshd.service 看[install]部分

target相关命令

systemctl list-unit-files –type=target 列出系统中所有的target

[root@hf system]# systemctl list-unit-files --type=target
UNIT FILE STATE
basic.target static
bluetooth.target static
cryptsetup-pre.target static
cryptsetup.target static
ctrl-alt-del.target disabled
default.target enabled
emergency.target static

systemctl list-dependencies multi-user.target 查看指定target下面有哪些unit

systemctl get-default 查看系统默认的target

systemctl set-default multi-user.target 设置默认的target

一个service属于一种类型的unit

多个unit组成了一个target

一个target里面包含了多个service

cat /usr/lib/systemd/system/sshd.service 看[install]部分

[root@han01 system]# cat /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
After=syslog.target network.target auditd.service
[Service]
EnvironmentFile=/etc/sysconfig/sshd
ExecStartPre=/usr/sbin/sshd-keygen
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target

只有multi-user.target 里面的service可以设置开机启动。其他的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