Ubuntu设置为路由器的方法分享

发布时间:2020-07-31编辑:脚本学堂
本文介绍下,将ubuntu作为路由器的配置方法,有需要的朋友参考下。

把ubuntu作为路由器,需要下面的步骤:
 

设置内/外网络接口
启用IP转发和伪装

步骤1,配置(eth1外网(wifi),eth0内网):
 

复制代码 代码示例:
#路由器(有线)
iface eth0 inet static
address 192.168.0.1
network 192.168.0.0
netmask 255.255.255.0
broadcast 192.168.0.255

步骤2,配置脚本
 

复制代码 代码示例:

echo -e "nnLoading simple rc.firewall-iptables version $FWVER..n"
DEPMOD=/sbin/depmod
MODPROBE=/sbin/modprobe

EXTIF="eth0"
INTIF="eth1"
#INTIF2="eth0"
echo " External Interface: $EXTIF"
echo " Internal Interface: $INTIF"

#==========================
#== No editing beyond this line is required for initial MASQ testing ==
echo -en " loading modules: "
echo " - Verifying that all kernel modules are ok"
$DEPMOD -a
echo "----------------------------------------------------------------------"
echo -en "ip_tables, "
$MODPROBE ip_tables
echo -en "nf_conntrack, "
$MODPROBE nf_conntrack
echo -en "nf_conntrack_ftp, "
$MODPROBE nf_conntrack_ftp
echo -en "nf_conntrack_irc, "
$MODPROBE nf_conntrack_irc
echo -en "iptable_nat, "
$MODPROBE iptable_nat
echo -en "nf_nat_ftp, "
$MODPROBE nf_nat_ftp
echo "----------------------------------------------------------------------"
echo -e " Done loading modules.n"
echo " Enabling forwarding.."
echo "1" > /proc/sys/net/ipv4/ip_forward
echo " Enabling DynamicAddr.."
echo "1" > /proc/sys/net/ipv4/ip_dynaddr
echo " Clearing any existing rules and setting default policy.."

iptables-restore <<-EOF
*nat
-A POSTROUTING -o "$EXTIF" -j MASQUERADE
COMMIT
*filter
:INPUT ACCEPT [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
-A FORWARD -i "$EXTIF" -o "$INTIF" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
-A FORWARD -i "$INTIF" -o "$EXTIF" -j ACCEPT
-A FORWARD -j LOG
COMMIT
EOF

echo -e "nrc.firewall-iptables v$FWVER done.n"

注意:
若要做无线路由器,文中使用的WEP加密方式,这是很不安全的,可以考虑配置WPA加密方式的无线路由器。
这个大家可以找些文章看看。

附,路由相关知识的链接:
Router:https://help.ubuntu.com/community/Router