精華:iptables和firewalld都不是防火牆,他們都只是管理防火牆的工具、服務而已!linux
重點:iptables防火牆策略規則是按照從上到下的順序匹配的,所以必定要把容許動做放到拒絕動做前面,不然全部的流量就將被拒絕掉,從而致使任何主機都沒法訪問咱們的服務vim
1.經常使用操做centos
iptables -I INPUT -s 網段/掩碼 -p tcp --dport 端口號 -j ACCEPT
iptables -A INPUT-p tcp --dport 端口號 -j REJECT
iptables -L
複製代碼
端口轉發
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
查看已有的防火牆規則鏈:
iptables -L
清空規則鏈
iptables -F
刪除用戶自定義 chain 或者全部用戶自定義chain (chain就是鏈的意思)
iptables -X
把 chain 或者全部 chain(當未指定 chain 名稱時)的包及字節的計數器清空
iptables -Z
複製代碼
2.在centos系統下想要保存iptables的規則鏈須要安裝iptables服務,由於centos下默認是firewalld服務沒有iptables,因此須要手動安裝iptables服務後重啓服務才行 如下兩個均可以保存iptables的配置bash
[root@b sysconfig]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[root@b sysconfig]#systemctl enable iptables
[root@b sysconfig]# systemctl restart iptables
複製代碼
3.把全部input規則鏈的默認策略設置拒絕服務器
iptables -P INPUT DROP
複製代碼
4.向INPUT鏈中添加容許ICMP流量進入的策略規則:markdown
iptables -I INPUT -p icmp -j ACCEPT
複製代碼
5.刪除INPUT規則鏈中剛剛加入的那條策略(容許ICMP流量)網絡
iptables -D INPUT 1
複製代碼
6.將INPUT規則鏈設置爲只容許指定網段的主機訪問本機的22端口,拒絕來自其餘全部主機的流量:app
iptables -I INPUT -s 192.168.10.0/24 -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j REJECT
iptables -L
複製代碼
7.向INPUT規則鏈中添加拒絕全部人訪問本機12345端口的策略規則:ssh
iptables -I INPUT -p tcp --dport 12345 -j REJECT
iptables -I INPUT -p udp --dport 12345 -j REJECT
iptables -L
複製代碼
8.向INPUT規則鏈中添加拒絕全部主機訪問本機1000~1024端口的策略規則:tcp
iptables -A INPUT -p tcp --dport 1000:1024 -j REJECT
iptables -A INPUT -p udp --dport 1000:1024 -j REJECT
iptables -L
複製代碼
精華:firewalld支持動態更新技術並加入了區域(zone)的概念
重點:區域就是我提早準備幾套防火牆策略針對不通的場景來用,在firewalld中能夠來回切換域,這樣就知足工做生產中不一樣的需求啦!
1.查看firewalld服務當前所使用的區域
firewall-cmd --get-default-zone
public
複製代碼
2.把firewalld服務的當前默認區域設置爲public
firewall-cmd --set-default-zone=public
success
firewall-cmd --get-default-zone
public
複製代碼
3.啓動/關閉firewalld防火牆服務的應急情況模式,阻斷一切網絡鏈接(當遠程控制服務器時請慎用):
firewall-cmd --panic-on
success
[root@linuxprobe ~]# firewall-cmd --panic-off
success
複製代碼
4.重點:流量轉發命令格式爲firewall-cmd --permanent --zone=<區域> --add-forward-port=port=<源端口號>:proto=<協議>:toport=<目標端口號>:toaddr=<目標IP地址>)*
firewall-cmd --permanent --zone=public --add-forward-port=port=888:proto=tcp:toport=22:toaddr=192.168.10.10
firewall-cmd --reload
success
複製代碼
5.咱們能夠在firewalld服務中配置一條規則,使其拒絕192.168.10.0/24網段的全部用戶訪問本機的ssh服務(22端口):
firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.10.0/24" service name="ssh" reject"
success
firewall-cmd --reload
success
複製代碼
精華: TCP Wrappers服務的防火牆策略由兩個控制列表文件所控制,用戶能夠編輯容許控制列表文件來放行對服務的請求流量,也能夠編輯拒絕控制列表文件來阻止對服務的請求流量。
重點文件: 控制列表文件(/etc/hosts.allow) 拒絕控制列表文件(/etc/hosts.deny)
編寫規則: 編寫拒絕策略規則時,填寫的是服務名稱,而非協議名稱; 建議先編寫拒絕策略規則,再編寫容許策略規則,以便直觀地看到相應的效果。
1.只容許192.168.10.0網段可以訪問sshd服務
vim /etc/hosts.deny
#
#hosts.deny This file contains access rules which are used to
#deny connections to network services that either use
#the tcp_wrappers library or that have been
#started through a tcp_wrappers-enabled xinetd.
#
#The rules in this file can also be set up in
#/etc/hosts.allow with a 'deny' option instead.
#
#See 'man 5 hosts_options' and 'man 5 hosts_access'
#for information on rule syntax.
#See 'man tcpd' for information on tcp_wrappers
sshd:
複製代碼
ssh 192.168.10.10
ssh_exchange_identification: read: Connection reset by peer
複製代碼
vim /etc/hosts.allow
#
# hosts.allow This file contains access rules which are used to
# allow or deny connections to network services that
# either use the tcp_wrappers library or that have been
# started through a tcp_wrappers-enabled xinetd.
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
sshd:192.168.10.
複製代碼
ssh 192.168.10.10
The authenticity of host '192.168.10.10 (192.168.10.10)' can't be established. ECDSA key fingerprint is 70:3b:5d:37:96:7b:2e:a5:28:0d:7e:dc:47:6a:fe:5c. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.10.10' (ECDSA) to the list of known hosts. root@192.168.10.10's password:
Last login: Wed May 4 07:56:29 2017
複製代碼
小結:三者之間有着類似的地方也有不一樣的地方,沒有必要把各類參數都背會,在實際工做作可以根據工做需求靈活的使用這些工具纔是王道!
4、實操演練 1.配置端口轉發 在系統 serverx 中配置端口轉發 在 172.25.x.0/24 網絡中的系統,訪問 server1 的本地端口 5423 將被轉發到 80 此設定時永久生效的
#1.容許172.25.4.0/24 網段的主機能夠訪問本機(添加到信任域)
[root@server4 ~]# firewall-cmd --permanent --add-source=172.25.4.0/24 --zone=trusted
success
複製代碼
#重啓火牆,注意:每此添加火牆策略都須要重啓
[root@server4 ~]# systemctl restart firewalld [root@server4 ~]# firewall-cmd --list-all --zone=trusted
#2.端口轉發
[root@server4 ~]# firewall-cmd --permanent --direct --add-rule ipv4 nat PREROUTING 1 -s 172.25.4.0/24 -p tcp --dport 5423 -j DNAT --to-dest :80
success
[root@server4 ~]# systemctl restart firewalld
[root@server4 ~]# firewall-cmd --direct --get-all-rules
ipv4 filter INPUT 1 -s 192.168.0.0/24 -p tcp --dport 22 -j REJECT
ipv4 nat PREROUTING 1 -s 172.25.4.0/24 -p tcp --dport 5423 -j DNAT --to-dest :80
複製代碼
**實驗測試:**
```bash
[root@server4 ~]# yum install -y httpd
[root@server4 ~]# systemctl start httpd
[root@server4 ~]# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
複製代碼
輸入:172.25.4.11 可訪問到apahce的默認發佈頁面(默認使用的是80端口) 輸入:172.25.4.11:5423 也可訪問到apahce的默認發佈頁面,即說明5423端口轉發到80端口