10.5 firewalld
之前提到,在CentOS7之前的CentOS版本的防火墙为netfilter
,CentOS7的防火墙为firewalld
。
firewalld的9个zone
要使用firewalld,需要我们关闭iptables服务,开启firewalld服务:
# systemctl stop iptables #关闭iptables服务
# systemctl disable iptables #禁止iptables服务开机启动
# systemctl enable firewalld #让firewalld开机启动
# systemctl start firewalld #启动firewalld服务
firewalld
默认有9个zone:drop
、block
、public
、external
、dmz
、work
、home
、internal
、trusted
。
drop 接收的任何网络数据包都会被丢弃,没有任何回复,仅能有发送出去的网络连接
block 接收的任何网络数据包都被 IPv4 的 icmp-host-prohibited 信息和 IPv6 的 icmp6-adm-prohibited 信息所拒绝
public 在公共区域内使用,不能相信网络内的其他计算机不会对你的计算机造成危害,只能接收经过选取的连接
external 特别是为路由器启用了伪装功能的外部网,你不能信任来自网络的其他计算机,不能相信他们不会对你的计算机造成危害,只能接收经过选取的连接
dmz 用于你的非军事区内的电脑,此区域内可公开访问,可以有限地进入你的内部网络,仅仅接收经过选取的连接
work 用于工作区,你可以基本相信网络内的其他计算机不会危害你的电脑,仅接收经过选取的连接
home 用于家庭网络,你可以基本相信网络内的其他计算机不会危害你的电脑,仅接收经过选取的连接
internal 用于内部网络,你可以基本相信网络内的其他计算机不会危害你的电脑,仅接收经过选取的连接
trusted 可以接收所有的网络连接
firewalld关于zone的操作
关于firewalld有两个基础的概念,分别是zone
和service
,每一个zone里面有不同的iptables规则。
# firewall-cmd --get-zones #查看所有的zone
block dmz drop external home internal public trusted work
# firewall-cmd --get-default-zone #查看firewalld的默认zone
public
可以看到,firewalld
的默认zone是public
。
# firewall-cmd --set-default-zone=work #设置默认的zone
Warning: ZONE_ALREADY_SET: work
success
# firewall-cmd --get-default-zone
work
# firewall-cmd --get-zone-of-interface=ens33 #查看指定网卡的zone
public
# firewall-cmd --zone=public --add-interface=ens33:1 #给指定网卡设置zone
success
# firewall-cmd --zone=dmz --change-interface=ens33:1 #给指定网卡更改zone
success
# firewall-cmd --zone=dmz --remove-interface=ens33:1 #给指定网卡删除zone
success
# firewall-cmd --get-active-zones #查看系统所有网卡所在的zone
public
interfaces: ens33
firewalld关于service的操作
之所以有9个zone,是因为每一个zone里面都使用了不同的service,而service是针对一个服务(端口)做的iptables规则。
service都是由一个个配置文件定义的,配置文件的模板在/usr/libfirewalld/services/
目录下,真正生效的配置则是在/etc/firewalld/services
目录下面。
# firewall-cmd --get-service #列出当前系统所有的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
# firewall-cmd --list-services #查看当前zone下有哪些service
ssh dhcpv6-client
# firewall-cmd --zone=public --list-services #查看指定zone下有哪些service
ssh dhcpv6-client
# firewall-cmd --zone=public --add-service=http #给指定zone添加服务,重启失效
success
# firewall-cmd --zone=public --list-services
ssh dhcpv6-client http
每个zone都有自己的配置文件,可以查看目录/usr/lib/firewalld/zones/
下面的文件,这些文件都是zone的配置文件模板。
# ls /usr/lib/firewalld/zones
block.xml drop.xml home.xml public.xml work.xml
dmz.xml external.xml internal.xml trusted.xml
但其实真正的配置文件则是在/etc/firewalld/zones/
目录下面。
# firewall-cmd --zone=public --list-services
ssh dhcpv6-client http
# 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"/>
</zone>
上面我们可以看到,之前添加的http服务并没有在配置文件中看到,所以重启后public这个zone中就不会有http服务。
给指定zone添加服务可以直接修改该zone的配置文件,这样会永久生效。修改配置文件时需要加一个选项:--permanent
。
# firewall-cmd --zone=public --add-service=http --permanent
success
# 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"/>
</zone>
上面我们可以看到,直接修改了public的配置文件,在配置文件中我们可以看到<service name="http"/>
字样,说明重启后public中也会有http服务。
10.6 Linux任务计划Cron
crontab
linux任务计划功能的操作是通过crontab
命令来完成的。
- 参数:
-u 表示指定某个用户,不加 -u 选项则为当前用户
-e 表示制定计划任务
-l 表示列出计划任务
-r 表示删除计划任务
注意:
- crontab -u、-e、-l、-r
- 格式:分 时 日 月 周 user command
- 文件
/var/spool/cron/username
(使用crontab -e
去编辑,直接vi或vim编辑会出错)- 分范围
0-59
,时范围0-23
,日范围1-31
,月范围1-12
,周1-7
- 可用格式
1-5
表示一个范围1到5- 可用格式
1,2,3
表示1或2或3- 可用格式
*/2
表示被2整除的数字,比如小时,那就是每隔2小时- 要保证服务是启动状态
systemctl start crond.service
- 查看任务计划的配置文件:
# 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;最后一列,是你要执行的命令
为什么没有年份呢?
因为用星期可以确定你的唯一性,比如说今年的6月14号和明年的6月14号的星期肯定是不同的,这样就可以确定某一天的唯一性。
另外,删除计划任务尽量不用crontab -r
命令,因为该命令会删除所有全部任务计划。
- 示例:
# crontab -e
30 20 14 6 4 echo "lzx" > /root/cron.log
01 20 1 7 0 echo "ok" > /root/cron.1.log
# crontab -l
30 20 14 6 4 echo "lzx" > /root/cron.log
01 20 1 7 0 echo "ok" > /root/cron.1.log
# crontab -r
# crontab -l
no crontab for root
删除计划任务,可以使用crontab -e
命令,进入crontab进行编辑。
10.7 Linux服务管理
chkconfig
CentOS6上的服务管理工具为chkconfig
,linux系统所有的预设服务都可以通过查看/etc/init.d/
目录得到。
# ls /etc/init.d
functions netconsole network README
CentOS7可以继续使用chkconfig
命令,系统的预设服务都可以这样实现:service 服务名 start/stop/restart
。
例如,启动crond除了可以使用命令service crond start
之外,还可以使用命令/etc/init.d/crond start
来启动。
# 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:关
- CentOS7之前的版本采用的服务管理为SysV,而CentOS7换成了systemd;
- 数字0~6为系统启动级别,CentOS7仅保留运行级别0、1、6;
- CentOS中,运行级别0表示关机;1表示重启至单用户模式;2表示无NFS支持的多用户模式;3表示完全多用户模式;4保留给用户自定义;5表示图形登录方式;6为重启。
# chkconfig --level 3 network off #修改某级别下某服务的开启状态,--level 指定级别,后面跟服务名 on/off
# chkconfig --list |grep network
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
network 0:关 1:关 2:开 3:关 4:开 5:开 6:关
# chkconfig --level 345 network off # --level 可以指定多个级别
# chkconfig --list |grep network
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
network 0:关 1:关 2:开 3:关 4:关 5:关 6:关
# chkconfig --list | grep network
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
# chkconfig network off #不指定--level时,默认针对级别2,3,4,5操作
# chkconfig --list |grep network
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
network 0:关 1:关 2:关 3:关 4:关 5:关 6:关
# chkconfig --del network #从系统服务中删除network服务
# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
# chkconfig --add network #增加network服务到系统服务中
# 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 --add/del 服务名
用来 增加或删除 系统服务,通常用来把自定义的启动脚本加入到系统服务中。
systemd
上面有讲过,CentOS7将SysV改为systemd了,因为SysV只能一个一个地启动服务,而systemd支持多个服务并发启动。
# systemctl list-units --all --type=service | head
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
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 Bus
display-manager.service not-found inactive dead display-manager.service
dracut-shutdown.service loaded inactive dead Restore /run/initramfs
ebtables.service loaded inactive dead Ethernet Bridge Filtering tables
命令systemctl list-units --all --type=service
会列出所有的服务,这些服务对应的的启动脚本文件位于/usr/lib/systemd/system/
目录下。
- 常用命令(以cron服务为例):
# systemctl enable crond.service #服务开机启动(.service可以省略)
# systemctl disable crond #服务不开机启动
# systemctl status crond #查看状态
# systemctl stop crond #停止服务
# systemctl start crond #启动服务
# systemctl restart crond #重启服务
# systemctl is-enabled crond #检查服务是否开机启动
# ls /usr/lib/systemd/system/ | head
arp-ethers.service
auditd.service
autovt@.service
basic.target
basic.target.wants
blk-availability.service
bluetooth.target
brandbot.path
brandbot.service
chrony-dnssrv@.service
可以看到,有很多种文件,可以归结为下面几大类:
service 系统服务
target 多个unit组成的组
device 硬件设备
mount 文件系统挂载点
automount 自动挂载点
path 文件或路径
scope 不是由systemd启动的外部进程
slice 进程组
snapshot systemd快照
socket 进程间通信套接字
swap swap文件
timer 定时器
上面每种类型的文件都是一个unit
,这些unit
组成了系统的各个资源(各个服务、各个设备等)。
- 相关命令:
# systemctl list-units #列出正在运行(active)的unit,若要列出所有的units,则需要加 --all
# systemctl list-units --all #列出所有的(包括失败的或者inactive的)unit
# systemctl list-units --all --state=inactive #列出inactive的unit
# systemctl list-units --type=service #列出状态为active的service,其中failed是一个特例,也会列出来
# systemctl is-active crond.service #查看某个服务是否为active
target
target
类似于CentOS6的启动级别,但CentOS7支持多个target同时启动。target是多个unit的组合,系统使用target来管理unit。
# systemctl list-unit-files --type=target | head
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
final.target static
getty-pre.target static
命令systemctl list-unit-files --type=target
查看当前系统所有的target。
# systemctl list-dependencies multi-user.target | head
multi-user.target
● ├─auditd.service
● ├─brandbot.path
● ├─chronyd.service
● ├─crond.service
● ├─dbus.service
● ├─firewalld.service
● ├─irqbalance.service
● ├─kdump.service
● ├─network.service
命令systemctl list-dependencies target名
查看某一target包含的所有unit,以树形方式列出来。
# systemctl get-default
multi-user.target
命令systemctl get-default
查看系统默认的target,multi-user.target
等同于CentOS6的运行级别3。
# 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.
运行级别和target的对比:
- 级别0:关闭系统,对应 poweroff.target
- 级别1:单用户模式,对应 rescure.target
- 级别2:用户自定义级别,通常识别为级别3,对应 muiti-user.target
- 级别3:多用户模式,无图形,对应 muiti-user.target
- 级别4:用户自定义级别,通常识别为级别3,对应 muiti-user.target
- 级别5:多用户模式,有图形,对应 graphical.target
- 级别6:重启,对应 reboot.target
- 总结:
- 一个service属于一种unit;
- 多个unit组成了target;
- 一个target里面包含了多个service,我们可以查看
/usr/lib/systemd/system/sshd.service
文件里面[install]部分的内容,它定义了该service属于哪一个target。
更多资料参考: