2018-08-28

10.19 iptables規則備份和恢復

備份linux

[root@localhost ~]# iptables-save > /tmp/ipt.bk

恢復bash

[root@localhost ~]# iptables-restore < /tmp/ipt.bk
10.20 firewalld的9個zone

打開firewalldssh

[root@localhost ~]# systemctl start firewalld.service 
[root@localhost ~]# systemctl enable firewalld.service

關閉iptablessocket

[root@localhost ~]# systemctl stop iptables.service 
[root@localhost ~]# systemctl disable iptables.service

firewalld有9個zone,默認zone爲publictcp

查看全部zone工具

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

查看默認的zonerest

[root@localhost ~]# firewall-cmd --get-default-zone
public
區域 默認規則策略
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 拒絕流入的流量,除非與流出的流量相關
10.21 firewalld關於zone的操做

設置默認zonecode

[root@localhost ~]# firewall-cmd --set-default-zone=work
success

查看指定網卡zonexml

[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33
work

給指定網卡設置zonedns

[root@localhost ~]# firewall-cmd --zone=dmz --add-interface=ens33
The interface is under control of NetworkManager, setting zone to 'dmz'.
success

更改指定網卡的zone

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

移除指定網卡的zone

[root@localhost ~]# firewall-cmd --zone=work --remove-interface=ens33
The interface is under control of NetworkManager, setting zone to default.
success

查看全部網卡的zone

[root@localhost ~]# firewall-cmd --get-active-zones
10.22 firewalld關於service的操做

查看全部的servies

[root@localhost ~]# firewall-cmd --get-services

查看當前zone下有哪些service

[root@localhost ~]# firewall-cmd --list-services

把http增長/移除到public zone下面

[root@localhost ~]# firewall-cmd --zone=public --add-service=http
[root@localhost ~]# firewall-cmd --zone=public --remove-service=http

zone的配置文件模板

ls /usr/lib/firewalld/zones/

更改配置文件,以後會在/etc/firewalld/zones目錄下面生成配置文件

firewall-cmd --zone=public --add-service=http --permanent

需求:ftp服務自定義端口1121,須要在work zone下面放行ftp

[root@localhost ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
[root@localhost ~]# vi /etc/firewalld/services/ftp.xml //port改成1121
<port protocol="tcp" port="1121"/>
[root@localhost ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
[root@localhost ~]# vi /etc/firewalld/zones/work.xml //增長一行
<service name="ftp"/>
[root@localhost ~]# firewall-cmd --reload //從新加載
[root@localhost ~]# firewall-cmd --zone=work --list-services
10.23 linux任務計劃cron

配置文件

[root@localhost ~]# 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@localhost ~]# crontab -e

查看已有計劃

[root@localhost ~]# crontab -l

刪除計劃

[root@localhost ~]# crontab -r

指定用戶計劃

[root@localhost ~]# crontab -u root

備份,直接複製如下目錄便可

/var/spool/cron/$username

格式

分範圍0-59,時範圍0-23,日範圍1-31,月範圍1-12,周1-7  
可用格式1-5表示一個範圍1到5  
可用格式1,2,3表示1或者2或者3  
可用格式*/2表示被2整除的數字,好比小時,那就是每隔2小時
10.24 chkconfig工具

列出服務

chkconfig --list

指定級別關閉

chkconfig network off  //所有關閉
chkconfig --level 3 network off 
chkconfig --level 345 network off

添加指定服務,讓chkconfig能夠管理

/etc/init.d/ //啓動腳本放入此目錄
chkconfig --del network
chkconfig --add network
10.25 systemd管理服務

查看全部服務

systemctl list-unit-files

查看全部service

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 //檢查服務是否開機啓動
10.26 unit介紹
ls /usr/lib/systemd/system //系統全部unit,分爲如下類型
service 系統服務 
target 多個unit組成的組
device 硬件設備
mount 文件系統掛載點
automount 自動掛載點
path 文件或路徑
scope 不是由systemd啓動的外部進程
slice 進程組
snapshot systemd快照
socket 進程間通訊套接字
swap  swap文件
timer 定時器
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
10.27 target介紹

系統爲了方便管理用target來管理unit

[root@localhost ~]# systemctl list-unit-files --type=target

查看指定target下面有哪些unit

[root@localhost ~]# systemctl list-dependencies multi-user.target

查看系統默認的target

[root@localhost ~]# systemctl get-default

設置默認的target

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

一個service屬於一種類型的unit
多個unit組成了一個target
一個target裏面包含了多個service

查看指定unit屬於哪一個target

[root@localhost ~]# cat /usr/lib/systemd/system/sshd.service
...
[Install]
WantedBy=multi-user.target
本站公眾號
   歡迎關注本站公眾號,獲取更多信息