1. 程式人生 > >10.11-10.14 網絡相關 firewall,netfilter,5表5鏈,iptables

10.11-10.14 網絡相關 firewall,netfilter,5表5鏈,iptables

網絡相關 firewall netfilter

七周三次課(3月21日)
10.11 Linux網絡相關
10.12 firewalld和netfilter
10.13 netfilter5表5鏈介紹
10.14 iptables語法

擴展(selinux了解即可)
1. selinux教程 技術分享圖片http://os.51cto.com/art/201209/355490.htm
2.selinux pdf電子書 技術分享圖片http://pan.baidu.com/s/1jGGdExK


10.11 Linux網絡相關


介紹

技術分享圖片

ifconfig查看網卡ip(yum install net-tools)

[root@centos7 ~]# ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.189.128 netmask 255.255.255.0 broadcast 192.168.189.255

inet6 fe80::243c:86d7:d85e:224d prefixlen 64 scopeid 0x20<link>

ether 00:0c:29:15:53:53 txqueuelen 1000 (Ethernet)

RX packets 44 bytes 6786 (6.6 KiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 78 bytes 9431 (9.2 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536

inet 127.0.0.1 netmask 255.0.0.0

inet6 ::1 prefixlen 128 scopeid 0x10<host>

loop txqueuelen 1 (Local Loopback)

RX packets 64 bytes 5344 (5.2 KiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 64 bytes 5344 (5.2 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


ifup ens33/ifdown ens33 開啟、關閉端口

註意,這裏如果是遠程登錄(xhsell,putty)的話,在遠程登錄處down了ens33端口的話,會使遠程登錄中斷,

需要在本機(虛擬機,linux本機)再up起來。


單獨針對一個網卡去做更改需要用到ifup/ifdown命令。

指定更改端口設置後可以使用此命令。


添加&&並且條件命令,可以讓端口斷掉馬上再次開啟起來,此時不會斷掉遠程終端。

[root@centos7 ~]# ifdown ens33 && ifup ens33

成功斷開設備 'ens33'。

連接已成功激活(D-Bus 活動路徑:/org/freedesktop/NetworkManager/ActiveConnection/2)


設定虛擬網卡ens33:1

[root@centos7 ~]# cd /etc/sysconfig/network-scripts/

[root@centos7 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:0

復制ens33網卡配置文件,改名ifcfg-ens33\:0 (\是脫義)


更改33:0文件,把name,device,和dns更改,添加對應參數:0,其他不變。

[root@centos7 network-scripts]# vi ifcfg-ens33:0

NAME=ens33:0

DEVICE=ens33:0

IPADDR=192.168.189.150


[root@centos7 ~]# ifdown ens33 && ifup ens33

[root@centos7 ~]# ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.189.128 netmask 255.255.255.0 broadcast 192.168.189.255

inet6 fe80::243c:86d7:d85e:224d prefixlen 64 scopeid 0x20<link>

ether 00:0c:29:15:53:53 txqueuelen 1000 (Ethernet)

RX packets 629 bytes 61934 (60.4 KiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 512 bytes 64012 (62.5 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.189.150 netmask 255.255.255.0 broadcast 192.168.189.255

ether 00:0c:29:15:53:53 txqueuelen 1000 (Ethernet)


lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536

inet 127.0.0.1 netmask 255.0.0.0

inet6 ::1 prefixlen 128 scopeid 0x10<host>

loop txqueuelen 1 (Local Loopback)

RX packets 64 bytes 5344 (5.2 KiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 64 bytes 5344 (5.2 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


由此可見,多了ens33:0的網卡信息。

還在可以windows上面ping通ens33:0的ip,並且可以利用Xshell等遠程工具去遠程連接它的22端口,


查看網卡連接狀態

mii-tool ens33 查看網卡是否連接

此用法,用在檢查機房端口是否亮。


[root@centos7 ~]# mii-tool ens33

ens33: negotiated 1000baseT-FD flow-control, link ok

link ok表示連接狀態。如果現實no link,說明網卡壞了或者沒有連接網線。


如果上述命令不能使用,可以使用#ethtool

#ethtool ens33

[root@centos7 ~]# ethtool ens33

Settings for ens33:

Supported ports: [ TP ]

Supported link modes: 10baseT/Half 10baseT/Full

100baseT/Half 100baseT/Full

1000baseT/Full

Supported pause frame use: No

Supports auto-negotiation: Yes

Advertised link modes: 10baseT/Half 10baseT/Full

100baseT/Half 100baseT/Full

1000baseT/Full

Advertised pause frame use: No

Advertised auto-negotiation: Yes

Speed: 1000Mb/s

Duplex: Full

Port: Twisted Pair

PHYAD: 0

Transceiver: internal

Auto-negotiation: on

MDI-X: off (auto)

Supports Wake-on: d

Wake-on: d

Current message level: 0x00000007 (7)

drv probe link

Link detected: yes

如果網卡沒有連接,link detected 會顯示no.


更改主機名 hostnamectl set-hostname hostname 此命令不適用與CentOS7之前的系統

[root@centos7 ~]# hostnamectl set-hostname centos7-01

[root@centos7 ~]# hostname 查看hostname名字

centos7-01

[root@centos7 ~]# bash 如果想使設置生效,可以重新登進登出。或者#bash進入子shell

[root@centos7-01 ~]# exit 生效退出子shell

exit

[root@centos7 ~]# cat /etc/hostname hostname的配置文件

centos7-01


DNS配置文件/etc/resolv.conf 此文件的參數可以在網卡配置文件發生更改而更改。

[root@centos7 ~]# cat /etc/resolv.conf

# Generated by NetworkManager

nameserver 114.114.114.114

如果#vi /etc/resolv.conf,更改了resolv.conf的參數之後,重啟了服務器或者服務,參數都會被網卡配置文件的參數所覆蓋掉。所以resolv.conf只適用於臨時更改。


/etc/hosts文件 作用是臨時解析某個域名,只在本機下生效。

格式是 左邊ip 右邊域名

一個IP可以跟多個域名,可以是幾十個,甚至上百個,用空格分隔。

每一行只能有一個IP,也就是說一個域名不能對應多個IP。

如果有多行中出現相同的域名(對應IP不一樣),會按最近出現的記錄來解析。

示例:

# vim /etc/hosts


127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.189.128 www.qq.com


[root@centos7 ~]# ping www.qq.com

PING www.qq.com (192.168.189.128) 56(84) bytes of data.

64 bytes from www.qq.com (192.168.189.128): icmp_seq=1 ttl=64 time=0.054 ms

64 bytes from www.qq.com (192.168.189.128): icmp_seq=2 ttl=64 time=0.055 ms

64 bytes from www.qq.com (192.168.189.128): icmp_seq=3 ttl=64 time=0.432 ms




10.12 firewalld和netfilter

技術分享圖片

selinux臨時關閉 setenforce 0

#setenforce 0


selinux永久關閉 vi /etc/selinux/config

更改配置文件/etc/selinux/config,把SELINUX=enforcing改成SELINUX=disabled,


#vim /etc/selinux/config



# 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

# 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


更改完成該配置文件後,重啟系統方可生效。可以使用getenforce命令獲得SELINUX的狀態,

[root@centos7 ~]# getenforce

Enforcing

[root@centos7 ~]# setenforce 0

[root@centos7 ~]# getenforce

Permissive


Enforcing與Permissive的區別在於,

Permissive selinux開啟,遇到需要發送阻斷的時候,不需要真正阻斷,它只是一個提醒。

!!!!!SELINUX一般都處於關閉狀態。因為開啟SELINUX會增加很多運維成本,很多服務受限於SELINUX。

把SELINUX關閉不會有太多安全問題。


centos7之前使用netfilter防火墻

centos7防火墻改名,firewalld

倆基制不一樣,但是內部工具iptables是一樣的,可以通過iptables去添加一些規則。例如把80端口開放,把22端口開放,把8080端口關閉,這時候可以使用iptables去實現,增加一定的規則即可。

不管是firewalld還是netfilter,他們都主持iptables命令的。


在centos7上,firewalld是默認開啟的。所以我們需要把firewalld關閉掉,再開啟netfilter。

#systemctl stop firewalld 關閉firewalld服務


#systemctl disable firewalled 禁止firewalld服務開機啟動


#yum install -y iptables-services 安裝iptables-services,這樣就可以之前版本的iptables了


#systemctl enable iptables 讓iptables開機啟動


#systemctl start iptables 啟動iptables服務




10.13 netfilter5表5鏈介紹


技術分享圖片

技術分享圖片

netfilter的5個表

filter表用於過濾包,最常用的表,有INPUT、FORWARD、OUTPUT三個鏈

默認的表,內置3個鏈

INPUT鏈作用於進入本機的包。數據包進入本機,之後做的相關操作。


OUTPUT鏈作用於本機送出的包。本本機產生數據包,出去之前做的操作。


FORWARE鏈作用於那些根本機無關的包。把目標地址進行更改,轉發等等操作。



nat表用於網絡地址轉換,有PREROUTING、OUTPUT、POSTROUTING三個鏈

PREROUTING鏈 在進來的那一刻更改數據包

OUTPUT鏈與filter的OUTPUT是一樣的

POSTROUTING鏈 在出的的那一刻更改數據包


nat用法實例

有ABC三臺機器,

A機器a網卡,公網連接。B機器b網卡公網,c網卡私網。C機器d卡,私網。

此時A想訪問C的80端口,應該怎麽做?

因為B可以跟C進行通訊,所以可以在B機器上面做iptables,做個映射端口8088,映射到C的80端口

當A訪問B的8088端口時候,可以直接映射到C的80端口上。

這時候,需要用到PREROUTING和POSTROUTING。


managle表用於給數據包做標記,幾乎用不到



raw表可以實現不追蹤某些數據包,阿銘從來不用


security表在centos6中並沒有,用於強制訪問控制(MAC)的網絡規則,阿銘沒用過


參考文章 http://www.cnblogs.com/metoy/p/4320813.html


總結:

如果是本機的數據包會經過PREROUTING,INPUT,OUTPUT,POSTROUTING,

如果不是本機的,PREROUTING,FORWARD,POSTROUTING.



10.14 Linux防火墻-netfilter


技術分享圖片


查看iptables規則:iptables -nvL

[root@centos7 ~]# service iptables restart 重啟iptables服務。

Redirecting to /bin/systemctl restart iptables.service

[root@centos7 ~]# iptables -nvL

[root@centos7 ~]# iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

31 2124 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED

0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0

0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0

0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22

0 0 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)

pkts bytes target prot opt in out source destination

0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited


Chain OUTPUT (policy ACCEPT 18 packets, 1736 bytes)

pkts bytes target prot opt in out source destination



[root@centos7 ~]# cat /etc/sysconfig/iptables 規則文件,默認規則。

cat /etc/sysconfig/iptables

# sample configuration for iptables service

# you can edit this manually or use system-config-firewall

# please do not ask us to add additional ports/services to this default configuration

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT


iptables -F 清空規則

[root@centos7 ~]# iptables -F

[root@centos7 ~]# iptables -nvL

Chain INPUT (policy ACCEPT 5 packets, 388 bytes)

pkts bytes target prot opt in out source destination


Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination


Chain OUTPUT (policy ACCEPT 4 packets, 448 bytes)

pkts bytes target prot opt in out source destination


service iptables save

把配置好的規則保存到配置文件裏面。

#service iptables save


# service iptables restart 重啟服務,會重新加載配置文件的規則。


iptables -t nat //-t指定表


iptables -Z 可以把計數器清零


#iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP

如果沒有-t 指的是針對filter表來操作。

此命令的意思就是,添加一條規則,針對INPUT的鏈。指定來源IP 來源IP 來源端口 目的IP 目的端口 -j 丟包


-A 表示增加一條規則。

-D 表示刪除一條規則

-I 表示插入一條規則,效果跟-A一樣。只是順序不同,-I會比-A的位置要前。所以-I會被優先過

-P 表示指定協議,可以是tcp udp或者icmp

--dport 跟-p一起使用,表示指定目標端口

--sport 跟-p一起使用,表示指定源端口

-s 表示源IP(可以是一個IP段)

-d 表示指定目的IP(可以是一個IP段)

-j 後面跟動作,其中ACCEPT表示允許包,DROP表示丟掉包,REJECT表示拒絕包。

-i 表示指定網卡(少用)

DROP把數據包扔掉,不會再看數據包,直接丟掉。(常用)

REJECT拒絕數據包,數據包來了先看一下,然後反饋信息,再扔掉。

兩效果一樣,相當於把IP封掉。


[root@centos7 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP

[root@centos7 ~]# iptables -nvL

Chain INPUT (policy ACCEPT 25 packets, 1744 bytes)

pkts bytes target prot opt in out source destination

0 0 DROP tcp -- * * 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80


-I插入規則,對比-A不同的是,-I的位置比-A前,會被優先過濾。

[root@centos7 ~]# iptables -I INPUT -p tcp --dport 80 -j DROP



刪除規則

[root@centos7 ~]# iptables -D INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP


刪除規則還有另一個方法,

首先添加和插入規則-A和-I

[root@centos7 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP

[root@centos7 ~]# iptables -I INPUT -p tcp --dport 80 -j DROP

添加首選行編號表示

[root@centos7 ~]# iptables -nvL --line-numbers

Chain INPUT (policy ACCEPT 72 packets, 5160 bytes)

num pkts bytes target prot opt in out source destination

1 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80

2 0 0 DROP tcp -- * * 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80


刪除第2條規則,利用編號刪除

[root@centos7 ~]# iptables -D INPUT 2

[root@centos7 ~]# iptables -nvL --line-numbers

Chain INPUT (policy ACCEPT 27 packets, 1840 bytes)

num pkts bytes target prot opt in out source destination

1 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80


iptables -I/-A/-D INPUT -s 1.1.1.1 -j DROP



iptables -I INPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT

插入規則


iptables -nvL --line-numbers

編號顯示規則


iptables -D INPUT 1

刪除規則


iptables -P INPUT DROP

默認規則

[root@centos7 ~]# iptables -nvL

Chain INPUT (policy ACCEPT 61 packets, 4381 bytes)

pkts bytes target prot opt in out source destination

0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80


Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination


Chain OUTPUT (policy ACCEPT 34 packets, 4104 bytes)

pkts bytes target prot opt in out source destination

鏈一般有默認的policy,policy ACCEPT表示這個鏈,不加規則的話,例如OUTPUT鏈沒有任何規則,所以OUTPUT的數據包,它就由默認的策略來決定,這個默認的策略就是ACCEPT。

*所有的數據包,只要是沒有具體的規則來匹配,那它就走默認的策略。


默認策略改成DROP

[root@centos7 ~]# iptables -P OUTPUT DROP

這個一般不要在遠程終端執行,因為如果執行了此命令。 因為默認的規則一旦被DROP掉,那麽當前窗口就會被禁掉。因為數據包和22端口通信,通信完後,需要反饋數據給客戶端(Xshell),結果Xshell OUTPUT出來的包到達不了Xshell 所以它接收不了數據,最終導致斷開。


如果斷開了 在本機(虛擬機、服務器)給OUTPUT的默認策略改回ACCEPT即可。

[root@centos7 ~]# iptables -P OUTPUT ACCEPT

10.11-10.14 網絡相關 firewall,netfilter,5表5鏈,iptables