局域网Linux单网卡ADSL拨号共享上网
生 活 四月 8th, 2008家里每人都有电脑,开通一条ADSL线路上网,下班回家,大家都需要上网,
使用大家都需要上网,最开始是谁先回家就自己拨号,然后使用CCProxy来开
代理,其他人通过来代理上网,这个方案用来看看网页什么的还行,但是要用
到其它的协议,比如ftp、telnet、ping、远程桌面等时,就很不方便了,要么
是设置不方便,要么就是根本不支持。
于是就打算用netfilter/iptables防火墙设置NAT来共享上网,开始还打算
用两个网卡,后来想了想,没必要,毕竟是家庭网络,对安全性的要求不高,
就直接用一个网卡接HUB/交换机来做。
网络拓扑如下:

对Linux的netfilter/iptables防火墙设置写在脚本里,如下:
# —————— begin adsl-nat.sh —————–
#!/usr/bin/env bash
## script to enable masquerading
## must be run as root after the ADSL connection is up
## usage: ./adsl-nat.sh
#
# bring up alias interface eth0:1 :
ifconfig eth0:1 192.168.1.1 netmask 255.255.255.0 \
broadcast 192.168.1.255 up
# Next, an iptables rule to enable masquerading:
iptables -t nat -I POSTROUTING -o ppp0 -j MASQUERADE
# Finally, enable ip forwarding:
echo 1 > /proc/sys/net/ipv4/ip_forward
# An optional rule to allow the laptop and desktop to talk to
# the Linux server (otherwise denied by my firewall script)
iptables -I INPUT -s 192.168.1.2 -d 192.168.1.1 \
-j ACCEPT
iptables -I INPUT -s 192.168.1.3 -d 192.168.1.1 \
-j ACCEPT
iptables -I INPUT -s 192.168.1.4 -d 192.168.1.1 \
-j ACCEPT
# —————— end adsl-nat.sh —————–
#!/usr/bin/env bash
## script to enable masquerading
## must be run as root after the ADSL connection is up
## usage: ./adsl-nat.sh
#
# bring up alias interface eth0:1 :
ifconfig eth0:1 192.168.1.1 netmask 255.255.255.0 \
broadcast 192.168.1.255 up
# Next, an iptables rule to enable masquerading:
iptables -t nat -I POSTROUTING -o ppp0 -j MASQUERADE
# Finally, enable ip forwarding:
echo 1 > /proc/sys/net/ipv4/ip_forward
# An optional rule to allow the laptop and desktop to talk to
# the Linux server (otherwise denied by my firewall script)
iptables -I INPUT -s 192.168.1.2 -d 192.168.1.1 \
-j ACCEPT
iptables -I INPUT -s 192.168.1.3 -d 192.168.1.1 \
-j ACCEPT
iptables -I INPUT -s 192.168.1.4 -d 192.168.1.1 \
-j ACCEPT
# —————— end adsl-nat.sh —————–
最近评论