docker建立私有倉庫
一 防火牆
1.防火牆 防火牆:防範一些網路攻擊。有軟體防火牆、硬體防火牆之分 防火牆的作用 防火牆具有很好的保護作用。入侵者必須首先穿越防火牆的安全防線,才能接觸目標計算機。 防火牆的功能 防火牆對流經它的網路通訊進行掃描,這樣能夠過濾掉一些攻擊,以免其在目標計算機上被執行。防火牆還可以關閉不使用的埠。而且它還能禁止特定埠的流出通訊。 最後,它可以禁止來自特殊站點的訪問,從而防止來自不明入侵者的所有通訊。 防火牆概念 防火牆一般有硬體防火牆和軟體防火牆 硬體防火牆:在硬體級別實現部分防火牆功能,另一部分功能基於軟體實現,效能高,成本高。 軟體防火牆:應用軟體處理邏輯運行於通用硬體平臺之上的防火牆,效能低,成本低。
2.firewalld防火牆的概念
### 1.區域
CentOS6x中防火牆叫做iptables
CentOS7.x 中預設使用的防火牆是firewalld,但是依然更多的是使用iptables,firewalld預設都關了。
firewalld增加了區域的概念,所謂區域是指,firewalld**預先準備了幾套防火牆策略的集合**,類似於**策略的模板**,使用者可以根據需求選擇區域。
常見區域及相應策略規則
2.firewalld防火牆的配置
1.檢視,開啟和停止firewalld服務 命令:systemctl 作用:管理服務 語法:#systemctl [選項] firewalld 選項: status:檢查指定服務的執行狀況 start:啟動指定服務 stop:停止指定服務 restart:重啟指定服務 reload:重新載入指定服務的配置檔案(並非所有服務都支援reload,通常使用restart) **使用systemctl來管理firewalld的服務,具體命令前面已經講過,只是服務名換成了firewalld,這裡不再贅述
查詢防火牆配置
關閉防火牆
2.管理firewall配置
命令:firewall-cmd 作用:管理firewall具體配置 語法:#firewall-cmd [引數選項1] ....[引數選項n] 常用選項: 檢視預設使用的區域 [root@linux0224 ~]# firewall-cmd --get-default-zone public 檢視所有可用區域 [root@linux0224 ~]# firewall-cmd --get-zones block dmz drop external home internal public trusted work 列出當前使用區域配置 [root@linux0224 ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens33 sources: services: ssh dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@linux0224 ~]# 列出所有區域的配置資訊 [root@linux0224 ~]# firewall-cmd --list-all-zones
新增允許通過的服務或埠(python,ntp)
你的linux機器,當前使用的是public區域的規則
預設信任的服務是,ssh,dhcp
準備一個web服務,通過python提供的簡單命令,
此時的防火牆,是沒有允許80埠的請求,進入到伺服器的,除非你加一個規則,允許80埠的請求通過。
新增埠:[root@linux0224 ~]# firewall-cmd --zone=public --add-port=80/tcp
success
此時客戶端已經可以正確訪問伺服器的80埠
去掉允許通過的埠
比如剛才你臨時個伺服器,添加了一個檔案下載的服務,需要訪問80埠
你現在不需要這個功能了,想去掉防火牆規則,繼續加強伺服器安全。
[root@linux0224 ~]# firewall-cmd --zone=public --remove-port=80/tcp
success
[root@linux0224 ~]# firewall-cmd --zone=public --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[root@linux0224 ~]#
此時再想訪問http://10.96.0.146/,你的請求就到不了伺服器
新增允許ntp的防火牆策略
[root@linux0224 ~]# firewall-cmd --permanent --add-service=ntp
success
[root@linux0224 ~]# firewall-cmd --reload
success
[root@linux0224 ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh dhcpv6-client ntp
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
檢查iptables語句
[root@linux0224 ~]# iptables -L |grep ntp
ACCEPT udp -- anywhere anywhere udp dpt:ntp ctstate NEW
**永久模式
permaent(永久性的)**
# 永久性新增規則,並未立即生效
root@linux0224 ~]# firewall-cmd --permanent --zone=public --add-port=80/tcp
success
[root@linux0224 ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh dhcpv6-client ntp
ports: # 埠還沒生效
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[root@linux0224 ~]#
#重新載入防火牆規則
[root@linux0224 ~]# firewall-cmd --reload
success
[root@linux0224 ~]# firewall-cmd --zone=public --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh dhcpv6-client ntp
ports: 80/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[root@linux0224 ~]#
root@linux0224 ~]# iptables -L |grep tcp
ACCEPT tcp -- anywhere anywhere tcp dpt:domain
ACCEPT tcp -- anywhere anywhere tcp dpt:bootps
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh ctstate NEW
ACCEPT tcp -- anywhere anywhere tcp dpt:http ctstate NEW
[root@linux0224 ~]#
總結:
使用防火牆命令,檢視系統提供了哪些模板
1.列出所有的區域模板
列出區域模板,以及具體的資訊
[root@linux0224 ~]# firewall-cmd --list-all-zones
列出所有的區域的名字
使用防火牆命令,檢視系統提供了哪些模板
[root@linux0224 ~]#firewall-cmd --get-zones
block dmz drop external home internal public trusted work
2.列出當前使用的區域是
[root@linux0224 ~]# firewall-cmd --get-default-zone
public
3.檢視當前的public區域,以及其詳細資訊
[root@linux0224 ~]# # 列出當前使用的區域,以及詳細資訊
[root@linux0224 ~]#
[root@linux0224 ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh dhcpv6-client ntp
ports: 80/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
4.先執行一個80埠的服務
[root@linux0224 ~]# python -m SimpleHTTPServer 80
5.給當前的防火牆區域,新增一個策略,允許80埠通過
[root@linux0224 ~]# firewall-cmd --add-port=80/tcp
success
6.再新增一個8000埠的規則,我們接觸的絕大多數,都是埠號/tcp 這個即可.
[root@linux0224 ~]# firewall-cmd --add-port=8000/tcp
success
7.刪除,新增的埠規則
[root@linux0224 ~]# firewall-cmd --remove-port=80/tcp
success
[root@linux0224 ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
8. 針對服務名新增,比如ntp服務
[root@linux0224 ~]# firewall-cmd --add-service=ntp
9. 檢視當前public區域,使用了哪些規則
[root@linux0224 ~]# firewall-cmd --list-all
10. firewalld,作用其實是新增iptables的規則
檢視系統上所有iptables的命令
iptables -L
tcp 是一個安全可靠的連線,需要雙向確認,客戶端,和服務端,都要確認對方以及連線上了
udp 是一個不可靠的額連線協議,客戶端可以隨便給服務端發,不需要對方確認
比如一個很差的網路環境下,網頁無法訪問,無法做dns解析(網路服務,網站服務,用的都是tcp協議)
但是qq可以收發訊息(qq用的是udp協議,以及ntp用的也是udp協議)
,檢視到firewalld命令,新增的防火牆規則如下
[root@linux0224 ~]# iptables -L |grep ntp
ACCEPT udp -- anywhere anywhere udp dpt:ntp ctstate NEW
1.關於firewalld如何新增nginx網站服務
關於防火牆策略的概念,其實就圍繞這個幾個點
- 來源ip 客戶端
- 目標ip 伺服器ip
- 目標埠 伺服器上哪個埠的程式
- 可以改為服務的名字
- sshd 服務 22
- mysql服務 3306
- 常見網站服務 httpd,nginx,預設埠是80
1.查詢支援的所有服務名字有哪些(nginx,httpd,統一被他製作為了 http服務,放行80埠)
firewall-cmd --get-services
2. 查詢當前區域,所用的服務名有哪些
firewall-cmd --list-services
3. 新增http服務,放行80埠即可
firewall-cmd --add-service=http
4.移除該服務,禁用80埠的請求
firewall-cmd --remove-service=http
5.建議,最好還是直接針對埠號,協議號,新增規則,
firewalld真不好用,不可用
iptables 支援很多複雜的引數,針對協議,來源埠,目標埠,等等
公有云的安全組(阿里雲提供的硬體防火牆),也是基於iptables這樣的規則新增的
2.firewalld允許ntp請求(注意ntpd有同步等待時間)
[root@linux0224 ~]# firewall-cmd --get-services |grep ntp
移除ntp的防火牆則
[root@linux0224 ~]# firewall-cmd --remove-service=ntp
success
允許客戶端,來這臺機器,進行ntp時間同步
[root@linux0224 ~]# firewall-cmd --add-service=ntp
success
二 學習定時任務,crontab
1.什麼是計劃任務: 後臺執行,到了預定的時間就會自動執行的任務,前提是:事先手動將計劃任務設定好。
週期性任務執行
清空/tmp目錄下的內容
mysql資料庫備份
redis資料備份
這就用到了crond服務。
計劃任務的作用
作用:
作業系統不可能24 小時都有人在操作,有些時候想在指定的時間點去執行任務(例如:每天凌晨 2 點去重新啟動Apache),此時不可能真有人每天夜裡 2 點去執行命令,這就可以交給計劃任務程式去執行操作了。
語法
crontab
-l 列出當前使用者有哪些計劃任務
-e 編輯當前使用者的計劃任務
-r 刪除當前使用者的計劃任務
[root@linux0224 ~]# crontab -l
no crontab for root
取值範圍(常識):
分:0~59
時:0~23
日:1~31
月:1~12
周:0~7,0 和 7 表示星期天
四個符號:
*:表示取值範圍中的每一個數字
-:做連續區間表示式的,要想表示1~7,則可以寫成:1-7
/:表示每多少個,例如:想每 10 分鐘一次,則可以在分的位置寫:*/10
,:表示多個取值,比如想在 1 點,2 點 6 點執行,則可以在時的位置寫:1,2,6
並且在定時任務裡,命令,請寫上絕對路徑
通過whereis命令搜尋 絕對路徑
[root@linux0224 ~]# whereis systemctl
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz
[root@linux0224 ~]#
案例
問題1:每月1、10、22 日的4:45 重啟network 服務
45 4 1,10,22 * * /usr/bin/systemctl restart network
問題2:每週六、週日的1:10 重啟network 服務
10 1 * * 6,7 /usr/bin/systemctl restart network
問題3:每天18:00 至23:00 之間每隔30 分鐘重啟network 服務
*/30 18-23 * * * /usr/bin/systemctl restart network
問題4:每隔兩天的上午8 點到11 點的第3 和第15 分鐘執行一次重啟
3,15 8-11 */2 * * /usr/sbin/reboot
問題5 :每天凌晨整點重啟nginx服務。
00 * * * * /usr/bin/systemctl restart nginx
問題6:每週4的凌晨2點15分執行命令
15 2 * * 4 command
問題7:工作日的工作時間內的每小時整點執行指令碼。
00 9-18 * * 1-5 /usr/bin/bash my.sh
2.擴充套件
① crontab 許可權問題
crontab是任何使用者都可以建立的計劃任務,但是超級管理員可以通過配置來設定某些使用者不允許設定計劃任務 。
==黑名單==配置檔案位於:/etc/cron.deny 裡面寫使用者名稱,一行只能寫一個
# 禁止yuchao01使用者設定定時任務
[root@linux0224 ~]# vim /etc/cron.deny
[root@linux0224 ~]#
[root@linux0224 ~]# cat /etc/cron.deny
yuchao01
# 切換yuchao01使用者登入
[yuchao01@linux0224 ~] $ crontab -e
You (yuchao01) are not allowed to use this program (crontab)
See crontab(1) for more information
==白名單==還有一個配置檔案,/etc/cron.allow (本身不存在,自己建立)
注意:白名單優先順序高於黑名單,如果一個使用者同時存在兩個名單檔案中,則會被預設允許建立計劃任務。
# 新增白名單後,會立即更新許可權
[root@linux0224 ~]# echo 'yuchao01' > /etc/cron.allow
# yuchao01就可以寫入了
[yuchao01@linux0224 ~]# ~]$ crontab -e
② 檢視計劃任務檔案儲存路徑
問題:計劃任務檔案具體儲存在哪裡呢?
答:/var/spool/cron/使用者名稱檔案中
,如果使用root使用者編輯計劃任務,則使用者檔名為root。
③ 檢視計劃任務日誌資訊
問題:在實際應用中,我們如何檢視定時任務執行情況?
答:通過計劃任務日誌,日誌檔案位於`/var/log/cron
最後,定時任務,crontab會在系統中,生成大量的郵件日誌,會佔用磁碟,因此我們都會關閉郵件服務即可
[root@linux0224 ~]# find / -type f -name 'post*.service'
/usr/lib/systemd/system/postfix.service
systemctl服務管理命令
[root@linux0224 ~]# systemctl list-units |grep post
postfix.service loaded active running Postfix Mail Transport Agent
systemctl status postfix
systemctl stop postfix
禁止開機自啟
systemctl disable postfix