0418第十五次課:平常運維-防火牆、定時任務、系統管理

平常運維-防火牆、定時任務

1、iptables的備份及恢復

  • iptables 規則備份mysql

    防火牆規則只存在內存中,並沒保存在文件中。若是系統重啓以前設定的規則就沒有了,因此設定完成後須要保存一下。linux

    service iptables save //會備份到/etc/sysconfig/iptables文件中sql

    service iptables save > 文件名 //指定文件備份shell

  • 恢復vim

    iptables-restore < 備份的文件名 //恢復備份文件中的規則centos

    步驟:bash

    清理nat表規則服務器

    iptables -t nat -F運維

    iptables -t nat -nvLssh

    恢復備份文件:

    iptables-restore < test.tpt

2、使用firewalld

  • 開啓firewalld

    systemctl stop iptables.service

    systemctl disable iptables.service

    systemctl enable firewalld.service

  • firewalld默認9個zone(zone爲firewalld的單位,默認爲pulic)

  • 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 --remove-interface=lo //刪除指定網卡zone

3、linux任務計劃

linux中任務計劃必不能夠少,它能夠定時執行一些備份數據、重啓服務器等操做,操做多是一個腳本或者命令。

  • crontab任務計劃命令:

    • 格式:分 時 日 月 周 user command

      分範圍:0-59,時範圍:0-60,日範圍:0-31,月範圍1-12,周範圍:1-7

      指定範圍:1-5 ,指定1到5範圍 能夠用1,2,3表示1或者2或者3

      */2表示被2整除的數字,好比小時,那就是每隔2小時

  • 注意點:

  1. 文件/var/spool/cron/username
  2. 可用格式1-5表示一個範圍1到5
  3. 可用格式1,2,3表示1或者2或者3
  4. 可用格式*/2表示被2整除的數字,好比小時,那就是每隔2小時
  5. 要保證服務是啓動狀態 systemctl start crond.service
  • 計劃任務配置文件

    cat /etc/crontab

    文件中有幾個環境變量:

    SHELL=/bin/bash

    PATH:環境變量,命令的路徑

    MAILTO:發郵件給誰

  • 格式說明

# 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  //須要執行的命令
  • 新增定時任務

    crontab -e //進入編輯模式,和vim同樣,按i進行編輯

  • 示例:

    1.新增一個凌晨3點執行的腳本,*表明全部的意思。而且把日誌和錯誤日誌輸出

    0 3 1-10 */2 * /bin/bash /usr/local/sbin/test.sh /tmp/test.log 2>> /tmp/test2.log

    2.新增一個周2、週五才執行的腳本: 0 3 * * 2,5 /bin/bash /usr/local/sbin/test.sh /tmp/test.log 2>> /tmp/test2.log

    若是想用年份能夠用星期肯定惟一性,好比說今年的6月18號和明年的6月18號的星期確定是不一樣的

  • 啓動定時任務

    須要啓動crond服務,定時任務纔會生效

    systemctl start crond.service

    檢查是否啓動成功

    ps aux |grep cron

    或者:systemctl status crond 查看,若是狀態是綠色就說明成功了

  • 任務計劃不執行的緣由分析

    寫了定時任務,crond服務也是正常,可是就是不執行。

    緣由可能爲:腳本中命令未使用絕對路徑,解決:要麼將命令寫一個絕對路徑,要麼將命令的路徑加入到PATH變量裏面去

    建議追加一個日誌,這樣就能夠根據日誌來判斷。

  • 任務計劃備份

    crontab文件存在位置/var/spool/cron/username,須要備份的時候直接複製便可。

4、chkconfig工具

  • chkconfig工具是linux服務的管理工具,cron、iptables、firewalld、mysql等都是服務。chkconfig能夠管理這些服務開機是否啓動等。

  • chkconfig工具在centos6和以前的版本中使用,centos7中已經逐漸的不在使用了,爲了過分centos7還可使用,可是後面趨勢是會被遺棄。

  • 列出系統全部服務

    chkconfig --list

    0-6 表示7個啓動級別(centos6及之前):

    0:關機 1: 單用戶 2:多用戶模式(不帶nfs服務) 3: 多用戶模式(不帶圖形) 4: 保留級別 5: 多用戶(帶有圖形) 6: 重啓

  • 須要注意的是:啓動腳本須要放到/etc/init.d/目錄下

  • 關閉network服務

    chkconfig network off

  • 指定某一服務的某個級別開啓或關閉

    chkconfig --level 3 network off //指定第三級別network服務關閉

    chkconfig --level 345 network on //指定network中的3,4,5級別開啓

  • 將一個腳本加入到服務列表列表中

    首先把腳本放到/etc/init.d/目錄下

    腳本名稱沒有要求,可是格式有要求:

    一、必須是一個shell腳本

    二、需指定運行級別,第10位開啓,第90關閉

    示例:init.d目錄下 cp network服務 隨便起一個名字,進行添加

    cp network 123

    chkconfig --add 123

    結果:

    刪除:chkconfig --del 123

5、systemd管理服務

  • 幾個經常使用的服務相關的命令

    systemctl enable crond.service //讓服務開機啓動

    systemctl disable crond //不讓開機啓動

    systemctl status crond //查看狀態

    systemctl stop crond //中止服務

    systemctl start crond //啓動服務

    systemctl restart crond //重啓服務

    systemctl is-enabled crond //檢查服務是否開機啓動

    在centos6或以前的版本中用chkconfig工具去管理系統的服務,在centos7中使用systemd。

  • 列出全部的service

  • 設置服務開機啓動

    systemctl enable crond.service //.service 能夠不加

  • 關閉開機啓動

    systemctl disable crond

  • 查看狀態

    systemctl status crond

  • 啓動服務

    systemctl start crond

  • 中止服務

    systemctl stop crond

  • 檢查服務是否開機啓動

    systemctl is-enabled crond

###6、unit介紹

  • ssystemctl 狀態若是是enabled 會在 /usr/lib/systemd/system 目錄下生產軟鏈接,這些文件都叫作unit。

  • unit分爲如下類型:

    service 系統服務

    target 多個unit組成的組

    device 硬件設備

    mount 文件系統掛載點

    automount 自動掛載點

    path 文件或路徑

    scope 不是由systemd啓動的外部進程

    slice 進程組

    snapshot systemd快照

    socket 進程間通訊套接字

    swap swap文件

    timer 定時器

  • unit相關的命令

    列出正在運行的unit:

    systemctl list-units

    列出全部的unit:

    systemctl list-units --all

    列出inactive狀態的unit

    systemctl list-units --all --state=inactive

    列出狀態爲active的service

    systemctl list-units --type=service

    查看某個服務是否爲active

    systemctl is-active crond.service

7、target介紹

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

  • target相關的命令

    列出系統中全部的target:

    systemctl list-unit-files --type=target

    查看指定target下面有哪些unit

    systemctl list-dependencies multi-user.target

    查看系統默認的target

    systemctl get-default

    設置默認的target

    systemctl set-default multi-user.target

  • 一個service屬於一種類型的unit

  • 多個unit組成了一個target

  • 一個target裏面包含了多個service

  • cat /usr/lib/systemd/system/sshd.service 看[install]部分

8、課堂筆記

  • firewalld-cmd添加規則示例

    firewall-cmd --add-port=80/tcp

    firewall-cmd --permanent --add-port=80/tcp; firewall-cmd --reload firewall-cmd --permanent --add-service=http; firewall-cmd --reload

  • firewall-cmd 如何添加自定義規則

  • centos各個版本服務器啓動

相關文章
相關標籤/搜索