1. 程式人生 > 實用技巧 >iptables的簡單用法和iptables實現外網連線內網

iptables的簡單用法和iptables實現外網連線內網

iptables的幾種簡單用法

1、拒絕所有主機ping當前的主機。

拒絕之前,先把自己允許了,
[ root@centos8-1 ~# iptables -AINPUT -s 10.0.0.1 -j REJECT
這就拒絕了所有主機ping我
[ root@centos8-1 ~# iptables -AINPUT -j  REJECT
檢視定義的規則,就看到剛定義的規則了,注意順序不能改變
[ root@centos8-1 ~# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt 
in out source destination 133 11022 ACCEPT all -- * * 10.0.0.1 0.0.0.0/0 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable 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、本機能夠訪問別的機器的HTTP服務,但是別的機器無法訪問本機。

拒絕了所有網段的主機訪問我的tcp80埠
[ root@centos8-1 /var/www/html# iptables -AINPUT  -p tcp --dport 80
-j REJECT 檢視在防火牆上定義的規則,第二條是拒絕了所有主機訪問我的http服務(http服務就是tcp 80埠) [ root@centos8-1 /var/www/html# iptables -vnL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 789 58572 ACCEPT all -- * * 10.0.0.1 0.0.0.0/0 0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 reject-with icmp-port-unreachable 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

3、當我們發現有 ip 惡意攻擊我們得時候,我們可以通過對防火牆設定規則來進行控制。所以我們可以新增connlimit模組來實現對最大併發得控制。

10.0.0.18為本機,當任何主機通過INPUT併發連線我,連線的數量大於2,就拒絕
[ root@centos8-1 /var/www/html# iptables -AINPUT -d 10.0.0.18 -m connlimit --connlimit-above 2 -j REJECT

用iptables實現外網通過ssh連線內網

4、實踐題

實驗前提需求

現在我在外地出差使用A7網際網路主機,但是現在由於公司有業務需要我 ssh 連結到內網、這時候

我就聯絡我們公司同事在防火牆上配置相關規則讓我連結進公司內網
A7:把網絡卡改為僅主機,ip地址改為192.168.1.128  閘道器指向192.168.1.129
A8:需要兩塊網絡卡一塊僅主機ip設定為 192.168.1.129 ;另一塊是nat,ip設定為10.0.0.8
B8:把網絡卡設定為nat ip為10.0.0.18
====================================================
A8:echo 1 > /proc/sys/net/ipv4/ip_forward
然後依次執行這三條命令
iptables -AFORWARD -j REJECT iptables -IFORWARD -s 192.168.1.128 -p tcp --dport 22 -j ACCEPT iptables -IFORWARD -d 192.168.1.128 -p tcp --sport 22 -j ACCEPT