修改10.19iptables规则备份和恢复 10.20firewalld的9个zone10.23 linux任务计划cron 10.24 chkconfig工具 10.25 systemd管理服务
七周五次10.19iptables规则备份和恢复 10.20firewalld的9个zone10.23 linux任务计划cron 10.24 chkconfig工具 10.25 systemd管理服务
10.19 iptables规则备份和恢复
10.20 firewalld的9个zone
10.21 firewalld关于zone的操作
10.22 firewalld关于service的操作
10.19 iptables规则备份和恢复
•service iptables save //会把规则保存到/etc/sysconfig/iptables
• 把iptables规则备份
iptables-save > 目标文件名
• 恢复备份的规则
iptables-restore < 目标文件名 iptables-restor < iptables.bak
10.20 firewalld的9个zone
•打开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
systemctl restart firewalld.service #重启firewalld
CentOS7使用firewalld打开关闭防火墙与端口
1、firewalld的基本使用
启动: systemctl start firewalld
关闭: systemctl stop firewalld
查看状态: systemctl status firewalld
开机禁用 : systemctl disable firewalld
开机启用 : systemctl enable firewalld
2.systemctl是CentOS7的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。
启动一个服务:systemctl start firewalld.service
关闭一个服务:systemctl stop firewalld.service
重启一个服务:systemctl restart firewalld.service
显示一个服务的状态:systemctl status firewalld.service
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl --failed
3.配置firewalld-cmd
查看版本: firewall-cmd --version
查看帮助: firewall-cmd --help
显示状态: firewall-cmd --state
查看所有打开的端口: firewall-cmd --zone=public --list-ports
更新防火墙规则: firewall-cmd --reload
查看区域信息: firewall-cmd --get-active-zones
查看指定接口所属区域: firewall-cmd --get-zone-of-interface=eth0
拒绝所有包:firewall-cmd --panic-on
取消拒绝状态: firewall-cmd --panic-off
查看是否拒绝: firewall-cmd --query-panic
那怎么开启一个端口呢
添加
firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,没有此参数重启后失效)
重新载入
firewall-cmd --reload
查看
firewall-cmd --zone= public --query-port=80/tcp
删除
firewall-cmd --zone= public --remove-port=80/tcp --permanent
• 查看所有zone
firewall-cmd --get-zones
• 查看默认zone(默认zone为public)
firewall-cmd --get-default-zone
• 9个zone的简介
10.21 firewalld关于zone的操作
• 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
• 设定默认zone
firewall-cmd --set-default-zone=zone的名称
• 查看指定网卡所使用的zone
firewall-cmd --get-zone-of-interface=网卡名称
firewall-cmd --get-zone-of-interface=ens33
• 给指定网卡设置zone
firewall-cmd --zone=zone名称 --add-interface=网卡名称
• 针对网卡更改zone
firewall-cmd --zone=zone名称 --change-interface=网卡名称
• 针对网卡删除zone
firewall-cmd --zone=zone名称 --remove-interface=网卡名称
firewall-cmd --zone=work --add-interface=ens33
• 查看系统所有网卡所在的zone
firewall-cmd --get-active-zones
10.22 firewalld关于service的操作
•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 //把http 删除 public
• ls /usr/lib/firewalld/zones/ //zone的配置文件模板
• firewall-cmd --zone=public --add-service=http --permanent //更改配置文件,之后会在/etc/firewalld/zones目录下面生成配置文件 firewall-cmd --zone=public --add-service=ftp --permanent
firewall-cmd --zone=public --list-services //查看刚刚添加到public 下面的 服务 ftp 和http
cat /etc/firewalld/zones/public.xml //
• 需求: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 //增加一行
• <service name="ftp"/>
• firewall-cmd --reload //重新加载
• firewall-cmd --zone=work --list-services
service:service是zone下面的子单元,可以理解为是指定的一个端口,防火墙的功能无外乎是针对一些端口进行限制,比如http操作的是80端口,https操作的是443端口,ssh操作的是22端口。
• 查看所有的services
firewall-cmd --get-services //结尾的s可加可不加
• 查看当前zone下有哪些service
firewall-cmd --list-services
• 把http增加到public zone下面
firewall-cmd --zone=public --add-service=http
• 把http从public zone下面删除
firewall-cmd --zone=public --remove-service=http
• 更改配置文件
firewall-cmd --zone=public --add-service=http --permanent
单纯修改规则只是在内存中修改,重启后会丢失,更改配置文件后,会在/etc/firewalld/zones目录下面生成配置文件,可以永久生效。
• zone的配置文件模板/usr/lib/firewalld/zones/
[[email protected] ~]# ls /usr/lib/firewalld/zones/
block.xml dmz.xml drop.xml external.xml home.xml internal.xml public.xml trusted.xml work.xml
• servies的配置文件模板/usr/lib/firewalld/services/
[[email protected]-5 ~]# ls /usr/lib/firewalld/services/
amanda-client.xml freeipa-replication.xml libvirt-tls.xml postgresql.xml spideroak-lansync.xml
amanda-k5-client.xml freeipa-trust.xml libvirt.xml privoxy.xml squid.xml
bacula-client.xml ftp.xml managesieve.xml proxy-dhcp.xml ssh.xml
bacula.xml ganglia-client.xml mdns.xml ptp.xml synergy.xml
bitcoin-rpc.xml ganglia-master.xml mosh.xml pulseaudio.xml syslog-tls.xml
bitcoin-testnet-rpc.xml high-availability.xml mountd.xml puppetmaster.xml syslog.xml
bitcoin-testnet.xml https.xml mssql.xml quassel.xml telnet.xml
bitcoin.xml http.xml ms-wbt.xml radius.xml tftp-client.xml
ceph-mon.xml imaps.xml mysql.xml RH-Satellite-6.xml tftp.xml
ceph.xml imap.xml nfs.xml rpc-bind.xml tinc.xml
cfengine.xml ipp-client.xml nrpe.xml rsh.xml tor-socks.xml
condor-collector.xml ipp.xml ntp.xml rsyncd.xml transmission-client.xml
ctdb.xml ipsec.xml openv*n.xml samba-client.xml vdsm.xml
dhcpv6-client.xml iscsi-target.xml ovirt-imageio.xml samba.xml vnc-server.xml
dhcpv6.xml kadmin.xml ovirt-storageconsole.xml sane.xml wbem-https.xml
dhcp.xml kerberos.xml ovirt-vmconsole.xml sips.xml xmpp-bosh.xml
dns.xml kibana.xml pmcd.xml sip.xml xmpp-client.xml
docker-registry.xml klogin.xml pmproxy.xml smtp-submission.xml xmpp-local.xml
dropbox-lansync.xml kpasswd.xml pmwebapis.xml smtps.xml xmpp-server.xml
elasticsearch.xml kshell.xml pmwebapi.xml smtp.xml
freeipa-ldaps.xml ldaps.xml pop3s.xml snmptrap.xml
freeipa-ldap.xml ldap.xml pop3.xml snmp.xml
• 系统中firewalld服务zone的配置文件/etc/firewalld/zones/
[[email protected] ~]# ls /etc/firewalld/zones/public.xml public.xml.old
注:在保存系统的firewalld服务配置文件时,系统会把旧的配置自动生成一个后缀名为.old的文件
• 系统中firewalld服务services的配置文件/etc/firewalld/services/
• 需求:ftp服务自定义端口1121,需要在work zone下面放行ftp
1.从servies的配置文件模板中将ftp服务的模板(ftp.xml)拷贝至系统中firewalld服务services的配置文件
cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
2.修改配置文件
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="21"/> //把21改为1121
<module name="nf_conntrack_ftp"/></service>
3.从zone的配置文件模板中将work服务的模板(work.xml)拷贝至系统中firewalld服务zone的配置文件
cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
4.修改配置文件
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>
5.重新加载
[[email protected]-5 ~]# firewall-cmd --reload
success
6.查看结果
[[email protected] ~]# firewall-cmd --zone=work --list-services
ssh dhcpv6-client ftp //成功添加ftp服务
firewall 2个角色 zone services
zone 规则集合 每个iptables 都有一个规则 每个zone 下面都有一些 services
八周一次课
10.23 linux任务计划cron
10.24 chkconfig工具
10.25 systemd管理服务
10.26 unit介绍
10.27 target介绍
扩展
1. anacron http://blog.****.net/strikers1982/article/details/4787226
2. xinetd服(默认机器没有安装这个服务,需要yum install xinetd安装) http://blog.sina.com.cn/s/blog_465bbe6b010000vi.html
3. systemd自定义启动脚本 http://www.jb51.net/article/100457.htm
10.23 linux任务计划cron
任务计划书写格式
[[email protected] ~]# cat /etc/crontab
SHELL=/bin/bash //定义shell变量
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 //编辑格式
5个*代表5个时间单位:分 时 日 月 周
“*”还代表全部的意思,如在第三位使用*代表每日,第四位使用“*”则代表每月
user-name 用户名(不写用户名,默认是root)
command 想要执行的命令
分范围0-59,时范围0-23,日范围1-31,月范围1-12,星期1-6(星期日可用0或7表示)
可用格式1-5表示一个范围1到5
可用格式1,2,3表示1或者2或者3
可用格式*/2表示被2整除的数字,比如小时,那就是每隔2小时
• 定义任务计划
crontab -e
注:定义任务计划时,编辑需要执行的命令的格式,最好均采用绝对路径,否则易出现问题
• 例:每个偶数月的1-10号中的周二和周五的03时00分执行脚本123.sh,并将正确日志追加重定向到/tmp/123.log,错误日志追加重定向到/tmp/456.log
0 3 1-10 */2 2,5 /bin/bash /user/loacl/sbin/123.sh >>/tmp/123.log 2>>/tmp/456.log
*/2 每2个月 1-10号的意思 2,5周2 周5的意思
• 要保证服务是启动状态,才能保证任务计划能够正常使用
systemctl start crond
• 检查服务是否正常启动
[[email protected]5 ~]# ps aux |grep cron
root 566 0.0 0.0 126232 1664 ? Ss 12:42 0:00 /usr/sbin/crond -n
root 1707 0.0 0.0 112676 984 pts/0 S+ 13:52 0:00 grep --color=auto cron
[[email protected]5 ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 五 2018-05-11 12:42:52 CST; 1h 11min ago //已经启动
Main PID: 566 (crond)
CGroup: /system.slice/crond.service
└─566 /usr/sbin/crond -n
5月 11 12:42:52 linux-5 systemd[1]: Started Command Scheduler.
5月 11 12:42:52 linux-5 systemd[1]: Starting Command Scheduler...
5月 11 12:42:52 linux-5 crond[566]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 93% if used.)
5月 11 12:42:52 linux-5 crond[566]: (CRON) INFO (running with inotify support)
• 列出任务计划
crontab -l
• 任务计划的文件位置(备份任务计划时,可直接拷贝该文件)
/var/spool/cron/username
• 指定用户
crontab -u username
• 删除任务计划
crontab -r
10.24 chkconfig工具
chkconfig:centos6以及6以前版本所使用的服务管理工具
Centos 6
cd /etc/init.d/ centos 6 开机启动脚本地方
• 列出所有服务
[[email protected]-5 ~]# 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:关
7个启动级别(centos6及以前):
0级别:关机
1级别: 单用户
2级别:多用户模式(不带nfs服务)
3级别: 多用户模式(不带图形)
4级别: 保留级别
5级别: 多用户(带有图形)
6级别: 重启
• 指定某一服务的某个级别开启或关闭
chkconfig --level 级别数 服务名称 on/off
chkconfig --level 3 network off
• 指定某一服务的多个级别开启或关闭
chkconfig --level 345 服务名称 on/off //345级别开启或关闭
• 添加某一服务到服务列表
chkconfig --add network
只有把启动脚本放到/etc/init.d/ (本次实验123) 才能添加到服务列表里去
• 从服务列表删除某一服务
chkconfig --del network
注:自定义服务文件必须放置在/etc/init.d目录下,且文件内容有一定的限制
必须是shell脚本
chkconfig: 2345 10 90 10位启动 90位关闭
description:描述表需要有
[[email protected] init.d]# vim !$
vim network
#! /bin/bash## network Bring up/down networking## chkconfig: 2345 10 90 //不能丢# description: Activates/Deactivates all network interfaces configured to \ //不能丢# start at boot time. //不能丢#### BEGIN INIT INFO# Provides: $network# Should-Start: iptables ip6tables NetworkManager-wait-online NetworkManager $network-pre# Short-Description: Bring up/down networking# Description: Bring up/down networking### END INIT INFO
# Source function library.
10.25 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:centos7的服务管理机制
• 列出所有类型为service的units
systemctl list-units --all --type=service
注:--all会显示出inactive的service
• 让服务开机启动
[[email protected]5 ~]# systemctl enable crond.service //.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,真正的配置文件目录位于/usr/lib/systemd/system/crond.service.
• 不让服务开机启动
[[email protected]5 ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
注:不让服务开机启动,会将软链接清除
• 查看状态
systemctl status crond
• 停止服务
systemctl stop crond
• 启动服务
systemctl start crond
• 重启服务
systemctl restart crond
• 检查服务是否开机启动
[[email protected]-5 ~]# systemctl is-enabled crond
enabled