1. 程式人生 > >31.網路相關 firewalld、netfilter 5表5鏈 iptables語法

31.網路相關 firewalld、netfilter 5表5鏈 iptables語法

10.12 firewalld和netfilter

10.13 netfilter5表5鏈介紹

10.14 iptables語法

(拓展:末尾有埠對映,jpg)

'

 

 

10.11 Linux網路相關:

~1. ifconfig 檢視網絡卡ip (yum install net-tools)

centos7預設沒有ifconfig這個命令,需要安裝。ifconfig檢視起來更加清爽

但是可以用ip addr 來檢視

ifconfig -a 當你的網絡卡down掉的時候,或者沒有ip的時候,ifconfig是不顯示的。加上-a就可以顯示

~2.ifup ens33/ifdown ens33

ifdown ens33 ens33這個網絡卡斷掉(也叫down掉)

ifup ens33 down掉之後,ifup重新連線

用在增加一個網絡卡或者更改一個閘道器,但不想重啟全部的網路服務,就可以用ifdown ifup指定一個來用。遠端連線的時候不要ifdown掉,但是可以這樣用ifdown ens33 && ifup ens33

~3. 設定虛擬網絡卡ens33:1

後面lvs keepalived的時候,會用到這個虛擬網絡卡

操作步驟詳見例項3.

~4.mii-tool ens33 檢視網絡卡是否連線

我們連線了網線,不在機房(可看燈有沒有亮),遠端連線的時候,檢查是否網絡卡有沒有插入網線

mii-tool ens33 結果是不是link ok

~5. ethtool ens33 也可以檢視網絡卡是否連線

如果沒有mii-tool可以用 ethtool ens33 ,檢查最後一行是不是Link detected: yes

~6.更改主機名 hostnamectl set-hostname aminglinux

centos7使用的

更改之後,他的配置檔案在 /etc/hostname

~7. DNS配置檔案 /etc/resolv.conf

如果我們更改DNS,就更改網絡卡的配置檔案就可以了。因為/etc/resolv.conf也是網絡卡的配置檔案(/etc/sysconfig/network-scripts/ens33)定義的。如果在/etc/resolv.conf更改DNS,當然也可以vim,但是重啟之後會被網絡卡的配置檔案所覆蓋掉,適合臨時更改。這個需要注意

~8. /etc/hosts檔案

這個檔案是linux和windows都有的。後面的lamp做實驗,訪問一個自定義的域名的時候就用到這個檔案,但是在windows上更改的

假如,我們ping www.qq.123.com,他是一個公網IP。但是我想讓他在我們的本機不要訪問那個公網IP,要讓他訪問到192.168..30.134來,可以直接vim /etc/hosts,在最下面一行直接寫 192.168.30.134 www.qq123.com。我們在ping就會到我們設定這個IP來,立即生效,但只在本機生效。可支援一個IP多個域名。同一個域名設定了多個IP,以最後一次設定的IP為準。以行為單位,左邊IP,右邊域名,以空格分割

 

 

 

例項:

3.

[root@axinlinux-01 ~]# cd /etc/sysconfig/network-scripts/ 我們先cd進網絡卡的配置檔案

[root@axinlinux-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:1 cp ens33重新命名為ens:1。要脫意一下冒號

[root@axinlinux-01 network-scripts]# vim ifcfg-ens33:1 修改一下ens:1的配置

vim ifcfg-ens33\:0

 

TYPE=Ethernet

BOOTPROTO=static

DEFROUTE=yes

NAME=ens33:0 找到NAME這一行名字改為ens:1(配置檔案裡不需要脫意)

UUID=d116f724-9bfb-4573-9f5b-7dace5640272

DEVICE=ens33 找到DEVICE這一行名字改為ens:1(配置檔案裡不需要脫意)

ONBOOT=yes:o

IPADDR=192.168.30.137

NETMASK=255.255.255.0

GATEWAY=192.168.30.2 將閘道器刪掉

DNS33=14.114.114.114 將DNS33刪掉,因為已經有一個DNS33了

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33:1
UUID=8ea80c7d-64b5-42aa-9ece-7b07528f4373
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.30.134
NETMASK=255.255.255.0

~                   

[root@axinlinux-01 network-scripts]# ifdown ens33:1 && ifup ens33:1 指定重啟ens33:1這個網絡卡

[root@axinlinux-01 network-scripts]# ifconfig 我們在ifconfig一下

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.30.134  netmask 255.255.255.0  broadcast 192.168.30.255
        inet6 fe80::181d:8196:fb2c:cb62  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:a0:e1:ed  txqueuelen 1000  (Ethernet)
        RX packets 10299  bytes 1703052 (1.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7133  bytes 572196 (558.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.30.137  netmask 255.255.255.0  broadcast 192.168.30.255
        ether 00:0c:29:a0:e1:ed  txqueuelen 1000  (Ethernet)

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.30.130  netmask 255.255.255.0  broadcast 192.168.30.255
        inet6 fe80::7398:c3c4:f2a6:1ede  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:a0:e1:f7  txqueuelen 1000  (Ethernet)
        RX packets 78488  bytes 5688301 (5.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 25428  bytes 1684172 (1.6 MiB)
        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 1000  (Local Loopback)
        RX packets 8019  bytes 736515 (719.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8019  bytes 736515 (719.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

4.5.

[root@axinlinux-01 network-scripts]# mii-tool ens33

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

[root@axinlinux-01 network-scripts]# ethtool ens33

ens33: negotiated 1000baseT-FD flow-control, link ok
[root@afeilinux-01 ~]# 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
	Supported FEC modes: Not reported
	Advertised link modes:  10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	                        1000baseT/Full 
	Advertised pause frame use: No
	Advertised auto-negotiation: Yes
	Advertised FEC modes: Not reported
	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

 

6.

[root@axinlinux-01 network-scripts]# cat /etc/hostname

axinlinux-01

 

8.

[root@axinlinux-01 network-scripts]# cat /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.30.130 axinlinux-01

192.168.30.131 axinlinux-02
192.168.30.134 www.wangxin.com axin.com

[root@axinlinux-01 network-scripts]# vim !$

vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.30.130 axinlinux-01

192.168.30.131 axinlinux-02
192.168.30.134 www.wangxin.com axin.com

 

[root@axinlinux-01 network-scripts]# ping www.wangxin.com

PING www.wangxin.com (192.168.30.134) 56(84) bytes of data.
64 bytes from www.wangxin.com (192.168.30.134): icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from www.wangxin.com (192.168.30.134): icmp_seq=2 ttl=64 time=0.049 ms
64 bytes from www.wangxin.com (192.168.30.134): icmp_seq=3 ttl=64 time=0.083 ms
64 bytes from www.wangxin.com (192.168.30.134): icmp_seq=4 ttl=64 time=0.083 ms
64 bytes from www.wangxin.com (192.168.30.134): icmp_seq=5 ttl=64 time=0.041 ms
^C
--- www.wangxin.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4002ms
rtt min/avg/max/mdev = 0.041/0.067/0.083/0.018 ms

 

 

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 

10.12 firewalld和netfilter:

 

linux防火牆-netfilter

~1. selinux臨時關閉 setenforce o

我們之前金鑰認證的時候就要臨時關閉

~2. selinux永久關閉 vi /etc/selinux/config

vi之後,將 SELINUX=enforcing 改為 disabled。getenforce一下顯示enforcing。這個時候再setenforce o,再去getenforce一下,發現已經為permissive

permissive表示,雖然selinux已經開啟了。但是僅僅是,遇到需要發生阻斷的時候,他不需要真正的去阻斷,僅僅是有一個提醒。我們不會從螢幕上看到,但是有個地方會去記錄

~3. centos7之前使用netfilter防火牆

都是可以用iptables來配置埠的

~4. centos7開始使用firewalld防火牆

都是可以用iptables來配置埠的

我們可以在7上將firewalld關閉,將netfiter開啟。是可以在7上使用netfilter的。方法如下:

先執行~6.(把firewalld停掉,不讓他開機啟動) > 在執行~5.(把這個服務關掉) > 然後再 執行~7. 把netfilter開啟(實際上就是安裝一下) > 再執行~8. (產生這個服務) > 最後執行~9. (開始這個服務)

iptables -nvL可以檢視他的規則

iptables是netfilter的工具,而名字是叫netfilter

~5. systemctl stop firewalld

~6. systemctl disable firewallded

~7. yum install -y iptables-services

~8. systemctl enable iptables

~9. systemctl start iptables

 

 

例項:

2.

[root@axinlinux-01 network-scripts]# vi /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

 

[root@axinlinux-01 network-scripts]# getenforce

Enforcing

[root@axinlinux-01 network-scripts]# setenforce 0

[root@axinlinux-01 network-scripts]# getenforce

Permissive

 

4.(將firewalld關閉,開啟netfilter)

[root@axinlinux-01 network-scripts]# systemctl disable firewallded

Failed to execute operation: No such file or directory

[root@axinlinux-01 network-scripts]# systemctl stop firewalld

[root@axinlinux-01 network-scripts]# yum install -y iptables-services

[root@axinlinux-01 network-scripts]# systemctl enable iptables

Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.

[root@axinlinux-01 network-scripts]# systemctl start iptables

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 

 

10.13 netfilter5表5鏈介紹:

 

linux防火牆—netfilter

 

~1. netfilter的5個表

~2. filter表用於過濾包,最常用的表,有INPUT、PORWARD、OUTPUT三個鏈

~3. nat表用於網路地址轉換,有PREROUTING、OUTPUT、POSTROUTING三個鏈

~4. managle表用於給資料包表標記,幾乎用不到

~5. raw表可以實現不追蹤某些資料包,阿明從來不用

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

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

 

 

 

linux防火牆-netfilter

~1. 資料包流向與netfilter的5個鏈

~2. PREROUTING:資料包進入路由表之前

~3. INPUT:通過路由表後目的地為本機

~4. FORWARD:通過路由表後,目的地不為本機

~5. OUTPUT:由本機產生,向外發出

~6. POSTROUTING:傳送到網絡卡介面之前

 

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 

 

10.14 iptables語法:

 

linux防火牆—netfilter

 

iptables相關的命令與用法:

~1. 檢視iptables規則:iptables -nvL

service iptables restart 重啟iptables

/etc/sysconfig/iptables 規則儲存位置

詳見例項1.

~2. iptables -F 清空規則

感覺規則不合適的時候可全部清除。但需注意即是清除了規則,他的檔案裡(/etc/sysconfig/iptables)還是有的

~3. service iptables save 儲存規則

把當前的規則儲存到檔案裡面去

但是我們剛剛已經刪除了,清空規則之後,我們在儲存規則儲存的僅僅是空規則。我們可以重啟他的服務,讓他重新載入回來(service iptables restart),

我們寫完的規則僅僅只是在記憶體中生效的,想讓他重啟後依然生效,就要service iptables save

也就是說,重啟伺服器或者是重啟iptables,他都會重新載入配置檔案裡的規則

 

~4. iptables -t nat -t 指定表(不加-t就是預設filter表)

以上1 2 3 針對的都是filter表

寫法為 iptables -t nat -nvL

當然net表裡面沒有規則

~5. iptables -Z 可以把計數器清零

iptables -nvL後,第二行是有數字的。pkts表示的是有多少包,byts表示的是資料量,資料大小(位元組)

-Z之後資料清零,再次檢視時,又會產生一些資料,是因為每時每刻都是在產生的

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

增加DROP的規則,來源IP是某某某IP的,訪問我們某一個埠的。或者來源埠是什麼的。我們給他DROP(扔掉

)掉

沒有加-t,就是預設的filter表

-A 增加一條規則。後面的INPUT就是增加的鏈是INPUT鏈。

-s指定一個來源IP

-p指定協議。是tcp還是udp或者icmp

sport 1234 來源的埠是1234

-d目標的IP

dport 80 目標的埠是80

最後操作 -j DROP ,DROP就是把資料直接給扔掉。還有一個 REJECT拒絕(來時會告訴他被拒絕了,比較有禮貌。DORP是看都不看直接扔掉)DORP用的比較多。扔掉和拒絕的效果都是一樣的,都是讓這個資料過不來。相當於把這個IP給封掉了

~7. iptables -I/-A/-D INPUT -s 1.1.1.1 -j DROP

iptables -I INPUT -p tcp --dport 80 -j DORP (在用-dport或-sport的時候,前面要指定哪個協議)

根據~6. -A為增加一條規則,即排在後面。-I為插入一條規則,即為排在最前面(置頂)。排在最前面,我們在匹配的時候回優先匹配最前面的規則,然後才會一條條的往下執行。如果你的這個規則,這個資料包已經匹配了第一條規則,比如我們訪問80埠的並且滿足下面的一條規則,這兩條規則同時滿足。那麼同時滿足他會匹配第一條,一旦匹配了第一條這個數包就DORP掉了,就不會往下走了。一旦匹配直接執行

-D 刪除一條規則,比如iptables -D INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP(刪除~6.那條規則)

~8. iptables -IINPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT

指定(-i)網絡卡為etho的

~9. iptables -nvL --line-numbers

假如,我們很久之前寫了一條很長的規則,就像~6.。我們想把它刪了,但是又想不起來這條規則是怎麼寫的。我們可以執行這條命令,在前面加上序號,用序號把他刪掉。寫法為 iptables -D INPUT 7

~10. iptables -D INPUT 1

利用序號,把規則刪掉

~11. iptables -P INPUT DROP

-P指定鏈的預設的規則。像OUTPUT沒有任何的規則。那麼他就有預設的策略(ACCEPT)來決定。預設的策略就是ACCEPT。所有的資料包,只要是沒有具體的規則來匹配,那麼就走預設的策略。那麼這個預設的策略也可以改,也可以改成DORP。寫法為:iptables -P OUTPUT DROP。但是不要這麼改預設的規則。一旦這麼改了,就會連不上伺服器,因為DORP掉了,資料回傳不過來,xshell只能掉了。就只能回到伺服器在改回來 iptables -P OUTPUT ACCEPT

預設的規則不要改,保持預設就可以了

 

例項:

1.

[root@axinlinux-01 ~]# iptables -nvL 紅色部分是他的規則

Chain INPUT (policy ACCEPT 0 packets, 0 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 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

--

-

 

2.

[root@axinlinux-01 ~]# iptables -F 清除規則

[root@axinlinux-01 ~]# iptables -nvL 會發現就沒有規則了

Chain INPUT (policy ACCEPT 23 packets, 1924 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 15 packets, 1608 bytes)

pkts bytes target prot opt in out source destination

 

3.

[root@axinlinux-01 ~]# service iptables restart 重啟iptables 在檢視規則

Redirecting to /bin/systemctl restart iptables.service 發現已載入回來

[root@axinlinux-01 ~]# iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

22 1872 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

 

4.

[root@axinlinux-01 ~]# iptables -t nat -nvL 空規則

Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

 

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

 

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

 

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

 

6.

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

[root@axinlinux-01 ~]# iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

197 16288 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

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

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

以上-選項相對應的。 即為新增加的DORP規則

 

 

擴充套件:

圖為 埠對映: