10.19-10.27linux防火墙

本文详细介绍了CentOS Linux的防火墙管理,包括firewall的9个zone及其配置,如何查看和设置zone规则,以及服务(service)在不同运行级别(init)的应用。还涉及到服务的启动、停止和状态检查,以及chkconfig在CentOS 7的兼容性。同时讲解了systemd的unit类型和服务管理,特别是target的概念和作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

保存

恢复

 

 

重新打开 firewall

[root@linux-01 ~]# systemctl disable iptables
Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.
[root@linux-01 ~]# systemctl stop iptables

[root@linux-01 ~]# 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@linux-01 ~]# systemctl start firewalld

 

firewall 9个zone  自带的规则集

# 查看firewalld 9个规则集
[root@linux-01 ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work

#查看默认规则集
[root@linux-01 ~]#  firewall-cmd --get-default-zone
public

 

各个zones 的区别

 

怎么查看zone 的规则?

 

设置更改zone  

[root@linux-01 network-scripts]# firewall-cmd --set-default-zone=work
success
[root@linux-01 network-scripts]# firewall-cmd --get-default-zone
work

查看网卡使用的是哪个zone 

[root@linux-01 network-scripts]# firewall-cmd --get-zone-of-interface=ens33
work
[root@linux-01 network-scripts]# firewall-cmd --get-zone-of-interface=ens37
work
[root@linux-01 network-scripts]# firewall-cmd --get-zone-of-interface=lo
no zone


如果新添加的网卡是no zone      复制ens33 网卡配置文件  并重启网络服务 然后重新加载firewalld 

给指定的网卡设置 zone

[root@linux-01 network-scripts]# firewall-cmd --zone=public --add-interface=lo
success
[root@linux-01 network-scripts]# firewall-cmd --get-zone-of-interface=lo
public

针对网卡更改zone 

[root@linux-01 network-scripts]# firewall-cmd --zone=block --change-interface=ens37
The interface is under control of NetworkManager, setting zone to 'block'.
success
[root@linux-01 network-scripts]# firewall-cmd --get-zone-of-interface=ens37
block
[root@linux-01 network-scripts]#

针对网卡删除zone 

[root@linux-01 network-scripts]# firewall-cmd --zone=block --remove-interface=ens37
The interface is under control of NetworkManager, setting zone to default.
success
[root@linux-01 network-scripts]# firewall-cmd --get-zone-of-interface=ens37
work                               # 变回默认zone
[root@linux-01 network-scripts]# 

 

 

查看系统网卡所在的zone

[root@linux-01 network-scripts]# firewall-cmd --get-active-zones
work
  interfaces: ens33 ens37
public
  interfaces: lo
[root@linux-01 network-scripts]# 

 

service 

service 就是zone下面的子单元可以理解成指定的一个端口,因为防火墙不外乎就是针对某个端口做一些限制。不如http它操作的是80端口、http是它操作的是43端口、ssh它操作是22端口

列出系统所有 service

#当前所有得service

[root@linux-01 ~]# firewall-cmd --get-service
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 nfs3 nrpe ntp openvpn 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

#现在使用的zone有哪些service

[root@linux-01 network-scripts]# firewall-cmd --list-service
ssh dhcpv6-client


#查看指定zone 有哪些service
[root@linux-01 ~]# firewall-cmd --zone=public --list-service
ssh dhcpv6-client
[root@linux-01 ~]# 


[root@linux-01 ~]# firewall-cmd --zone=trusted --list-service

[root@linux-01 ~]#
# trusted 没有service

 

把http 加到public 

[root@linux-01 ~]# firewall-cmd --zone=public --add-service=http
success
[root@linux-01 ~]# firewall-cmd --zone=public --list-service
ssh dhcpv6-client http       # 已经添加
[root@linux-01 ~]#
[root@linux-01 ~]# firewall-cmd --zone=public --add-service=ftp
success
[root@linux-01 ~]# firewall-cmd --zone=public --list-service
ssh dhcpv6-client http ftp
[root@linux-01 ~]# 

 

把配置保存到配置里

添加service并保存到配置文件 (永久保存)
[root@linux-01 ~]# firewall-cmd --zone=public --add-service=http --permanent
success

#存放位置
[root@linux-01 ~]# ls /etc/firewalld/zones   #zone 的配置文件模板
public.xml  public.xml.old
[root@linux-01 ~]# 

[root@linux-01 ~]# 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="ssh"/>
  <service name="dhcpv6-client"/>
  <service name="http"/>
  <service name="ftp"/>
</zone>
[root@linux-01 ~]# 

 

 

更改zones后 永久保存时 会将就的备份

 

# zones 模板
[root@linux-01 ~]# ls /usr/lib/firewalld/zones
block.xml  dmz.xml  drop.xml  external.xml  home.xml  internal.xml  public.xml  trusted.xml  work.xml

#service模板
[root@linux-01 ~]# ls /usr/lib/firewalld/services/
amanda-client.xml        freeipa-replication.xml  libvirt-tls.xml           pop3.xml             snmp.xml
amanda-k5-client.xml     freeipa-trust.xml        libvirt.xml               postgresql.xml       spideroak-lansync.xml
bacula-client.xml        ftp.xml                  managesieve.xml           privoxy.xml          squid.xml
bacula.xml               ganglia-client.xml       mdns.xml                  proxy-dhcp.xml       ssh.xml
bitcoin-rpc.xml          ganglia-master.xml       mosh.xml                  ptp.xml              synergy.xml
bitcoin-testnet-rpc.xml  high-availability.xml    mountd.xml                pulseaudio.xml       syslog-tls.xml
bitcoin-testnet.xml      https.xml                mssql.xml                 puppetmaster.xml     syslog.xml
bitcoin.xml              http.xml                 ms-wbt.xml                quassel.xml          telnet.xml
ceph-mon.xml             imaps.xml                mysql.xml                 radius.xml           tftp-client.xml
ceph.xml                 imap.xml                 nfs3.xml                  RH-Satellite-6.xml   tftp.xml
cfengine.xml             ipp-client.xml           nfs.xml                   rpc-bind.xml         tinc.xml
condor-collector.xml     ipp.xml                  nrpe.xml                  rsh.xml              tor-socks.xml
ctdb.xml                 ipsec.xml                ntp.xml                   rsyncd.xml           transmission-client.xml
dhcpv6-client.xml        iscsi-target.xml         openvpn.xml               samba-client.xml     vdsm.xml
dhcpv6.xml               kadmin.xml               ovirt-imageio.xml         samba.xml            vnc-server.xml
dhcp.xml                 kerberos.xml             ovirt-storageconsole.xml  sane.xml             wbem-https.xml
dns.xml                  kibana.xml               ovirt-vmconsole.xml       sips.xml             xmpp-bosh.xml
docker-registry.xml      klogin.xml               pmcd.xml                  sip.xml              xmpp-client.xml
dropbox-lansync.xml      kpasswd.xml              pmproxy.xml               smtp-submission.xml  xmpp-local.xml
elasticsearch.xml        kshell.xml               pmwebapis.xml             smtps.xml            xmpp-server.xml
freeipa-ldaps.xml        ldaps.xml                pmwebapi.xml              smtp.xml
freeipa-ldap.xml         ldap.xml                 pop3s.xml                 snmptrap.xml
[root@linux-01 ~]# 

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

以配置文件方式操作

第一步

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

 <?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"/>     # 21改成1121
  <module name="nf_conntrack_ftp"/>
</service>

第二步

 

 

 

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


<?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"/>                      #增加一行ftp
</zone>

第三步、重新加载服务

[root@linux-01 ~]# firewall-cmd --reload
success
[root@linux-01 ~]# firewall-cmd --zone=work --list-service
ssh dhcpv6-client ftp
[root@linux-01 ~]# 

 

23、任务计划

#任务计划的配置文件

[root@linux-01 ~]# 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
               # 不写用户名就默认为root

使用方法 

# 编辑crontab 配置文件
 [root@linux-01 ~]# crontab -e

与vim一样按“i” 进入编辑
0 3 * * * /bin/bash  /usr/local/abin/123.sh >/tmp/123.log 2>>/tmp/123.log

/usr/local/abin/123.sh  #执行脚本
>/tmp/123.log           #正确的输出重定向
2>>/tmp/123.log         #错误的也追加到这来



0 3 1-10 */2 2,5 
0 3 
1-10     #1-10号
*/2      #能被2整除的双数月份 
2,5      #周二或者周五

启动停止服务

#启动
[root@linux-01 ~]# systemctl start crond 

#停止
[root@linux-01 ~]# systemctl stop crond

查看进程


[root@linux-01 ~]# ps aux |grep crond
root        606  0.0  0.1 126316  1672 ?        Ss   17:52   0:00 /usr/sbin/crond -n
root       3264  0.0  0.0 112724   996 pts/1    R+   21:19   0:00 grep --color=auto crond

 查看状态

[root@linux-01 ~]# systemctl status crond

 [root@linux-01 ~]# systemctl stop crond

 

服务没有启动,有能是编写配置文件的时候没有使用绝对路径 ,或者命令没有在配置文件中命令路径的目录中。

*建议没写一个任务计划都写一个日志,无论成功失败都有路径可查

备份任务配置文件

#查看任务计划列表
[root@linux-01 ~]# crontab -l
0 3 * * * /bin/bash  /usr/local/abin/123.sh >/tmp/123.log 2>>/tmp/123.log

[root@linux-01 ~]# cat /var/spool/cron/root 
0 3 * * * /bin/bash  /usr/local/abin/123.sh >/tmp/123.log 2>>/tmp/123.log
#配置文件存放路径 每个用户都有一个对应得cron 
#备份时复制这个文件

#删除任务文件
[root@linux-01 ~]# crontab -r


#查看指定用户任务计划
[root@linux-01 ~]# crontab -u root -l
no crontab for root

服务管理工具chkconfig(centos7不在使用但仍然兼容)

#列出所有服务
[root@linux-01 ~]# 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:关   #6个级别

 # 0级别 关机状态  
 # 1级别 单用户
 # 2级别 多用户模式(不带图形、NFS服务)
 # 3级别 多用户模式(不带图形)
 # 4级别 保留
 # 5级别 多用户模式(带图形)
 # 6级别 重启

 

服务的服务脚本

[root@linux-01 ~]# ls /etc/init.d
functions  netconsole  network  README
#关闭服务
[root@linux-01 ~]# chkconfig network off
[root@linux-01 ~]# 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:关

linux 有7个运行级别,分别从init 0到init 6。

init 0:关机状态,默认级别不能设为0,否则一开机就会自动关机

init 1:单用户模式,root权限,一般系统维护时才会使用,不能远程登录,只能本地

init 2:多用户模式,没有NFS和网络支持,说白了就是不能上网的init 3

init 3:完整的多用户文本模式,有NFS和网络,登录后可以进入控制台命令行模式(最常用的)

init 4:没啥用的模式,不用管它......

init 5:图形化界面,比较美观,但耗内存

init 6:重启,默认级别不能设为6,否则一开机就会自动重启

比较重要的级别:0,6(关机和重启的时候用)和3,5,所以先记住这四个级别就够用了

 

更改文件定义运行级别,系统一启动就定义在某个级别

[root@linux-01 ~]# vi /etc/inittab 

修改以下配置文件(新系统已全部注析)
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target

设置某个级别开启或关闭

netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
[root@linux-01 ~]# chkconfig --level 3 network off
[root@linux-01 ~]# 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@linux-01 ~]# chkconfig --level 345 network off
[root@linux-01 ~]# 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@linux-01 ~]# cd /etc/init.d
[root@linux-01 init.d]# ls
functions  netconsole  network  README
[root@linux-01 init.d]# cp network 123
[root@linux-01 init.d]# ls -l
总用量 48
-rwxr-xr-x  1 root root  7293 9月  11 22:57 123
-rw-r--r--. 1 root root 18104 1月   3 2018 functions
-rwxr-xr-x. 1 root root  4334 1月   3 2018 netconsole
-rwxr-xr-x. 1 root root  7293 1月   3 2018 network
-rw-r--r--. 1 root root  1160 4月  11 2018 README
[root@linux-01 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@linux-01 init.d]# chkconfig --add  123
[root@linux-01 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@linux-01 init.d]# 



#删除
[root@linux-01 init.d]# chkconfig --del  123
[root@linux-01 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@linux-01 ~]# systemctl list-units --all --type=service  

按空格键向下翻

 

如果想列出所有service 请使用   systemctl list-unit-files'

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

•systemctl enable crond.service //让服务开机启动

[root@linux-01 ~]# systemctl enable crond.service
[root@linux-01 ~]# 

• systemctl disable crond //不让开机启动

[root@linux-01 ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@linux-01 ~]# 

• systemctl status crond //查看状态

[root@linux-01 ~]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

9月 06 22:10:24 linux-01 systemd[1]: Started Command Scheduler.
9月 06 22:10:24 linux-01 systemd[1]: Starting Command Scheduler...
9月 06 22:10:24 linux-01 crond[606]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 86% if used.)
9月 06 22:10:25 linux-01 crond[606]: (CRON) INFO (running with inotify support)
9月 11 21:23:12 linux-01 systemd[1]: Stopping Command Scheduler...
9月 11 21:23:12 linux-01 systemd[1]: Stopped Command Scheduler.
Hint: Some lines were ellipsized, use -l to show in full.
[root@linux-01 ~]# 

 

• systemctl start crond //启动服务

[root@linux-01 ~]# systemctl start crond.service
[root@linux-01 ~]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
   Active: active (running) since 五 2019-09-13 15:23:09 CST; 3s ago
 Main PID: 4066 (crond)
   CGroup: /system.slice/crond.service
           └─4066 /usr/sbin/crond -n

9月 13 15:23:09 linux-01 systemd[1]: Started Command Scheduler.
9月 13 15:23:09 linux-01 systemd[1]: Starting Command Scheduler...
9月 13 15:23:09 linux-01 crond[4066]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 98% i...ed.)
9月 13 15:23:09 linux-01 crond[4066]: (CRON) INFO (running with inotify support)
9月 13 15:23:09 linux-01 crond[4066]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
Hint: Some lines were ellipsized, use -l to show in full.

 

• systemctl stop crond //停止服务

[root@linux-01 ~]# systemctl stop crond.service
[root@linux-01 ~]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

9月 06 22:10:25 linux-01 crond[606]: (CRON) INFO (running with inotify support)
9月 11 21:23:12 linux-01 systemd[1]: Stopping Command Scheduler...
9月 11 21:23:12 linux-01 systemd[1]: Stopped Command Scheduler.
9月 13 15:23:09 linux-01 systemd[1]: Started Command Scheduler.
9月 13 15:23:09 linux-01 systemd[1]: Starting Command Scheduler...
9月 13 15:23:09 linux-01 crond[4066]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 98% i...ed.)
9月 13 15:23:09 linux-01 crond[4066]: (CRON) INFO (running with inotify support)
9月 13 15:23:09 linux-01 crond[4066]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
9月 13 15:24:03 linux-01 systemd[1]: Stopping Command Scheduler...
9月 13 15:24:03 linux-01 systemd[1]: Stopped Command Scheduler.
Hint: Some lines were ellipsized, use -l to show in full.
[root@linux-01 ~]# 

• systemctl restart crond //重启服务

[root@linux-01 ~]# systemctl restart crond.service
[root@linux-01 ~]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
   Active: active (running) since 五 2019-09-13 15:26:04 CST; 6s ago
 Main PID: 4083 (crond)
   CGroup: /system.slice/crond.service
           └─4083 /usr/sbin/crond -n

9月 13 15:26:04 linux-01 systemd[1]: Started Command Scheduler.
9月 13 15:26:04 linux-01 systemd[1]: Starting Command Scheduler...
9月 13 15:26:04 linux-01 crond[4083]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 43% i...ed.)
9月 13 15:26:04 linux-01 crond[4083]: (CRON) INFO (running with inotify support)
9月 13 15:26:04 linux-01 crond[4083]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
Hint: Some lines were ellipsized, use -l to show in full.

• systemctl is-enabled crond //检查服务是否开机启动

[root@linux-01 ~]# systemctl is-enabled crond
disabled

查看service 配置文件内容

[root@linux-01 ~]# 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@linux-01 ~]# 

这个文件实际上是个软连接 

[root@linux-01 ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service
lrwxrwxrwx 1 root root 37 9月  13 15:30 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service
[root@linux-01 ~]#  
[root@linux-01 ~]# 
[root@linux-01 ~]# ls -l /usr/lib/systemd/system/crond.service
-rw-r--r--. 1 root root 284 4月  11 2018 /usr/lib/systemd/system/crond.service
[root@linux-01 ~]# 

如果enable 就会创建一个软连接它实际的源路径是/usr/lib/systemd/system

[root@linux-01 ~]# ls  /usr/lib/systemd/system
arp-ethers.service                      rescue.target
auditd.service                          rescue.target.wants
autovt@.service                         rhel-autorelabel-mark.service
basic.target                            rhel-autorelabel.service
basic.target.wants                      rhel-configure.service
blk-availability.service                rhel-dmesg.service
bluetooth.target                        rhel-domainname.service
brandbot.path                           rhel-import-state.service
brandbot.service                        rhel-loadmodules.service
chrony-dnssrv@.service                  rhel-readonly.service

 

unit介绍

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

• service 系统服务

• target 多个unit组成的组

• device 硬件设备

• mount 文件系统挂载点

• automount 自动挂载点

• path 文件或路径

• scope 不是由systemd启动的外部进程

• slice 进程组

• snapshot systemd快照

• socket 进程间通信套接字

• swap  swap文件

• timer 定时器

 

centos有7个运行级别 、而centos7也有7个运行级别

unit里有种类型是target,它是由多个service或unit组成组形成target 

• 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介绍 

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

• systemctl list-unit-files --type=target

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

 对比

 root@localhost ~]# systemctl list-dependencies basic.target

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

[root@localhost ~]# systemctl get-default
multi-user.target
[root@localhost ~]# 

 centos7 可以更改默认的target来决定类似centos6的运行级别

• systemctl set-default multi-user.target    设置默认运行级别

[root@localhost ~]# systemctl set-default multi-user.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@localhost ~]# 

• unit有多种类型、一个service属于一种类型的unit

• 多个unit组成了一个target

• 一个target里面包含了多个service

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

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值