前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >学习笔记0418----系统管理

学习笔记0418----系统管理

作者头像
嘻哈记
发布2022-01-11 10:47:41
2K0
发布2022-01-11 10:47:41
举报
文章被收录于专栏:运维学习交流运维学习交流

系统管理

预习内容

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone 10.21 firewalld关于zone的操作 10.22 firewalld关于service的操作 10.23 linux任务计划cron 10.24 chkconfig工具 10.25 systemd管理服务 10.26 unit介绍 10.27 target介绍 扩展 firewalld自定义iptables规则 https://blog.51cto.com/jevic/1785162 提供一个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

1.iptables规则备份和恢复

iptables备份数据到配置文件/etc/sysconfig/iptables,使用如下命令

[root@linux-001 ~]# service  iptables  save

iptables备份规则到另外一个文件,而不是配置文件

[root@linux-001 ~]# iptables-save  > my.ipt
[root@linux-001 ~]# cat my.tpt 
# Generated by iptables-save v1.4.21 on Wed Apr 17 03:35:23 2019
*nat
:PREROUTING ACCEPT [267:82430]
:INPUT ACCEPT [260:81921]
:OUTPUT ACCEPT [25:8200]
:POSTROUTING ACCEPT [26:8252]
-A PREROUTING -d 192.168.141.128/32 -p tcp -m tcp --dport 1122 -j DNAT --to-destination 192.168.100.100:22
-A POSTROUTING -s 192.168.100.100/32 -j SNAT --to-source 192.168.141.128
COMMIT
# Completed on Wed Apr 17 03:35:23 2019
# Generated by iptables-save v1.4.21 on Wed Apr 17 03:35:23 2019
*filter
:INPUT ACCEPT [2610:326613]
:FORWARD ACCEPT [827:68276]
:OUTPUT ACCEPT [1817:173008]
COMMIT
# Completed on Wed Apr 17 03:35:23 2019
[root@linux-001 ~]# 

iptables恢复文件数据

[root@linux-001 ~]# iptables -t nat -F 
[root@linux-001 ~]# iptables -t nat
iptables v1.4.21: no command specified
Try `iptables -h' or 'iptables --help' for more information.
[root@linux-001 ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@linux-001 ~]# 
[root@linux-001 ~]# iptables-restore < my.tpt 
[root@linux-001 ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            192.168.141.128      tcp dpt:1122 to:192.168.100.100:22

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 SNAT       all  --  *      *       192.168.100.100      0.0.0.0/0            to:192.168.141.128
[root@linux-001 ~]#  

2.firewalld的9个zone

2.1开启firewalld

在之前的iptables中,我们关闭掉了firewalld,并且设置开机不可启动,安装了iptables-service,开启了iptables,并且设置了开机启动。这次我们反的操作,把iptables关闭掉,开启firewalld。

[root@linux-001 ~]# systemctl  stop iptables.service 
[root@linux-001 ~]# systemctl  disable  iptables.service 
Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.
[root@linux-001 ~]# 
[root@linux-001 ~]# systemctl enable firewalld.service 
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-001 ~]# systemctl start firewalld.service 
[root@linux-001 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   28  2056 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    1    92 INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    1    92 INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    1    92 INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_IN_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_IN_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_OUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_OUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 20 packets, 1904 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   20  1904 OUTPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain FORWARD_IN_ZONES (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 FWDI_public  all  --  ens37  *       0.0.0.0/0            0.0.0.0/0           [goto] 
    0     0 FWDI_public  all  --  ens33  *       0.0.0.0/0            0.0.0.0/0           [goto] 
    0     0 FWDI_public  all  --  +      *       0.0.0.0/0            0.0.0.0/0           [goto] 

Chain FORWARD_IN_ZONES_SOURCE (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD_OUT_ZONES (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 FWDO_public  all  --  *      ens37   0.0.0.0/0            0.0.0.0/0           [goto] 
    0     0 FWDO_public  all  --  *      ens33   0.0.0.0/0            0.0.0.0/0           [goto] 
    0     0 FWDO_public  all  --  *      +       0.0.0.0/0            0.0.0.0/0           [goto] 

Chain FORWARD_OUT_ZONES_SOURCE (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDI_public (3 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 FWDI_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FWDI_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FWDI_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain FWDI_public_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDI_public_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDI_public_log (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDO_public (3 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 FWDO_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FWDO_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FWDO_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain FWDO_public_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDO_public_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDO_public_log (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT_ZONES (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 IN_public  all  --  ens37  *       0.0.0.0/0            0.0.0.0/0           [goto] 
    1    92 IN_public  all  --  ens33  *       0.0.0.0/0            0.0.0.0/0           [goto] 
    0     0 IN_public  all  --  +      *       0.0.0.0/0            0.0.0.0/0           [goto] 

Chain INPUT_ZONES_SOURCE (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain IN_public (3 references)
 pkts bytes target     prot opt in     out     source               destination         
    1    92 IN_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    1    92 IN_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    1    92 IN_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain IN_public_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    1    92 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 ctstate NEW

Chain IN_public_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain IN_public_log (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination         
[root@linux-001 ~]# 
[root@linux-001 ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 1 packets, 328 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    1   328 PREROUTING_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    1   328 PREROUTING_ZONES_SOURCE  all  --  *      *       0.0.0.0/0             0.0.0.0/0           
    1   328 PREROUTING_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 OUTPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 POSTROUTING_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 POSTROUTING_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 POSTROUTING_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING_ZONES (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 POST_public  all  --  *      ens37   0.0.0.0/0            0.0.0.0/0           [goto] 
    0     0 POST_public  all  --  *      ens33   0.0.0.0/0            0.0.0.0/0           [goto] 
    0     0 POST_public  all  --  *      +       0.0.0.0/0            0.0.0.0/0           [goto] 

Chain POSTROUTING_ZONES_SOURCE (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain POST_public (3 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 POST_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 POST_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 POST_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain POST_public_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain POST_public_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain POST_public_log (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain PREROUTING_ZONES (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 PRE_public  all  --  ens37  *       0.0.0.0/0            0.0.0.0/0           [goto] 
    1   328 PRE_public  all  --  ens33  *       0.0.0.0/0            0.0.0.0/0           [goto] 
    0     0 PRE_public  all  --  +      *       0.0.0.0/0            0.0.0.0/0           [goto] 

Chain PREROUTING_ZONES_SOURCE (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain PREROUTING_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain PRE_public (3 references)
 pkts bytes target     prot opt in     out     source               destination         
    1   328 PRE_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    1   328 PRE_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    1   328 PRE_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain PRE_public_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain PRE_public_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain PRE_public_log (1 references)
 pkts bytes target     prot opt in     out     source               destination         
[root@linux-001 ~]# 

2.2 firewalld中的zone

firewalld 默认有 9 个 zone,默认的 zone 为 public,zone 可以理解为 firewalld 的单位:规则集。

## 查看firewalld种的zone ##
[root@linux-02 ~]# firewall-cmd --get-zones 
block dmz drop external home internal public trusted work
[root@linux-02 ~]# 

## 查看默认的zone是什么 ##
[root@linux-02 ~]# firewall-cmd --get-default-zone 
public
[root@linux-02 ~]# 

firewalld种zone的区别如下图:

在这里插入图片描述
在这里插入图片描述

3.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 ##
[root@linux-02 ~]# firewall-cmd --set-default-zone=work
success
[root@linux-02 ~]# firewall-cmd --get-default-zone 
work

## 查指定网卡的zone ##
[root@linux-02 ~]# firewall-cmd --get-zone-of-interface=ens33
work
[root@linux-02 ~]# firewall-cmd --get-zone-of-interface=ens37
work
[root@linux-02 ~]# firewall-cmd --get-zone-of-interface=lo
no zone


## 给指定网卡设置zone ##
[root@linux-02 ~]# firewall-cmd --zone=dmz --add-interface=ens37
The interface is under control of NetworkManager, setting zone to 'dmz'.
success
[root@linux-02 ~]# firewall-cmd --get-zone-of-interface=ens37
dmz


## 给指定网卡更改zone ##
[root@linux-02 ~]# firewall-cmd --zone=public --change-interface=ens37
The interface is under control of NetworkManager, setting zone to 'public'.
success
[root@linux-02 ~]# firewall-cmd --get-zone-of-interface=ens37
public


## 删除指定网卡的zone ##
[root@linux-02 ~]# firewall-cmd --zone=public --remove-interface=ens37
The interface is under control of NetworkManager, setting zone to default.
success
[root@linux-02 ~]# firewall-cmd --get-zone-of-interface=ens37
work


## 查看所有网卡的zone ##
[root@linux-02 ~]# firewall-cmd --get-active-zones 
work
  interfaces: ens33 ens37

4.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 ls /usr/lib/firewalld/zones/ //zone的配置文件模板 firewall-cmd --zone=public --add-service=http --permanent //更改配置文件,之后会在/etc/firewalld/zones目录下面生成配置文件

## 查看所有的service ##
[root@linux-02 ~]# firewall-cmd --get-service
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry docker-swarm dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master git gre high-availability http https imap imaps ipp ipp-client ipsec irc ircs iscsi-target jenkins kadmin kerberos kibana klogin kpasswd kprop kshell ldap ldaps libvirt libvirt-tls managesieve mdns minidlna mongodb mosh mountd ms-wbt mssql murmur mysql nfs nfs3 nmea-0183 nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius redis rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh syncthing syncthing-gui synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client upnp-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server zabbix-agent zabbix-server

## 查看当前zone下都有哪些service ##
[root@linux-02 ~]# firewall-cmd  --list-services 
ssh dhcpv6-client

## 查看public的zone下都有哪些service ##
[root@linux-02 ~]# firewall-cmd  --zone=public --list-services 
ssh dhcpv6-client

## 把http增加到public的zone下 ##
[root@linux-02 ~]# firewall-cmd  --zone=public --add-service=http 
success
[root@linux-02 ~]# firewall-cmd  --zone=public --list-services 
ssh dhcpv6-client http

## 把public所在的zone下的http service删除 ##
[root@linux-02 ~]# firewall-cmd  --zone=public --remove-service=http 
success
[root@linux-02 ~]# firewall-cmd  --zone=public --list-services 
ssh dhcpv6-client

## 把增加的service保存到配置文件当中去,配置文件在/etc/firewalld/zones/下 ##
[root@linux-02 ~]# firewall-cmd  --zone=public --add-service=http  --permanent 
success
[root@linux-02 ~]# ls /etc/firewalld/zones/
public.xml  public.xml.old
[root@linux-02 ~]# 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>

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

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

需求: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
[root@linux-02 ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/
[root@linux-02 ~]# 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>

[root@linux-02 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
[root@linux-02 ~]# 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@linux-02 ~]# firewall-cmd --reload 
success
[root@linux-02 ~]# firewall-cmd --zone=work --list-services
ssh dhcpv6-client ftp

5.linux任务计划cron

5.1 crontab 选项:

选项

含义

-u

用户

-e

创建定时任务

-l

查看定时任务

-r

删除定时任务

systemctl start crond.service

5.2 crontab 的格式

字段名称

说明

取值范围

分钟

每小时的第几分钟执行

0-59

小时

每日的第几个小时执行

0-24

每月的第几天执行

1-31

月份

每年的第几月执行

1-12

星期几

每周的第几天执行

0-6(0和7都代表星期日)

执行的命令

欲执行的命令及参数

## crontab格式:分钟 小时 日期 月份 星期几  执行命令 ##
[root@linux-02 ~]# 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@linux-02 ~]# crontab -e
 * * * * * command path  // 前五个字段可以取整数值,指定何时开始工作,第六个域是字符串,即命令字段,其中包括了crontab调度执行的命令。 各个字段之间用spaces和tabs分割。
 
 ## 查看定时任务 ##
 [root@linux-02 ~]# crontab -l
 30 2 * * * sh /root/oss.sh

5.3 crontab命令中的一些常用特殊符号:

符号

说明

*

表示任何时刻

,

表示分割,如第二段 2,5 代表2点和5点

-

表示一个段,如第二段里: 1-5,就表示1到5点

/n

表示每个n的单位执行一次,如第二段里,*/1, 就表示每隔1个小时执行一次命令。也可以写成1-23/1.

5.4 crontab 备份

每个用户创建的定时任务在 /var/spool/cron/此目录下以用户名命名,如果需要备份可以直接复制此文件即可。

6. Centos 7之前系统服务管理工具chkconfig

chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息。谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接。

6.1显示所有运行级系统服务的运行状态信息

[root@linux-02 ~]# 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.2 参数用法

–add  增加所指定的系统服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文件内增加相关数据。 –del  删除所指定的系统服务,不再由chkconfig指令管理,并同时在系统启动的叙述文件内删除相关数据。 –level<等级代号>  指定读系统服务要在哪一个执行等级中开启或关毕。 等级0表示:表示关机 等级1表示:单用户模式 等级2表示:无网络连接的多用户命令行模式 等级3表示:有网络连接的多用户命令行模式 等级4表示:不可用 等级5表示:带图形界面的多用户模式 等级6表示:重新启动 需要说明的是,level选项可以指定要查看的运行级而不一定是当前运行级。对于每个运行级,只能有一个启动脚本或者停止脚本。当切换运行级时,init不会重新启动已经启动的服务,也不会再次去停止已经停止的服务。

6.3 服务某个级别关闭

[root@linux-02 ~]# chkconfig --level 3 network off 
[root@linux-02 ~]# 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-02 ~]# chkconfig --level 345 network off
[root@linux-02 ~]# 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-02 ~]# 

6.4 删除一个服务

[root@linux-02 ~]# chkconfig  --del network
[root@linux-02 ~]# chkconfig  --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
[root@linux-02 ~]# 

6.5 增加一个服务

[root@linux-02 ~]# chkconfig  --add network
[root@linux-02 ~]# 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. Centos 7系统服务管理工具systemd

RHEL7里面,初始进程变成了systemd。RHEL6和之前的版本使用的初始进程是init,init是一个线性的启动过程,一个接一个的启动,比较慢;systemd则可以多进程启动,速度提高很多。另外,systemd还有简化了开发,集成日志等优点。

7.1列出所有的服务名称

[root@linux-02 ~]# systemctl list-unit-files
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-ask-password-console.path             static  
systemd-ask-password-plymouth.path            static  
systemd-ask-password-wall.path                static  
session-1.scope                               static  
session-32.scope                              static  
session-37.scope                              static  
arp-ethers.service                            disabled
auditd.service                                enabled 
autovt@.service                               enabled 
blk-availability.service                      disabled
brandbot.service                              static  
console-getty.service                         disabled
console-shell.service                         disabled
container-getty@.service                      static  
cpupower.service                              disabled
crond.service                                 enabled 
……
runlevel0.target                              disabled
runlevel1.target                              disabled
runlevel2.target                              enabled 
runlevel3.target                              enabled 
runlevel4.target                              enabled 
runlevel5.target                              static  
runlevel6.target                              disabled
shutdown.target                               static  
sigpwr.target                                 static  
sleep.target                                  static  
slices.target                                 static  
smartcard.target                              static  
sockets.target                                static  
sound.target                                  static  
suspend.target                                static  
swap.target                                   static  
sysinit.target                                static  
system-update.target                          static  
time-sync.target                              static  
timers.target                                 static  
umount.target                                 static  
fstrim.timer                                  disabled
systemd-readahead-done.timer                  indirect
systemd-tmpfiles-clean.timer                  static  

226 unit files listed.
lines 204-229/229 (END)

7.2 列出所有units,且type为service的项

[root@linux-02 ~]# systemctl list-units --all --type=service
  UNIT                                             LOAD      ACTIVE   SUB     DESCRIPTION
  auditd.service                                   loaded    active   running Security Auditing 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 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
  emergency.service                                loaded    inactive dead    Emergency Shell
● exim.service                                     not-found inactive dead    exim.service
  firewalld.service                                loaded    active   running firewalld - dynamic firewall daemon
  getty@tty1.service                               loaded    active   running Getty on tty1
● ip6tables.service                                not-found inactive dead    ip6tables.service
● ipset.service                                    not-found inactive dead    ipset.service
● iptables.service                                 not-found inactive dead    iptables.service
  irqbalance.service                               loaded    inactive dead    irqbalance daemon
  kdump.service                                    loaded    active   exited  Crash recovery kernel arming
  kmod-static-nodes.service                        loaded    active   exited  Create list of required static device nodes f
  microcode.service                                loaded    inactive dead    Load CPU microcode update
  network.service                                  loaded    active   exited  LSB: Bring up/down networking
  NetworkManager-wait-online.service               loaded    active   exited  Network Manager Wait Online
  NetworkManager.service                           loaded    active   running Network Manager
  plymouth-quit-wait.service                       loaded    inactive dead    Wait for Plymouth Boot Screen to Quit
  plymouth-quit.service                            loaded    inactive dead    Terminate Plymouth Boot Screen
  plymouth-read-write.service                      loaded    inactive dead    Tell Plymouth To Write Out Runtime Data
  plymouth-start.service                           loaded    inactive dead    Show Plymouth Boot Screen
……
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.

83 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.

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

systemctl enable crond.service //让服务开机启动 systemctl disable crond //不让开机启动 systemctl status crond //查看状态 systemctl stop crond //停止服务 systemctl start crond //启动服务 systemctl restart crond //重启服务 systemctl is-enabled crond //检查服务是否开机启动

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

8. unit介绍

Systemd由一个叫做单元(Unit)的概念,它保存了服务、设备、挂载点和操作系统其他信息的配置文件,并能够处理不同单元之间的依赖关系。大部分单元都静态的定义在单元文件中,也有一些是动态生成的。单元有多种状态:

  • 处于活动的则是(active),当前正在运行
  • 停止的则是(inactive),当前已经停止
  • 启动中的则是(activing),当前正在启动
  • 停止中的则是(deactiving),当前正在停止
  • 失败的则是(failed)状态,意思说单元启动过程中遇到错误比如找不到文件、路径或者进程运行中崩溃了等。

系统所有unit的文件位置 /usr/lib/systemd/system 一共有11中单元类型: service 系统服务 target 多个unit组成的组 device 硬件设备 mount 文件系统挂载点 automount 自动挂载点 path 文件或路径 scope 不是由systemd启动的外部进程 slice 进程组 snapshot systemd快照 socket 进程间通信套接字 swap swap文件 timer 定时器

单元类型

说明

service单元

用于封装一个后台服务进程,比如通过systemctl start firewalld启动防火墙,这种就属于service单元。

socket单元

用于封装一个后台服务进程,比如通过systemctl start firewalld启动防火墙,这种就属于service单元。

target单元

用于将多个单元在逻辑上组合在一起让它们同时启动。

device单元

用于封装一个设备文件,可用于基于设备启动。并不是每一个设备文件都需要一个device单元,但是每一个被udev规则标记的设备都必须作为一个device单元出现。

mount单元

用于封装一个文件系统挂载点(向后兼容/etc/fstab)

automount单元

用于封装一个文件系统自动挂载点,只有该文件系统被访问时才会进行挂载,它取代了传统的autofs服务。

timer单元

用于封装一个基于时间触发的动作,它取代了atd、crond等计划任务。

swap单元

用于封装一个交换分区或者交换文件,它与mount类似。

path单元

用于根据文件系统上特定对象的变化来启动其他服务。

slice单元

用于控制特定的CGroup内所有进程的总体资源占有。

scope单元

它与service单元类似,但是由systemd根据D-bus接口接收到的信息自动创建,可用于管理外部创建的进程。

9.target介绍

前面学习systemctl的时候,对service结尾的特定服务可以执行start,stop,restart等操作;另外还有一类后缀是以target的结尾,这些target 服务可以视作一批服务的集合,里面预定义了哪些服务运行,哪些不运行。这个概念非常类似RHEL6级之前的runlevel的概念。

查看系统中的target

在这里插入图片描述
在这里插入图片描述

查看指定target包含哪些unit:

在这里插入图片描述
在这里插入图片描述

RHEL6之前的版本,可以在/etc/rc.d里面查看7个运行级别,每个运行级别对应不同的功能,比如0是关机,1是单用户,2是多用户(没网络),3是多用户,4没用,5是图形界面,6是重启。 可以使用 init 0 直接关机等等。RHEL7里面,target有类似等同的定义:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

查看默认的target:

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

查看一个service属于哪个target

在这里插入图片描述
在这里插入图片描述

课后总结

1. 系统放行一个80端口

 ## 使用firewall 添加80端口 ##
 [root@linux-001 ~]# firewall-cmd  --add-port=80/tcp
 
 ## 使用iptables添加80端口 ##
 [root@linux-001 ~]# iptables -A INPUT -p tcp  -dport 80 -j ACCEPT
    
 ## firewall 添加的80端口保存在配置文件中并且重新加载生效使其主机重启也生效 ##
[root@linux-001 ~]#  firewall-cmd --permanent  --add-port=80/tcp && firewall-cmd --reload

 ## firewall 添加http 服务保存在配置文件中并且重新加载生效使其主机重启也生效 ##
[root@linux-001 ~]#  firewall-cmd --permanent --add-service=http && firewall-cmd --reload

2.firewall-cmd 如何添加自定义规则

以下只是针对自定义规则的简单说明和举例,其实也就是帮助文档里面的东西;详细的说明自行参考 firewall-cmd --help

传递的参数 与 iptables, ip6tables 以及 ebtables 一致!

Centos7 Firewall 用户操作接口依然调用系统内核的iptables模块来设定规则!

直接选项

–direct需要是直接选项的第一个参数。

将命令传递给防火墙。参数 可以是 iptables, ip6tables 以及 ebtables 命令行参数。

firewall-cmd --direct --passthrough { ipv4 | ipv6 | eb }

为表 增加一个新链 。

firewall-cmd --direct --add-chain { ipv4 | ipv6 | eb }

从表 中删除链 。

firewall-cmd --direct --remove-chain { ipv4 | ipv6 | eb }

查询 链是否存在与表 . 如果是,返回0,否则返回1.

firewall-cmd --direct --query-chain { ipv4 | ipv6 | eb }

如果启用,此命令将有返回值。此命令没有输出信息。

获取用空格分隔的表 中链的列表。

firewall-cmd --direct --get-chains { ipv4 | ipv6 | eb }

为表 增加一条参数为 的链 ,优先级设定为 。

firewall-cmd --direct --add-rule { ipv4 | ipv6 | eb }

参考示例:

方式-:[reload 生效,修改后重启才可生效]

firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 1 -s 192.168.1.0/24 -p tcp --dport=22 -j ACCEPT

firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 2 -p tcp --dport=22 -j DROP

方式二:[重载或重启后失效]

iptables -t filter -I INPUT_direct -s 192.168.1.20 -p tcp --dport=22 -j ACCEPT

iptables -A INPUT_direct -p tcp --dport=22 -j DROP

从表 中删除带参数 的链 。

firewall-cmd --direct --remove-rule { ipv4 | ipv6 | eb }

firewall-cmd --direct --remove-rule ipv4 filter INPUT 2 -p tcp --dport=22 -j DROP

查询带参数 的链 是否存在表 中

firewall-cmd --direct --query-rule { ipv4 | ipv6 | eb }

获取表 中所有增加到链 的规则,并用换行分隔。

firewall-cmd --direct --get-rules { ipv4 | ipv6 | eb }

此部分扩展链接:https://blog.51cto.com/jevic/1785162

3.系统服务启动顺序

在这里插入图片描述
在这里插入图片描述

Linux内核加载启动后,用户空间的第一个进程就是初始化进程,这个程序的物理文件约定位于/sbin/init,当然也可以通过传递内核参数来让内核启动指定的程序。这个进程的特点是进程号为1,代表第一个运行的用户空间进程。不同发行版采用了不同的启动程序,主要有以下几种主流选择:

以 Ubuntu 为代表的 Linux 发行版采用 upstart CentOS7.0 版本之前的 System V init CentOS7.0 版本的 systemd

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 系统管理
  • 预习内容
    • 1.iptables规则备份和恢复
      • 2.firewalld的9个zone
        • 2.1开启firewalld
        • 2.2 firewalld中的zone
      • 3.firewalld关于zone的操作
        • 4.firewalld中关于service的操作
          • 5.linux任务计划cron
            • 5.1 crontab 选项:
            • 5.2 crontab 的格式
            • 5.3 crontab命令中的一些常用特殊符号:
            • 5.4 crontab 备份
          • 6. Centos 7之前系统服务管理工具chkconfig
            • 6.1显示所有运行级系统服务的运行状态信息
            • 6.2 参数用法
            • 6.3 服务某个级别关闭
            • 6.4 删除一个服务
            • 6.5 增加一个服务
          • 7. Centos 7系统服务管理工具systemd
            • 7.1列出所有的服务名称
              • 7.2 列出所有units,且type为service的项
                • 7.3 几个常用的服务相关的命令
                  • 8. unit介绍
                    • 9.target介绍
                    • 课后总结
                      • 1. 系统放行一个80端口
                        • 2.firewall-cmd 如何添加自定义规则
                        • 3.系统服务启动顺序
                        相关产品与服务
                        命令行工具
                        腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档