日常运维常用命令工具3-firewalld、任务计划、systemd等

一、firewalld防火墙

1、firewalld防火墙介绍

在RHEL/CentOS 7.x开始使用firewalld防火墙管理工具,firewalld防火墙配置管理工具有CLI(命令行工具)和GUI(图形界面工具)两种工具。firewalld跟iptables相比,引入了区域的概念,每个区域有相关的策略。总共有9个区域。每个区域介绍如下:

trusted :允许所有的数据包

home :拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh、mdns、ipp-client、amba-client与dhcpv6-client服务相关,则允许流量

internal :等同于home区域

work : 拒绝流入的流量,除非与流出的流量数相关;而如果流量与ssh、ipp-client与dhcpv6-client服务相关,则允许流量

public :拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh、dhcpv6-client服务相关,则允许流量

external :拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh服务相关,则允许流量

dmz :拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh服务相关,则允许流量

block :拒绝流入的流量,除非与流出的流量相关

drop :拒绝流入的流量,除非与流出的流量相关

每个区域的模版文件:

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

各个服务的默认文件:

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

启动firewalld防火墙。为了防止与iptables冲突,先关闭iptables再开启firewalld。

[root@node5 ~]# systemctl stop iptables
[root@node5 ~]# systemctl start firewalld

2、命令行管理工具:firewall-cmd

firewall-cmd命令中使用的参数以及作用

--get-default-zone:查询默认的区域名称

--set-default-zone= :设置默认的区域,使其永久生效

--get-zones : 显示可用的区域

--get-services :显示预先定义的服务

--get-active-zones :显示当前正在使用的区域与网卡名称

--add-source= :将源自此IP或子网的流量导向指定的区域

--remove-source=:不再将源自此IP或子网的流量导向某个指定区域

--add-interface= :将源自该网卡的所有流量都导向某个指定区域

--change-interface=:将某个网卡与区域进行关联

--list-all : 显示当前区域的网卡配置参数、资源、端口以及服务等信息

--list-all-zones :显示所有区域的网卡配置参数、资源、端口以及服务等信息

--add-service= : 设置默认区域允许该服务的流量

--add-port= :设置默认区域允许该端口的流量

--remove-service= : 设置默认区域不再允许该服务的流量

--remove-port= : 设置默认区域不再允许该端口的流量

--reload : 让“永久生效”的配置规则立即生效,并覆盖当前的配置规则

--panic-on :开启应急状况模式

--panic-off :关闭应急状况模式

使用firewalld配置的防火墙策略默认为运行时(Runtime)模式,又称为当前生效模式,而且随着系统的重启会失效。如果想让配置策略一直存在,就需要使用永久(Permanent)模式了,方法就是在用firewall-cmd命令正常设置防火墙策略时添加--permanent参数,这样配置的防火墙策略就可以永久生效了;如果想让配置的策略立即生效,需要手动执行firewall-cmd --reload命令。

1、firewall-cmd基本使用

1、查看全部区域:

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

总共9个区域。

[root@node5 ~]# systemctl stop iptables && systemctl start firewalld

部分区域的配置文件:

[root@node5 ~]# ls /etc/firewalld/zones/
home.xml  public.xml  public.xml.old
[root@node5 ~]# 

2、查看默认区域:

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

默认区域是public。

3、查看某块网卡在firewalld服务中的区域

[root@node5 ~]# firewall-cmd --get-zone-of-interface=ens33
public

4、将ens33网卡的区域修改为work

[root@node5 ~]# firewall-cmd --permanent --zone=work --change-interface=ens33
The interface is under control of NetworkManager, setting zone to 'work'.
success
[root@node5 ~]# firewall-cmd --get-zone-of-interface=ens33
work
[root@node5 ~]# firewall-cmd --reload 
success
[root@node5 ~]# 

--permanent:表示永久生效。

5、修改当前的区域为home

[root@node5 ~]# firewall-cmd --set-default-zone=home
success
[root@node5 ~]# firewall-cmd --get-default-zone 
home
[root@node5 ~]# 

6、启动/关闭firewalld防火墙服务的应急状况模式,阻断一切网络连接(当远程控制服务器时请慎用)

[root@node5 ~]# firewall-cmd --panic-on && firewall-cmd --panic-off
success
success
[root@node5 ~]# 

上述命令,先开启应急模式,再关闭应急模式。

7、查询public区域是否允许ssh流量

[root@node5 ~]# firewall-cmd --zone=public --query-service=ssh
yes
[root@node5 ~]#

public区域默认允许ssh。

8、设置public区域中永久拒绝http,并且立即生效。

[root@node5 ~]# firewall-cmd --permanent --zone=public --remove-service=httpd
Warning: NOT_ENABLED: httpd
success
[root@node5 ~]# firewall-cmd --reload 
success
[root@node5 ~]# 

因为没有安装httpd,所以设置的时候会警告。

9、设置public区域中放行8080、8081端口流量,但仅当前生效。

[root@node5 ~]# firewall-cmd --zone=public --add-port=8080-8081/tcp
success
[root@node5 ~]# 

10、伪装 IP 的功能

[root@node5 ~]# firewall-cmd --add-masquerade 
success
[root@node5 ~]# 

11、查看public区域下的服务

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

12、查看全部服务

[root@node5 ~]# firewall-cmd --get-services 
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
[root@node5 ~]# 

14、通过修改配置文件修改ftp的端口为1121,并且在work区域中放行。

步骤1:把/usr/lib/firewalld/services/ftp.xml 复制/etc/firewalld/services/中,然后将21修改过为1121

[root@node5 ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/
[root@node5 ~]# vim /etc/firewalld/services/ftp.xml
<?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>

步骤2:把/usr/lib/firewalld/zones/work.xml 复制/etc/firewalld/zones/目录中,然后把ftp服务添加。最后重新加载。

[root@node5 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
[root@node5 ~]# vim /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"/>
</zone>
[root@node5 ~]# firewall-cmd --reload 
success
[root@node5 ~]# 

查看一下:

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

2、端口转发

端口转发命令格式:

firewall-cmd --permanent --zone= --add-forward-port=port=:proto=:toport=:toaddr=

把访问本机(192.168.10.205)888端口的流量22端口,永久并立即生效。

[root@node5 ~]# firewall-cmd --permanent --zone=public --add-forward-port=port=888:proto=tcp:toport=22:toaddr=192.168.10.205
success
[root@node5 ~]# firewall-cmd --reload 
success
[root@node5 ~]# 

3、富规则

firewall中的富规则可以设置更详细的防火墙规则。

因为前面的实验中已经修改了默认区域和ens33网卡的区域,所以做富规则实验前把ens33网卡的区域改为public。

[root@node5 ~]# firewall-cmd --permanent --zone=public --change-interface=ens33
The interface is under control of NetworkManager and already bound to 'public'
The interface is under control of NetworkManager, setting zone to 'public'.
success
[root@node5 ~]# firewall-cmd --set-default-zone=public 
Warning: ZONE_ALREADY_SET: public
success
[root@node5 ~]# firewall-cmd --reload 
success

例1:拒绝192.168.10.0/24网段的所有用户访问本机的ssh服务。

[root@node5 ~]# firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.10.0/24" service name="ssh" reject"
success
[root@node5 ~]# firewall-cmd --reload 
success
[root@node5 ~]# 

移除这条富规则:

[root@node5 ~]# firewall-cmd --permanent --zone=public --remove-rich-rule="rule family="ipv4" source address="192.168.10.0/24" service name="ssh" reject"
success
[root@node5 ~]# firewall-cmd --reload 
success

删除之后查看一下:

[root@node5 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens38
  sources: 
  services: ssh dhcpv6-client
  ports: 
  protocols: 
  masquerade: no
  forward-ports: port=1122:proto=tcp:toport=22:toaddr=10.0.0.101
    port=888:proto=tcp:toport=22:toaddr=192.168.10.205
  source-ports: 
  icmp-blocks: 
  rich rules: 

[root@node5 ~]# 

富规则已删除。

例2:把访问本机(192.168.10.205)888端口的流量22端口,永久并立即生效。

[root@node5 ~]# firewall-cmd --permanent --add-rich-rule "rule family="ipv4" forward-port port=888 protocol=tcp to-port=22"
success
[root@node5 ~]# firewall-cmd --reload 
success
[root@node5 ~]#

查看一下public配置文件:

[root@node5 ~]# 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"/>
  <forward-port to-addr="10.0.0.101" to-port="22" protocol="tcp" port="1122"/>
  <forward-port to-addr="192.168.10.205" to-port="22" protocol="tcp" port="888"/>
  <rule family="ipv4">
    <forward-port to-port="22" protocol="tcp" port="888"/>
  </rule>
</zone>
[root@node5 ~]# 

3、图形界面管理工具:firewall-config

运行图形界面工具,必须主机已安装图形界面,然后运行firewall-config即可。但不能远程运行firewall-config命令。

解释:

1:选择运行时(Runtime)模式或永久(Permanent)模式的配置。

2:可选的策略集合区域列表。

3:常用的系统服务列表。

4:当前正在使用的区域。

5:管理当前被选中区域中的服务。

6:管理当前被选中区域中的端口。

7:开启或关闭SNAT(源地址转换协议)技术。

8:设置端口转发策略。

9:控制请求icmp服务的流量。

10:管理防火墙的富规则。

11:管理网卡设备。

12:被选中区域的服务,若勾选了相应服务前面的复选框,则表示允许与之相关的流量。

13:firewall-config工具的运行状态。

配置比较简单。

二、任务计划cron

任务计划分一次性计划任务和长期性任务计划。

1、一次性计划任务(at)

命令:at 时间

安装at并启动atd服务:

[root@node5 ~]# yum install -y at
[root@node5 ~]# systemctl start atd

例如:在22:56分打印hello world

[root@node5 ~]# echo "Hello World" | at 22:58
job 3 at Sat Jun  9 22:58:00 2018
[root@node5 ~]# at -l
3    Sat Jun  9 22:58:00 2018 a root
You have mail in /var/spool/mail/root
[root@node5 ~]# 

查看计划任务:at -l

[root@node5 ~]# at -l
3    Sat Jun  9 22:58:00 2018 a root
You have mail in /var/spool/mail/root
[root@node5 ~]# 

删除计划任务:atrm num

[root@node5 ~]# echo "Hello World" | at 23:00
job 4 at Sat Jun  9 23:00:00 2018
[root@node5 ~]# echo "Hello World" | at 22:59
job 5 at Sun Jun 10 22:59:00 2018
[root@node5 ~]# at -l
4    Sat Jun  9 23:00:00 2018 a root
5    Sun Jun 10 22:59:00 2018 a root
[root@node5 ~]# atrm 4
[root@node5 ~]# at -l
5    Sun Jun 10 22:59:00 2018 a root
[root@node5 ~]# 

2、长期性计划任务(crontab)

crontab命令语法:

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

启动crond服务:

[root@node5 ~]# systemctl start crond

创建计划任务:crontab -u 用户名 -e

如果不指定用户,则表示自己。

[root@node5 ~]# crontab -e
5 23 * * * /bin/bash echo haha

查看计划任务:crontab -l

[root@node5 ~]# crontab -l
5 23 * * * /bin/bash echo haha
[root@node5 ~]# 

删除计划任务:crontab -r

[root@node5 ~]# crontab -l
08 23 * * * /usr/bin/echo haha
[root@node5 ~]# crontab -r
[root@node5 ~]# crontab -l
no crontab for root
[root@node5 ~]# 

计划任务创建后,保存在:/var/spool/cron/用户名文件中

3、anacron

anacron:可以指定任务在每天几点钟运行,可是如果那个钟点机器没有开,那个任务便错过了时间在一个新的时间轮回之内不再运行了。而 anacron 可以在每天、每周、每月(时间轮回天数可以自己指定)服务启动时便会将所有服务置为 Ready 状态,只等时间一到,便执行任务。

配置文件:/etc/anacrontab

三、系统服务管理-chkconfig

chkconfig命令用于管理系统服务,比如添加、删除、列出服务等。chkconfig是红帽6.x版本中的命令,但为了兼容,7.x版本中保留了这个命令。

1、列出系统服务

[root@node5 ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

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:off
rhnsd              0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@node5 ~]# 

2、关闭3级别的network服务

[root@node5 ~]# chkconfig --level 345 network off

3、移除network服务

[root@node5 ~]# chkconfig --del network

4、添加network服务

[root@node5 ~]# chkconfig --add network

5、设置network开机启动

[root@node5 ~]# chkconfig network on

服务脚本保存在:/etc/rc.d/init.d/目录中

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

四、systemd管理服务

红帽7.x版本系统使用systemd初始化进程服务。

systemctl start 服务名:启动服务,例如:

[root@node5 ~]# systemctl start crond

systemctl stop 服务名:停止服务,例如:

[root@node5 ~]# systemctl stop crond

systemctl restart 服务名:重启服务,例如:

[root@node5 ~]# systemctl restart crond

systemctl reload 服务名:重新加载配置文件,例如:

[root@node5 ~]# systemctl reload crond

systemctl status 服务名:查看服务状态,例如:

[root@node5 ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-06-09 23:38:26 CST; 49s ago
  Process: 3113 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 3106 (crond)
   CGroup: /system.slice/crond.service
           └─3106 /usr/sbin/crond -n

Jun 09 23:38:26 node5 systemd[1]: Started Command Scheduler.
Jun 09 23:38:26 node5 systemd[1]: Starting Command Scheduler...
Jun 09 23:38:26 node5 crond[3106]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 69% if used.)
Jun 09 23:38:26 node5 crond[3106]: (CRON) INFO (running with inotify support)
Jun 09 23:38:26 node5 crond[3106]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
Jun 09 23:38:53 node5 systemd[1]: Reloaded Command Scheduler.
Jun 09 23:39:01 node5 crond[3106]: (CRON) INFO (running with inotify support)
[root@node5 ~]# 

systemctl enable 服务名:设置开机启动服务,例如:

[root@node5 ~]# systemctl enable crond

systemctl disable 服务名:取消开机启动,例如:

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

systemctl is-enabled 服务名:查看服务是否开机启动,例如:

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

systemctl list-unit-files --type=service :查看个基本的服务启动情况

[root@node5 ~]# systemctl list-unit-files --type=service
UNIT FILE                                     STATE   
arp-ethers.service                            disabled
atd.service                                   enabled 
auditd.service                                enabled 
autovt@.service                               enabled 
blk-availability.service                      disabled
brandbot.service                              static  
...

systemctl list-units --all --type service:查看全部服务启动情况

[root@node5 ~]# systemctl list-units --all --type service
  UNIT                                          LOAD      ACTIVE   SUB     DESCRIPTION
  atd.service                                   loaded    active   running Job spooling tools
  auditd.service                                loaded    active   running Security Auditing Service
  brandbot.service                              loaded    inactive dead    Flexible Branding Service
  cpupower.service                              loaded    inactive dead    Configure CPU power related settings
  crond.service                                 loaded    active   running Command Scheduler
  dbus.service                                  loaded    active   running D-Bus System Message 
...

五、unit介绍

1、查看系统所有的unit

[root@node5 ~]# ls /usr/lib/systemd/system
arp-ethers.service                      rescue.service
atd.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
...

unit分类:

service:系统服务

target:多个unit组成的组

device:硬件设备

mount:文件系统挂载点

automount:自动挂载点

path:文件或路径

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

slice:进程组

snapshot :systemd快照

socket:进程间通信套接字

swap:swap文件

timer:定时器

2、unit相关命令

systemctl list-units:列出正则运存的unit

root@node5 ~]# systemctl list-units
UNIT                                           LOAD   ACTIVE SUB       DESCRIPTION
proc-sys-fs-binfmt_misc.automount              loaded active waiting   Arbitrary Executable File Formats File System A
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda1.device loaded active plugged   VMware_Vir
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda2.device loaded active plugged   LVM PV 0OO
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda.device loaded active plug
...

systemctl list-units --all:列出所有

[root@node5 ~]# systemctl list-units --all
  UNIT                                         LOAD      ACTIVE   SUB       DESCRIPTION
  proc-sys-fs-binfmt_misc.automount            loaded    active   waiting   Arbitrary Executable File Formats File Sys
  dev-block-8:2.device                         loaded    active   plugged   LVM PV 0OOJg7-hs0E-W3rK-7Jhe-LTmU-0jBF-c1x
  dev-cdrom.device                             loaded    active   plugged   VMware_Virtual_SATA_CDRW_Drive
...

systemctl list-units --all --state=inactive:列出inactive状态的unit

[root@node5 ~]# systemctl list-units --all --state=inactive
  UNIT                                            LOAD      ACTIVE   SUB  DESCRIPTION
  proc-sys-fs-binfmt_misc.mount                   loaded    inactive dead Arbitrary Executable File Formats File Syste
  sys-fs-fuse-connections.mount                   loaded    inactive dead FUSE Control File System
  tmp.mount                                       loaded    inactive dead Temporary Directory
...

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

[root@node5 ~]# systemctl list-units --type=service
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
atd.service                        loaded active running Job spooling tools
auditd.service                     loaded active running Security Auditing Service
crond.service                      loaded active running Command Scheduler
dbus.service                       loaded active running D-Bus System Messa
...

systemctl is-active crond.service :查看crontab服务是否激活

[root@node5 ~]# systemctl is-active crond.service 
active

六、target介绍

系统为了方便管理,所有使用target来管理unit,列出所有的target:

[root@node5 ~]# 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 
...

查看指定target下有哪些unit:

[root@node5 ~]# systemctl list-dependencies multi-user.target 
multi-user.target
● ├─atd.service
● ├─auditd.service
● ├─brandbot.path
● ├─dbus.service
● ├─firewalld.service
● ├─irqbalance.service
● ├─network.service
● ├─NetworkManager.service
● ├─plymouth-quit-wait.service
● ├─plymouth-quit.service
● ├─postfix.service
● ├─rhel-configure.service
...

查看默认的target:

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

multi-user.target:多用户模式

设置默认的target:

[root@node5 ~]# systemctl set-default graphical.target 
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
[root@node5 ~]# 

graphical.target:图形界面

查看某个服务属于哪个target,比如sshd,看[Install]部分:

[root@node5 ~]# 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]
WantedBy=multi-user.target
[root@node5 ~]# 

总结:

一个service属于一种类型的unit

多个unit组成了一个target

一个target里面包含了多个service

扩展:https://note.youdao.com/share/?token=9C1195E40F024155AD27C5EB997D1960&gid=28142651#/

转载于:https://my.oschina.net/logmm/blog/1827459

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值