1. 程式人生 > >2018-06-11筆記(日常運維二)

2018-06-11筆記(日常運維二)

linux

10.12 firewalld和netfilter

(1)selinux
安全增強型 Linux(Security-Enhanced Linux)簡稱 SELinux,它是一個 Linux 內核模塊,也是 Linux 的一個安全子系統,seinux是Linux系統特有的安全機制,不過因為限制太多,所以很少人使用,一般我們都是關閉SELinux。

臨時關閉:setenforce 0; 永久關閉:編輯配置文件 vim /etc/selinux/config,找到SELINUX=enforcing 改成 SELINUX=disabled。重啟後生效!

\# This file controls the state of SELinux on the system.
\# SELINUX= can take one of these three values:
\#     enforcing - SELinux security policy is enforced.
\#     permissive - SELinux prints warnings instead of enforcing.
\#     disabled - No SELinux policy is loaded.
SELINUX=disabled                    ###修改這一行為disabled
\# SELINUXTYPE= can take one of three two values:
\#     targeted - Targeted processes are protected,
\#     minimum - Modification of targeted policy. Only selected processes are protected.
\#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

關於SELINUX的運行狀態
1:enforcing #開啟狀態,會強制執行SELINUX的安全策略
2:permissive #提示狀態,會打印觸發SELINUX安全策略的警告,但是不會執行相應的策略。
3:disabled #關閉SELINUX,重啟後也不會再啟動SELINUX
可以使用genenforce命令獲得當前selinux的狀態

 [root@luo ~]# getenforce 
Disabled

(2)netfilter
CentOS 5和6 的防火墻是netfilter;主要是使用iptables工具實現防火墻。
CentOS7使用的是firewalld,在這裏先關閉firewalld服務,使用CentOS6的netfilter演示。更換操作如下:

systemctl stop firewalld 關閉firewalld服務
systemctl disable firewalld 禁止firewalld服務開機啟動
yum install -y iptables-services 安裝iptables-services
systemctl enable iptables 讓它開機啟動
systemctl start iptables 啟動iptables服務

[root@luo ~]# systemctl stop firewalld
[root@luo ~]# systemctl disable firewalld.
[root@luo ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
   Docs: man:firewalld(1)
[root@luo ~]# yum install -y iptables-services^C               #這裏已經安裝過了就不再安裝了
[root@luo ~]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@luo ~]# systemctl start iptables

(3)firewalld
CentOS7 的防火墻使用firewalld,不是netfilter

10.13 netfilter5表5鏈介紹

5表:filter表、nat表、mangle表、raw表、security表
5鏈:PEROUTING、INPUT、FORWARD、OUTPUT、POSTROUTING
下面介紹5表5鏈:
filter表:主要用於過濾包,是系統預設的表。平時用的最多的表。該表內建3個鏈:IUPUT、OUTPUT、FORWARD,INPUT鏈作用進入本機的包,OUTPUT鏈作用於本機送出的包,FOEWARD作用於跟本機無關的包
nat表:主要用於網絡地址轉換,它也有3個鏈。PREROUTING鏈的作用是在包剛剛到達防火墻時改變它的目的地址(非必須),OUTPUT鏈的作用是改變本地包的目的地址,POSTROUTING鏈的作用是在包即將離開防火墻時改變其源地址。偶爾用到!
mangle表:主要用於給數據包做標記,然後根據標記去操作相應的包。(運維幾乎不用,網絡工程師用)
raw表:可以實現不追蹤某些數據包,默認系統的數據都會被跟蹤。(也是幾乎不用)
security表:CentOS6沒有的,它用於強制訪問控制(MAC)的網絡規則。(了解即可)
PEROUTING:數據包進入路由表之前
INPUT:通過路由表後目的地為本機
FORWARD:通過路由表後,目的地不為本機
OUTPUT:有本機產生,向外轉發
POSTROUTING:發送到網卡接口之前
iptables數據包流向:
技術分享圖片
表和鏈對應的關系:
技術分享圖片

10.14 iptables語法

查看規則:iptables -t nat -nvL; 規則保存在/etc/sysconfig/iptables文件中;-t 指定表名;-nvL表示查看該表規則,-n表示不針對IP反解析主機名,-L表示列出,-v表示列出更加詳細。不加-t,默認打印filter表的相關信息。
清空規則:iptables -F,臨時清空,重啟後會還原配置iptables文件的規則;
保存規則:service iptables save
包及流量計數器置零:iptables -Z
增加/刪除一條規則,用法:iptables -A INPUT -s 192.168.128.1 -p tcp --sport 1234 -d 192.168.128.28 --dport 80 -j DROP
各個選項的作用:
-A/-D 表示增加/刪除一條規則
-I 表示插入一條規則,其實效果跟-A一樣(執行順序不一樣)
-p 表示指定協議,可以是tcp、udp或者icmp
-P 後面跟鏈名,它表示預設策略; 如iptables -P INPUT DROP; (使用這個選項是要小心,特別是遠程服務器)
--dport:跟-p一起使用,表示指定目標端口
--sport:跟-p一起使用,表示指定源端口
-s 表示指定源IP(可以是一個IP段)
-d 表示指定目標IP(可以是一個IP段)
-j 後面跟動作,其中ACCEPT表示允許包,DROP表示丟棄包,REJECT表示拒絕包
-i 表示指定網卡
舉例:
①插入一條規則,把來自2.2.2.2的所有數據包丟棄:iptables -I INPUT -s 2.2.2.2 -j DROP
②下面是刪除上面這條規則,註意刪除規則時,必須和插入的規則一致,也就是說兩條命令,除了-I和-D不一樣,其他地方都是一樣的:iptables -D INPUT -s 2.2.2.2 -j DROP
③把來自2.2.2.2並且是TCP協議到本機80端口的數據包丟棄:iptables -I INPUT -s 2.2.2.2 -p tcp --dport 80 -j DROP; 註意--dport/--sport必須和-p一起使用,否則會報錯。
④把發送到10.0.0.21的22端口的數據包丟棄:iptables -I OUTPUT -p tcp -dport 22 -d 10.0.0.21 -j DROP
有時候服務器上iptables過多,你想刪除一條規則,但又忘記了創建是的規則,怎麽刪除呢?其實還有一種簡單的方法:
iptables -nvL --line-numbers查看規則的number

[root@luo ~]# iptables -nvL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       94  6648 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
4        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
5       17  1634 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)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

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

然後你想刪除某一條規則:iptables -D INPUT 1; -D後面跟鏈名、規則num,這個num就是上面的數字
規則選項說明:

-i或–in-interface <網絡接口名>:指定數據包從哪個網絡接口進入,如ppp0、eth0和eth1等

-o或–out-interface <網絡接口名>:指定數據包從哪塊網絡接口輸出,如ppp0、eth0和eth1等

-p或—proto協議類型 < 協議類型>:指定數據包匹配的協議,如TCP、UDP和ICMP等

-s或–source <源地址或子網>:指定數據包匹配的源地址

–sport <源端口號>:指定數據包匹配的源端口號,可以使用“起始端口號:結束端口號”的格式指定一個範圍的端口

-d或–destination <目標地址或子網>:指定數據包匹配的目標地址

–dport <目標端口號>:指定數據包匹配的目標端口號,可以使用“起始端口號:結束端口號”的格式指定一個範圍的端口

動作選項說明:

ACCEPT:接受數據包

DROP:丟棄數據包

REDIRECT:與DROP基本一樣,區別在於它除了阻塞包之外, 還向發送者返回錯誤信息。

SNAT:源地址轉換,即改變數據包的源地址

DNAT:目標地址轉換,即改變數據包的目的地址

MASQUERADE:
    IP偽裝,即是常說的NAT技術,
    MASQUERADE只能用於ADSL等撥號上網的IP偽裝,也就是主機的IP是由ISP分配動態的;
    如果主機的IP地址是靜態固定的,就要使用SNAT

LOG日誌功能,將符合規則的數據包的相關信息記錄在日誌中,以便管理員的分析和排錯

10.15 iptables filter表案例

需求:只針對filter表,預設策略INPUT鏈DROP,其他兩個鏈ACCEPT,然後針對192.168.66.0/24放行22端口,所有網段開放80端口,對所有網段開放21端口
可以編寫一個shell腳本來實現。
vim /usr/local/sbin/iptables.sh ##編輯腳本文件,加入以下內容

#! /bin/bash              #定義執行腳本的shell
IPT="/usr/sbin/iptables"  #定義變量,iptables命令的絕對路徑。
$IPT -F                   #清空iptables規則
$IPT -P INPUT DROP        #默認規則,丟棄所有數據包。
$IPT -P OUTPUT ACCEPT     #默認規則,放行所有OUTPUT鏈的數據包
$IPT -P FORWARD ACCEPT    #默認規則,放行所有FORWARD鏈的數據包
##下面這條規則允許通過RELATED和ESTABLISHED狀態的數據包,這條規則必加,否則可能導致某些服務連不上。
$IPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT  
$IPT -A INPUT -s 192.168.66.0/24cp --dport 22 -j ACCEPT  #僅允許10.1.1.0/24網段鏈接22端口
$IPT -A INPUT -p tcp --dport 80 -j ACCEPT    #允許通過所有80端口的數據包
$IPT -A INPUT -p tcp --dport 21 -j ACCEPT    #允許通過所有21端口的數據包
[root@luo ~]# vi /usr/local/bin/iptables.sh
[root@luo ~]# cat !$
cat /usr/local/bin/iptables.sh
#! /bin/bash             
IPT="/usr/sbin/iptables" 
$IPT -F                  
$IPT -P INPUT DROP       
$IPT -P OUTPUT ACCEPT    
$IPT -P FORWARD ACCEPT   
$IPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 
$IPT -A INPUT -s 192.168.66.0/24 -p tcp --dport 22 -j ACCEPT  
$IPT -A INPUT -p tcp --dport 80 -j ACCEPT    
$IPT -A INPUT -p tcp --dport 21 -j ACCEPT

執行這個腳本後查看添加的規則:

[root@luo ~]# iptables -nvL
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   33  2188 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     tcp  --  *      *       192.168.66.0/24      0.0.0.0/0            tcp dpt:22
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:21

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

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

關於icmp的包有一個常見的應用(請牢記):

iptables -I INPUT -p icmp --icmp-type 8 -j DROP

這裏--icmp-type選項要跟-p icmp一起使用,後面指定類型編號。這個8指的是本機ping通其他機器,而其他機器不能ping通本機。

10.16-10.18nat表應用:

A機器兩塊網卡ens33(192.168.66.130)、ens37(192.168.66.1),ens33可以上外網,ens37僅僅是內部網絡,B機器只有ens37(192.168.100.100),和A機器ens37可以通信互聯。
需求1:可以讓B機器連接外網
A機器上打開路由轉發 echo "1">/proc/sys/net/ipv4/ip_forward

[root@luo ~]# echo "1" > /proc/sys/net/ipv4/ip_forward
[root@luo ~]# cat /proc/sys/net/ipv4/ip_forward
1

A上執行 iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE

[root@luo ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE

B上設置網關為192.168.100.1

route add default gw 192.168.100.1  #設置一個默認網關

echo "nameserver 119.29.29.29" > /etc/resolv.conf  #添加一個臨時的DNS服務器

這樣B機器就可以連外網了

[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=128 time=6.99 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=3 ttl=128 time=13.3 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=4 ttl=128 time=7.54 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=5 ttl=128 time=6.98 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=6 ttl=128 time=9.47 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=7 ttl=128 time=8.52 ms

需求2:端口轉發

讓PC能遠程登錄B

添加規則1,將訪問192.168.66.130:1122端口的數據包轉發給192.168.100.100:22端口
[root@luo ~]# iptables -t nat -A PREROUTING -d 192.168.66.130 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22

添加規則2,將來源於192.168.100.100的數據包的源地址轉換為192.168.66.130
[root@luo ~]# iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.66.130

然後遠程連接192.168.66.130:1122就可以間接的遠程登錄B主機了。

Connecting to 192.168.66.130:1122...
Connection established.

擴展
selinux教程 http://os.51cto.com/art/201209/355490.htm
selinux pdf電子書 http://pan.baidu.com/s/1jGGdExK
iptables應用在一個網段 http://www.aminglinux.com/bbs/thread-177-1-1.html
sant,dnat,masquerade http://www.aminglinux.com/bbs/thread-7255-1-1.html
iptables限制syn速率 http://www.aminglinux.com/bbs/thread-985-1-1.html http://jamyy.us.to/blog/2006/03/206.html

2018-06-11筆記(日常運維二)