Linux iptables使用例項

2022-05-01 17:00:08 字數 1844 閱讀 1018

1. 使區域網使用者可共享外網(撥號上網)

echo

1 > /proc/sys/net/ipv4/ip_forward

iptables -t nat -a postrouting -o ppp0 -j masquerade

iptables -t nat -a postrouting -s 192.168.1.0/24 -o eth0 -j snat --to 1.2.3.4

iptables -t nat -a prerouting -s 192.168.1.0/24 -i eth1 -j dnat --to 1.2.3.4

4. 使外網使用者可以訪問到區域網192.168.138.21這台http服務

echo

1 > /proc/sys/net/ipv4/ip_forward

iptables -t nat -a prerouting -p tcp -m tcp --dport 80 -j dnat --to-destination 192.168.138.21

iptables -t nat -a postrouting -o ppp0 -j masquerade

5. 使區域網使用者,訪問外網web服務時,自動使用squid作web透明**伺服器

echo

1 > /proc/sys/net/ipv4/ip_forward

iptables -t nat -a prerouting -s 192.168.138.0/24 -p tcp --dport 80 -i eth0 -j dnat --to 192.168.138.1

iptables -t nat -a prerouting -s 192.168.138.0/24 -p tcp --dport 80 -i eth0 -j redirect --to 3128

iptables -t nat -a postrouting -o ppp0 -j masquerade

6. iptables安全策略,閘道器伺服器系統自生安全策略,只對內網使用者開放22埠(sshd服務)

#清空 filter table

[root@localhost]# iptables -f -t filter

[root@localhost]# iptables -x -t filter

[root@localhost]# iptables -z -t filter

#清空 nat table

[root@localhost]# iptables -f -t nat

[root@localhost]# iptables -x -t nat

[root@localhost]# iptables -z -t nat

#設定預設策略(input鏈預設為drop)

[root@localhost]# iptables -t filter -p input drop

[root@localhost]# iptables -t filter -p output accept

[root@localhost]# iptables -t filter -p forward accept

#回環介面(lo),預設accept

[root@localhost]# iptables -a input -p all -i lo -j accept

#只對內網使用者開放sshd服務

[root@localhost]# iptables -a input -p tcp -s 192.168.138.0/24 --dport 22 -j accept

[1] 

[2] 

linux iptables 防火牆使用

centos 7.0 更改了預設的防火牆 使用了 firewalld 程序做為防火牆 開啟關閉方法 分別是 service firewalld start service firewlld stop 在7.0之前,預設的防火牆軟體是 iptables 如果不習慣的話,完成可以把 firewalld ...

Linux Iptables命令詳解

ipaddr ipaddr port port 可以指定乙個單一的新的ip位址,乙個ip位址範圍,也可以附加乙個埠範圍 只能在指定 p tcp 或者 p udp的規則裡 如果未指定埠範圍,源埠中512以下的 埠 會被安置為其他的512以下的埠 512到1024之間的埠會被安置為1024以下的,其他埠...

Linux iptables規則詳解

filter input drop 345 43237 forward accept 0 0 output accept 306 41346 ainput p tcp m tcp dport 10022 j accept ainput p tcp m tcp dport 80 j accept ai...