運維平常

1、firewalld和netfilter

一、關閉防火牆

[root@ma-1 ~]# 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  (更改配置爲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@ma-1 ~]# getenforce
Enforcing
[root@ma-1 ~]# setenforce 0  (臨時關閉防火牆)
[root@ma-1 ~]# getenforce
Permissive

二、iptables工具

netfilter服務是CentOS7以前版本中使用的防火牆,firewalld服務是CentOS7版本以後的使用的防火牆,可是二者的iptable工具用法相同html

[root@ma-1 ~]# systemctl disable firewalld  (關閉firewalld服務,有兩個步驟,分別是:disable表示開機不啓動和stop中止)
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@ma-1 ~]# systemctl stop firewalld
[root@ma-1 ~]# yum install -y iptables-services  (要使用iptables工具,須要安裝iptables-services軟件包)
已加載插件:fastestmirror
base
.......(中間省略)

做爲依賴被升級:
  iptables.x86_64 0:1.4.21-24.1.el7_5   

完畢!
[root@ma-1 ~]# systemctl enable iptables (此安裝包會產生iptable服務)
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@ma-1 ~]# systemctl start iptables  (開啓iptable服務)
[root@ma-1 ~]# iptables -nvL  (此命令能夠查看iptable的默認規則)
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target prot opt in out source   destination 
   31  2144 ACCEPT all  --  *  *   0.0.0.0/00.0.0.0/0state RELATED,ESTABLISHED
0 0 ACCEPT icmp --  *  *   0.0.0.0/00.0.0.0/0   
0 0 ACCEPT all  --  lo *   0.0.0.0/00.0.0.0/0   
0 0 ACCEPT tcp  --  *  *   0.0.0.0/00.0.0.0/0state NEW tcp dpt:22
0 0 REJECT all  --  *  *   0.0.0.0/00.0.0.0/0reject-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/00.0.0.0/0reject-with icmp-host-prohibited

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

三、iptable的表和連接

iptables只是Linux防火牆的管理工具而已。真正實現防火牆功能的是netfilter(CentOS7以後是firewalld),它是Linux內核中實現包過濾的內部結構linux

1)iptable的五表五鏈

五表
filter表:     過濾數據包
Nat表:        用於網絡地址轉換(IP、端口)
Mangle表:     修改數據包的服務類型、TTL、而且能夠配置路由實現QOS
Raw表:        決定數據包是否被狀態跟蹤機制處理
Security表:   用於強制訪問控制(MAC)的網絡規則
五鏈
INPUT鏈:      進來的數據包應用此規則鏈中的策略
OUTPUT鏈:     外出的數據包應用此規則鏈中的策略
FORWARD鏈:    轉發數據包時應用此規則鏈中的策略
PREROUTING鏈: 對數據包做路由選擇前應用此鏈中的規則(全部的數據包進來的時侯都先由這個鏈處理)
POSTROUTING鏈:對數據包做路由選擇後應用此鏈中的規則(全部的數據包出來的時侯都先由這個鏈處理)

2)iptable基本用法

一.基本格式
1. iptable [-t 表] 命令選項 [連名]  匹配條件  [-j 動做]
2.經常使用命令選項以下:
-A (append) 在指定的連的結尾添加規則
-D (delete)刪除指定連中的規則,能夠按規則號或規則內容匹配
-I (insert)插入一條新規則,默認是在最前面
-R (replace) 替換某一條規則
-L (list)列出全部規則
-F (flush)清空全部規則
-N (new)自定義一條規則連
-X (--delete-chain) 刪除用戶自定義規則連
-P (policy)設置默認策略
-n (numeric)以數字方式顯示,如:顯示ip,但不顯示主機名
-v (verbose)顯示詳細信息
-V (version)查看iptable的版本信息
-Z  清空計數器值
--line-number 查看規則連是,顯示列表號 


二.舉例
# iptable -t filter -F(清空filter表中全部規則)
# iptable -t filter -Z(清空filter表中的計數器值)
# iptable -t filter -X (清除filter表中自定義連)
# iptable -t filter -P INPUT DROP (設置INPUT連默認策略爲DROP)
# iptable -t filter -P OUTPUT DROP
# iptable -t filter -P FORWROD DROP
# iptable -t filter -A INPUT -p tcp -j ACCEPT (在INPUT連最後添加一條容許tcp協議的數據包進入的規則)
# iptable -t filter -R INPUT 1 -p tcp -j DROP (替換INPUT連的第1條規則爲拒絕tcp數據包進入)
# iptable -t nat -vnL --line-number (以詳細的、數字的格式列出nat表中的全部規則)
# iptable -t nat -D POSTROUTING 1 (刪除nat表POSTROUTING 連中的第1條規則)


三.條件匹配
1. 協議匹配:用於檢查數據包使用的協議,符合規則就容許,反之拒絕。容許使用的協議名在/etc/protocols文件中。經常使用的協議有tcp,udp,icmp,ip 和all。( -p 協議名 )

# iptable -I INPUT -p icmp -j REJECT (拒絕進入防火牆自己的icmp數據包)
# iptable -A FORWARD -p udp -j ACCEPT (容許轉發udp的全部數據包)

2. 地址匹配:用於檢查數據包的地址是否符合規則,包括源地址和目的地址。(-s 源地址, -d 目的地址)
# iptable -A FORWARD -s 10.0.0.0/8  -j DROP (拒絕轉發來自10.0.0.0/8 網段的數據包)
# iptable -A FORWARD -d 80.0.0.0/8  -j DROP ( 拒絕轉發目的是80.0.0.0/8 網段的數據包)

3.端口匹配:用於檢查數據包的tcp或udp端口,須要和 「-p 協議類型」 一塊兒使用(-sport 源端口,-dport 目的端口)

# iptables -A FORWARD -s 10.0.0.0/8 -p tcp --dport 80  -j ACCEPT (容許轉發來自10.0.0.0/8網段,目的端口是80的數據包)
# iptables -I FORWARD -s 10.0.0.0/8 -p tcp --sport 21   -j ACCEPT(容許轉發來自10.0.0.0/8網段,源端口是21的數據包)

4.接口匹配:用於檢查數據包從防火牆那個接口進入或出去,來判斷是否容許。

# iptables -A FORWARD -i eth0 -s 10.0.0.0/8 -p tcp --dport 80 -j ACCEPT(容許轉發從eth0進入,來自10.0.0.0/8網段,使用tcp 協議,目的端口椒80的數據包)
# iptables -A INPUT -i eth0 -s 80.0.0.0/8 -j DORP (拒絕從eth0進入,來自80.0.0.0/8的數據包)

5.SNAT轉換:通常linux充當網關服務器時使用,SNAT只能用在nat表的POSTROUTING連,用於對源地址進行轉換。要結合 --to 使用。

# iptables -t nat -A POSTROUTING -s 10.0.0.0/8 -j SNAT --to 202.106.1.1(未來自10.0.0.0/8網段的全部數據包的源地址轉爲202.106.1.1)
# iptables -t nat -A POSTROUTING -i eth0 -s 80.0.0.0/8 -p tcp --dport 25 -j SNAT --to 202.106.1.1

6.DNAT轉換:只能用在nat表中的PREROUTING連,用於對目的地址或端口進行轉換。

# iptables -t nat -A PREROUTING -i eth1 -d 202.106.1.1 -p tcp --dport 80 -j DNAT --to 10.0.0.10(將從eth1 進入,目的地址是202.106.1.1,使用tcp 協議,目的端口是80的數據包的目的地址轉爲10.0.0.1)

7.MASQUERADE:假裝,是SNAT的特例。

# iptables -t nat -A POSTROUTING -s 10.0.0.0/8 -o eth1 -j MASQUERADE(未來自10.0.0.0/8網段,從eth1出去的數據包的源地址假裝爲eth1接口地址)

3)數據包流向判斷

若是是本機:   PREROUTING——INPUT——OUTPUT——POSTROUTING  
不是本機:PREROUTING——FORWARD——POSTROUTING

https://blog.csdn.net/achejq/article/details/53067170

4)具體操做

[root@ma-1 ~]# iptables -nvL  (查看iptable默認規則)
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target prot opt in out source   destination 
  380 29628 ACCEPT all  --  *  *   0.0.0.0/00.0.0.0/0state RELATED,ESTABLISHED
0 0 ACCEPT icmp --  *  *   0.0.0.0/00.0.0.0/0   
0 0 ACCEPT all  --  lo *   0.0.0.0/00.0.0.0/0   
0 0 ACCEPT tcp  --  *  *   0.0.0.0/00.0.0.0/0state NEW tcp dpt:22
   63  6726 REJECT all  --  *  *   0.0.0.0/00.0.0.0/0reject-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/00.0.0.0/0reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 358 packets, 26368 bytes)
 pkts bytes target prot opt in out source   destination 
[root@ma-1 ~]# iptables -F  (清空規則命令)
[root@ma-1 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 7 packets, 488 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 5 packets, 492 bytes)
 pkts bytes target prot opt in out source   destination 
[root@ma-1 ~]# service iptables save  (將當前的規則保存到配置文件中)
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  肯定  ]
[root@ma-1 ~]# cat /etc/sysconfig/iptables  (保存規則的配置文件)
# Generated by iptables-save v1.4.21 on Sat Jun  9 14:30:38 2018
*filter
:INPUT ACCEPT [63:4324]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [40:4104]
COMMIT
# Completed on Sat Jun  9 14:30:38 2018
[root@ma-1 ~]# service iptables restart  (重啓iptables)
Redirecting to /bin/systemctl restart iptables.service
[root@ma-1 ~]# iptables -nvL  (已經沒有規則)
Chain INPUT (policy ACCEPT 8 packets, 600 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 8 packets, 744 bytes)
 pkts bytes target prot opt in out source   destination


[root@ma-1 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP  (不加-t,則默認是filter表,添加規則,各選項和參數意義上文已經說起)
[root@ma-1 ~]# iptables -nvL  (查看已經添加的規則)
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target prot opt in out source   destination 
   31  2160 ACCEPT all  --  *  *   0.0.0.0/00.0.0.0/0state 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/0state NEW tcp dpt:22
0 0 REJECT all  --  *  *   0.0.0.0/0 0.0.0.0/0reject-with icmp-host-prohibited
0 0 DROP   tcp  --  *  *   192.168.188.1 192.168.188.128  tcp spt:1234 dpt:80

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/00.0.0.0/0reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 8 packets, 1120 bytes)
 pkts bytes target prot opt in out source   destination 
[root@ma-1 ~]# iptables -I INPUT -p tcp --dport 80 -j DROP   (還有-I,是一種插入的方式,會在規則第一行顯示)
[root@ma-1 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target prot opt in out source   destination 
0 0 DROP   tcp  --  *  *   0.0.0.0/00.0.0.0/0tcp dpt:80
  141  9768 ACCEPT all  --  *  *   0.0.0.0/00.0.0.0/0state RELATED,ESTABLISHED
0 0 ACCEPT icmp --  *  *   0.0.0.0/00.0.0.0/0   
0 0 ACCEPT all  --  lo *   0.0.0.0/00.0.0.0/0   
0 0 ACCEPT tcp  --  *  *   0.0.0.0/00.0.0.0/0state NEW tcp dpt:22
0 0 REJECT all  --  *  *   0.0.0.0/00.0.0.0/0reject-with icmp-host-prohibited
0 0 DROP   tcp  --  *  *   192.168.188.1192.168.188.128  tcp spt:1234 dpt:80

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/00.0.0.0/0reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 5 packets, 588 bytes)
 pkts bytes target prot opt in out source   destination   (-A會添加在最後,-I會添加在最前邊,添加的規則會優先過濾前邊的規則)
[root@ma-1 ~]# iptables -D INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP (-D爲刪除規則)
[root@ma-1 ~]# iptables -D INPUT -p tcp --dport 80 -j DROP
[root@ma-1 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target prot opt in out source   destination 
  368 29076 ACCEPT all  --  *  *   0.0.0.0/00.0.0.0/0state RELATED,ESTABLISHED
0 0 ACCEPT icmp --  *  *   0.0.0.0/00.0.0.0/0   
0 0 ACCEPT all  --  lo *   0.0.0.0/00.0.0.0/0   
0 0 ACCEPT tcp  --  *  *   0.0.0.0/00.0.0.0/0state NEW tcp dpt:22
0 0 REJECT all  --  *  *   0.0.0.0/00.0.0.0/0reject-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/00.0.0.0/0reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 5 packets, 732 bytes)
 pkts bytes target prot opt in out source   destination 
[root@ma-1 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
[root@ma-1 ~]# iptables -nvL --line-number  (會顯示規則的順序號)
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target prot opt in out source   destination 
1  492 38008 ACCEPT all  --  *  *   0.0.0.0/00.0.0.0/0state RELATED,ESTABLISHED
2  0 0 ACCEPT icmp --  *  *   0.0.0.0/00.0.0.0/0   
3  0 0 ACCEPT all  --  lo *   0.0.0.0/00.0.0.0/0   
4  0 0 ACCEPT tcp  --  *  *   0.0.0.0/00.0.0.0/0state NEW tcp dpt:22
5  0 0 REJECT all  --  *  *   0.0.0.0/00.0.0.0/0reject-with icmp-host-prohibited
6  0 0 DROP   tcp  --  *  *   192.168.188.1192.168.188.128  tcp spt:1234 dpt:80

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target prot opt in out source   destination 
10 0 REJECT all  --  *  *   0.0.0.0/00.0.0.0/0reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 73 packets, 6908 bytes)
num   pkts bytes target prot opt in out source   destination 
[root@ma-1 ~]# iptables -D INPUT 6  (能夠按照順序號刪除規則)
[root@ma-1 ~]# iptables -nvL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target prot opt in out source   destination 
1  628 47972 ACCEPT all  --  *  *   0.0.0.0/00.0.0.0/0state RELATED,ESTABLISHED
2  0 0 ACCEPT icmp --  *  *   0.0.0.0/00.0.0.0/0   
3  0 0 ACCEPT all  --  lo *   0.0.0.0/00.0.0.0/0   
4  0 0 ACCEPT tcp  --  *  *   0.0.0.0/00.0.0.0/0state NEW tcp dpt:22
5  1   229 REJECT all  --  *  *   0.0.0.0/00.0.0.0/0reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target prot opt in out source   destination 
10 0 REJECT all  --  *  *   0.0.0.0/00.0.0.0/0reject-with icmp-host-prohibited

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

[root@ma-1 ~]# iptables -P INPUT DROP  (丟棄默認的規則,會斷開會致使數據包沒法傳送回來,只能回服務器加載默認規則)

5)iptable小案例

[root@ma-1 ~]# vim /usr/local/sbin/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  (指定狀態,加此條規則是保證通訊更加通暢;RELATED表示編譯狀態,ESTABLISHED保持連接)
$ipt -A INPUT -s 192.168.133.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@ma-1 ~]# sh !$
sh /usr/local/sbin/iptables.sh
[root@ma-1 ~]# iptables -nvL --line-number  (查看添加以後的規則)
Chain INPUT (policy DROP 0 packets, 0 bytes)
num   pkts bytes target prot opt in out source   destination 
19   672 ACCEPT all  --  *  *   0.0.0.0/00.0.0.0/0state RELATED,ESTABLISHED
20 0 ACCEPT tcp  --  *  *   192.168.133.0/24 0.0.0.0/0tcp dpt:22
30 0 ACCEPT tcp  --  *  *   0.0.0.0/00.0.0.0/0tcp dpt:80
40 0 ACCEPT tcp  --  *  *   0.0.0.0/00.0.0.0/0tcp dpt:21

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

Chain OUTPUT (policy ACCEPT 7 packets, 916 bytes)
num   pkts bytes target prot opt in out source   destination 
[root@ma-1 ~]# service iptables restart  (重啓以後規則再也不有效,是由於沒有保存到配置文件)
Redirecting to /bin/systemctl restart iptables.service
[root@ma-1 ~]# iptables -nvL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target prot opt in out source   destination 
15   356 ACCEPT all  --  *  *   0.0.0.0/00.0.0.0/0state RELATED,ESTABLISHED
20 0 ACCEPT icmp --  *  *   0.0.0.0/00.0.0.0/0   
30 0 ACCEPT all  --  lo *   0.0.0.0/00.0.0.0/0   
40 0 ACCEPT tcp  --  *  *   0.0.0.0/00.0.0.0/0state NEW tcp dpt:22
50 0 REJECT all  --  *  *   0.0.0.0/00.0.0.0/0reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target prot opt in out source   destination 
10 0 REJECT all  --  *  *   0.0.0.0/00.0.0.0/0reject-with icmp-host-prohibited

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

icmp示例web

[root@ma-1 ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP  (使用此命令可使別的機器ping不一樣本機,但本機卻能夠ping通外機器)
[root@ma-1 ~]# ping www.qq.com
PING www.qq.com (182.254.34.74) 56(84) bytes of data.
64 bytes from 182.254.34.74 (182.254.34.74): icmp_seq=1 ttl=128 time=24.9 ms
64 bytes from 182.254.34.74 (182.254.34.74): icmp_seq=2 ttl=128 time=31.5 ms
64 bytes from 182.254.34.74 (182.254.34.74): icmp_seq=3 ttl=128 time=24.8 ms
64 bytes from 182.254.34.74 (182.254.34.74): icmp_seq=4 ttl=128 time=24.5 ms
64 bytes from 182.254.34.74 (182.254.34.74): icmp_seq=5 ttl=128 time=24.6 ms
^C
--- www.qq.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 8226ms
rtt min/avg/max/mdev = 24.518/26.128/31.554/2.721 ms
C:\Users\ma>ping 192.168.1.131
正在 Ping 192.168.1.131 具備 32 字節的數據:
請求超時。
請求超時。

192.168.1.131 的 Ping 統計信息:
數據包: 已發送 = 2,已接收 = 0,丟失 = 2 (100% 丟失)
Control-C
^C
[root@ma-1 ~]# iptables -D INPUT -p icmp --icmp-type 8 -j DROP

C:\Users\ma>ping 192.168.1.131

正在 Ping 192.168.1.131 具備 32 字節的數據:
來自 192.168.1.131 的回覆: 字節=32 時間<1ms TTL=64
來自 192.168.1.131 的回覆: 字節=32 時間<1ms TTL=64

192.168.1.131 的 Ping 統計信息:
數據包: 已發送 = 2,已接收 = 2,丟失 = 0 (0% 丟失),
往返行程的估計時間(以毫秒爲單位):
最短 = 0ms,最長 = 0ms,平均 = 0ms
Control-C

2、iptables nat 表應用

A機器兩塊網卡ens33(192.168.1.131)、ens37(192.168.100.1),ens33能夠上外網,ens37僅僅是內部網絡,B機器只有ens37(192.168.100.100),和A機器ens37能夠通訊互聯。vim

需求1:可讓B機器鏈接外網

一、添加網卡,設置成添加LAN字段,命名一個新的字段

二、在機器內操做修改ip
[root@ma-1 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
inet 192.168.1.131  netmask 255.255.255.0  broadcast 192.168.1.255
inet6 fe80::6aac:3e4d:6b3:73ee  prefixlen 64  scopeid 0x20<link>
ether 00:0c:29:ec:44:82  txqueuelen 1000  (Ethernet)
RX packets 110  bytes 10783 (10.5 KiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 130  bytes 14454 (14.1 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.1.145  netmask 255.255.255.0  broadcast 192.168.1.255
ether 00:0c:29:ec:44:82  txqueuelen 1000  (Ethernet)

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
ether 00:0c:29:ec:44:8c  txqueuelen 1000  (Ethernet)
RX packets 34  bytes 11628 (11.3 KiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 55  bytes 10362 (10.1 KiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@ma-1 ~]# ifconfig ens37 192.168.100.1/24  (直接設置IP的命令,重啓後將再也不有用,須要修改配置文件)
[root@ma-1 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
inet 192.168.1.131  netmask 255.255.255.0  broadcast 192.168.1.255
inet6 fe80::6aac:3e4d:6b3:73ee  prefixlen 64  scopeid 0x20<link>
ether 00:0c:29:ec:44:82  txqueuelen 1000  (Ethernet)
RX packets 344  bytes 30654 (29.9 KiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 313  bytes 41938 (40.9 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.1.145  netmask 255.255.255.0  broadcast 192.168.1.255
ether 00:0c:29:ec:44:82  txqueuelen 1000  (Ethernet)

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
inet 192.168.100.1  netmask 255.255.255.0  broadcast 192.168.100.255
inet6 fe80::20c:29ff:feec:448c  prefixlen 64  scopeid 0x20<link>
ether 00:0c:29:ec:44:8c  txqueuelen 1000  (Ethernet)
RX packets 36  bytes 11748 (11.4 KiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 65  bytes 11170 (10.9 KiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

(一樣的方法將另外一臺機器ip設置爲192.168.100.100)
[root@ma-1 ~]# ping 192.168.100.100
PING 192.168.100.100 (192.168.100.100) 56(84) bytes of data.
64 bytes from 192.168.100.100: icmp_seq=1 ttl=64 time=1.03 ms
64 bytes from 192.168.100.100: icmp_seq=2 ttl=64 time=0.461 ms
^C
--- 192.168.100.100 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.461/0.747/1.034/0.287 ms

三、準備工做完成以後,修改配置,添加規則

[root@ma-1 ~]# cat /proc/sys/net/ipv4/ip_forward
0  (0狀態下表示未打開端口轉發)
[root@ma-1 ~]# echo "1">/proc/sys/net/ipv4/ip_forward
[root@ma-1 ~]# cat /proc/sys/net/ipv4/ip_forward
1
[root@ma-1 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE  (添加規則)
[root@ma-1 ~]# 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 3 packets, 228 bytes)
 pkts bytes target prot opt in out source   destination 

Chain POSTROUTING (policy ACCEPT 3 packets, 228 bytes)
 pkts bytes target prot opt in out source   destination 
0 0 MASQUERADE  all  --  *  ens33   192.168.100.0/24 0.0.0.0/0

四、在B機器上設置網關,爲192.168.100.1,就能夠進行通訊

[root@ma-1 ~]# route add default gw 192.168.100.1  
設置完成以後就能夠在機器上鍊接外網,可是在外網上是鏈接不到機器

需求2:C機器只能和A通訊,讓C機器能夠直接連通B機器的22端口

A機器打開路由轉發 echo "1">/ proc/sys/net/ipv4/ip_forward
A上執行          iptables -t nat -A PREROUTING -d 192.168.133.130 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22 (設置規則)
A上執行          iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.133.130(設置規則)
B上設置網關爲     192.168.100.1

知識點

1)iptables應用在一個網段

[root@ma-1 ~]#iptables -I INPUT -m iprange --src-range 61.4.176.0-61.4.191.255 -j DROP

2)iptables限制syn速率

http://www.aminglinux.com/bbs/thread-985-1-1.html

3)sant,dnat,masquerade

http://www.aminglinux.com/bbs/thread-7255-1-1.html

4)-m state --state 後跟狀態

相關文章
相關標籤/搜索