# 放行80/21/22端口(其中22端口只放行特定網段) [root@localhost ~]# vim /usr/local/src/iptables.sh #!/bin/bash ipt="/usr/sbin/iptables" # 狀況iptables表 $ipt -F # 修改默認策略 $ipt -P INPUT DROP $ipt -P OUTPUT ACCEPT $ipt -P FOREWARD ACCEPT # 指定相關狀態的數據包放行,必須寫!! $ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT # 容許ssh遠程鏈接的網段數據包通訊 $ipt -A INPUT -s192.168.65.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
原理是經過設置防火牆將發給本機的icmp包(ping請求,type爲8)丟棄,致使外機沒法收到ping應答包;本機發給外部主機的icmp包則不作處理,能夠照常通訊。shell
# 測試時,排除其餘規則干擾,先清空規則表 [root@localhost ~]# iptables -F [root@localhost ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP [root@localhost ~]# iptables -nvL Chain INPUT (policy ACCEPT 47 packets, 3312 bytes) pkts bytes target prot opt in out source destination 0 0 DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 28 packets, 2736 bytes) pkts bytes target prot opt in out source destination # ping外網可通 [root@localhost ~]# ping www.baidu.com PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data. 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=128 time=37.4 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=128 time=38.0 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=128 time=37.9 ms ^C --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 37.487/37.858/38.094/0.265 ms # ping本機,沒法ping通 C:\Users\18367>ping 192.168.65.133 正在 Ping 192.168.65.133 具備 32 字節的數據: 請求超時。 請求超時。 請求超時。 來自 192.168.65.133 的回覆: 字節=32 時間=1ms TTL=64 192.168.65.133 的 Ping 統計信息: 數據包: 已發送 = 4,已接收 = 1,丟失 = 3 (75% 丟失), 往返行程的估計時間(以毫秒爲單位): 最短 = 1ms,最長 = 1ms,平均 = 1ms
刪除規則後從新測試(實驗效果的緣由直接使用-F清空,真實狀況下最好很差這樣作)vim
[root@localhost ~]# iptables -F [root@localhost ~]# iptables -nvL Chain INPUT (policy ACCEPT 42 packets, 2976 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 23 packets, 2180 bytes) pkts bytes target prot opt in out source destination # ping本機,ping通 C:\Users\18367>ping 192.168.65.133 正在 Ping 192.168.65.133 具備 32 字節的數據: 來自 192.168.65.133 的回覆: 字節=32 時間<1ms TTL=64 來自 192.168.65.133 的回覆: 字節=32 時間<1ms TTL=64 來自 192.168.65.133 的回覆: 字節=32 時間<1ms TTL=64 來自 192.168.65.133 的回覆: 字節=32 時間<1ms TTL=64 192.168.65.133 的 Ping 統計信息: 數據包: 已發送 = 4,已接收 = 4,丟失 = 0 (0% 丟失), 往返行程的估計時間(以毫秒爲單位): 最短 = 0ms,最長 = 0ms,平均 = 0ms
主機1:centos7,新增網卡ens37,原網卡能夠鏈接外網,新增網卡鏈接內網(能夠鏈接centos7-1),ip:192.168.0.100/24 主機2:centos7-1,新增一塊網卡ens37,禁止原網卡啓動(只能鏈接內網),網卡ip:192.168.0.200/24,沒法訪問外網 主機1與主機2之間能夠通訊
網卡設置(測試環境VMware下):centos
點擊編輯虛擬設置選項 --> 添加(A)... --> 選擇網絡適配器 --> 下一步 --> 完成bash
點擊LAN區段(S)...
--> 添加,命名後肯定 --> 在LAN區段下拉菜單中選中便可網絡
在一個虛擬機中設置了LAN,在其餘虛擬機中就能夠直接選擇ssh
臨時設置ip:ipconfig ens37 192.168.0.100/24tcp
永久修改ip:vim /etc/sysconfig/network-scripts/ifcfg-ens37; 配置ip爲192.168.0.100,網關爲192.168.0.255 (無ifcfg-ens37文件,能夠拷貝ens33文件)oop
對於主機2來講,因爲原網卡被禁,須要直接登入,一樣能夠使用上述2種方法設置ip測試
驗證環境是否搭建成功centos7
主機1
# 網卡正常運行 [root@localhost ~]# mii-tool ens37 ens37: negotiated 1000baseT-FD flow-control, link ok # ip配置成功 [root@localhost ~]# ifconfig ens37 ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.0.100 netmask 255.255.255.0 broadcast 192.168.0.255 inet6 fe80::20c:29ff:fe8b:d486 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:8b:d4:86 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 62 bytes 10104 (9.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 # ping 主機2可通 [root@localhost ~]# ping 192.168.0.200 PING 192.168.0.200 (192.168.0.200) 56(84) bytes of data. 64 bytes from 192.168.0.200: icmp_seq=1 ttl=64 time=0.267 ms 64 bytes from 192.168.0.200: icmp_seq=2 ttl=64 time=1.16 ms 64 bytes from 192.168.0.200: icmp_seq=3 ttl=64 time=0.604 ms ^C --- 192.168.0.100 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2005ms rtt min/avg/max/mdev = 0.267/0.679/1.166/0.370 ms
主機2
配置好ip(爲了排除ens33的干擾,將其down掉),ping測試連通性,主機1,本機均可通
主機1開啓路由轉發
# 0表示未開啓 [root@localhost ~]# cat /proc/sys/net/ipv4/ip_forward 0 [root@localhost ~]# echo "1" > /proc/sys/net/ipv4/ip_forward
主機1上添加nat規則
# MASQUERADW:IP假裝成爲封包出去(-o)的那塊裝置上的IP [root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o ens33 -j MASQUERADE # 查看nat表是否添加了該規則 [root@localhost ~]# 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 0 0 MASQUERADE all -- * ens33 192.168.0.0/24 0.0.0.0/0
主機2設置網關192.168.0.100(主機1新增網卡)
route add default gw 192.168.0.100
設置主機2的DNS,使之能夠鏈接外網
[root@localhost ~]# vim /etc/reslov.conf nameserver 119.29.29.29 :wq保存退出
驗證DNS及外網鏈接
# ping DNS [root@localhost ~]# ping 119.29.29.29 # ping 主機1 [root@localhost ~]# ping 192.168.65.134 # ping 外網 [root@localhost ~]# ping baidu.com
按上述操做設置完畢後,驗證時沒法ping通DNS,錯誤爲Destination Host Prohibited
;查看iptables時發現INPUT和FORWARD鏈內存在reject規則,使用iptables -D
刪除該規則後,主機2可ping通!
打開端口轉發
[root@localhost ~]# echo "1" > /proc/sys/net/ipv4/ip_forward
主機1添加端口轉規則
進來的數據包規則 [root@localhost ~]# iptables -t nat -A PREROUTING -d 192.168.65.134 -p tcp --dport 1122 -j DNAT --to 192.168.0.200:22 出去的數據包規則 [root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.0.200 -j SNAT --to 192.168.65.134 # 查看規則表 [root@localhost ~]# iptables -t nat -nvL Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 DNAT tcp -- * * 0.0.0.0/0 192.168.65.134 tcp dpt:1122 to:192.168.0.200:22 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 27 2020 MASQUERADE all -- * ens33 192.168.0.0/24 0.0.0.0/0 0 0 SNAT all -- * * 192.168.0.200 0.0.0.0/0 to:192.168.65.134
主機2設置網關192.168.0.100(主機1新增網卡)
# route add default gw 192.168.0.100
驗證是否能夠遠程鏈接 新建遠程鏈接,輸入ip:192.168.65.134,端口爲1122,點擊肯定;輸入用戶名、密碼,成功登陸
Connecting to 192.168.65.134:1122... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last login: Thu Nov 30 21:03:36 2017 from 192.168.65.1 [root@localhost ~]# # ifconfig,ens37的ip爲主機2新增網卡ip! [root@localhost ~]# ifconfig ens33: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 ether 00:0c:29:d0:81:f5 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.0.200 netmask 255.255.255.0 broadcast 192.168.0.255 inet6 fe80::20c:29ff:fed0:81ff prefixlen 64 scopeid 0x20<link> ether 00:0c:29:d0:81:ff txqueuelen 1000 (Ethernet) RX packets 113 bytes 11726 (11.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 161 bytes 21946 (21.4 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 496 bytes 45156 (44.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 496 bytes 45156 (44.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
在上述操做完成以後,沒法遠程鏈接;檢測主機2的網關,發現ens33重連了,執行ifdown,關閉ens33網卡,遠程鏈接成功!!