linux双网卡vpn配置
时间:2015-01-19 18:24 来源:linux.it.net.cn 作者:IT
实现环境:
预装Linux(kernal version 2.6.27.15,Fedora 10 )的主机一台+双网卡+ADSL
VPN SERVER PC in a LAN
ADSL modem eth0 eth1 |#####|
公网-----------|######|---------------||----------||------------|#####|
|<----PPPoE----->|<---NAT--->|<----PPTP--->|
eth0接口用来通过拨号从网通获取公网ip,拨号成功后会在VPN Server上产生一个ppp0接
口,该接口的ip地址就是从公网获取的ip地址
eth1接口是与局域网相连的那块网卡接口,如果此接口已连上开启DHCP服务的路由器,则
它会有一个ip地址
PPPoE:ADSL专用的通讯协议,需要输入电信给你的帐号和密码;
PPTP:点对点隧道协议,是一种支持多协议虚拟专用网络的协议。
/******************************** A.VPN Server通过ADSL拨号上网
****************/
1.PPPoE软件的安装
1) 在 http://www.roaringpenguin.com/pppoe/#download 下载
2) 安装rp-pppoe。以root身份执行
2.查看与ADSL modem连接的那块网卡是否被系统识别
运行 ifconfig eth0
显示类似如下信息:
eth0 Link encap:Ethernet HWaddr 00:E0:4C:50:E1:90
inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::2e0:4cff:fe50:e190/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:11031331 errors:20041 dropped:0 overruns:0 frame:0
TX packets:9151127 errors:0 dropped:0 overruns:0 carrier:0
collisions:24 txqueuelen:1000
RX bytes:2507000230 (2.3 GiB) TX bytes:2096138656 (1.9 GiB)
Interrupt:17 Base address:0x4000
/***注意***/:a.不需要特意给eth0填上ip地址,此处的192.168.0.2是由于eth0所连接
的ADSL MODEM具有路由功能,故给eth0分配了ip地址,
但这并不影响我们的配置;b.不需要设置此网卡随开机启动
3.pppoe客户端(VPN Server)拨号设置
运行 #/usr/sbin/adsl-setup
>>> Enter your PPPoE user name: ——此处输入拨号帐号的用户名(申请adsl时网
通给分配的)
>>> Enter the Ethernet interface connected to the ADSL modem For Solaris,
this is likely to be something like /dev/hme0. For Linux, it will be ethn,
where 'n' is a number. (default eth0): ——输eth0(与adsl modem连接的那块网卡
接口)
>>> Enter the demand value (default no): ——输no
>>> Enter the DNS information here: ——输202.106.46.151,202.106.195.68(
网通的DNS)
>>> Please enter your PPPoE password: ——输网通用户口令
>>> Choose a type of firewall (0-2): ——输0
>>> Accept these settings and adjust configuration files (y/n)? ——输y
以上的这些设置将会更改以下两个文件
/etc/ppp/pppoe.conf ---涉及与adsl modem连接的接口选择,用户名等
/etc/resolv.conf ---与DNS有关
将用户名和密码存入/etc/ppp/chap-secrets中,以便下次连接时不用再此输入
4.启动ADSL连接
运行 #/usr/sbin/adsl-start
若一切正常,则显示
[root@PCN-CAD ppp]# adsl-start
... Connected!
至此,此VPN Server已经连上公网
5.运行 #ifconfig
eth0 Link encap:Ethernet HWaddr 00:E0:4C:50:E1:90
inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0
........
eth1 Link encap:Ethernet HWaddr 00:08:74:B7:03:C3
inet addr:59.64.154.187 Bcast:59.64.154.255 Mask:255.255.255.0
.........
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
.........
ppp0 Link encap:Point-to-Point Protocol
inet addr:123.119.245.174 P-t-P:123.119.240.1 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1492 Metric:1
RX packets:312 errors:0 dropped:0 overruns:0 frame:0
TX packets:459 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:65478 (63.9 KiB) TX bytes:61611 (60.1 KiB)
多出来的那个ppp0就是我们拨号成功后系统虚拟的一个接口
/************************************* END OF A ******************************
*/
/************************************ B.VPN Server开启pptpd服务
***************/
1.确保如下软件正确安装(版本可能会有出入)
kernel_ppp_mppe-0.0.4-2dkms.noarch.rpm (MPPE的内核补丁)
dkms-1.12-1.noarch.rpm (升级dkms)
pptpd-1.2.1.tar.gz (pptpd服务软件)
2.配置VPN服务器
修改/etc/pptpd.conf文件:
ppp /usr/sbin/pppd #指定PPP服务程序
option /etc/ppp/options.pptpd #指定选项配置
localip 59.64.154.187 #指定本地IP
remoteip 192.168.2.139-245 #指定分配的远程IP
修改/etc/ppp/options.pptpd文件:
name pptpd
refuse-pap
refuse-chap
refuse-mschap
require-mschap-v2
require-mppe-128
ms-dns 202.106.46.151
ms-dns 202.106.195.68
proxyarp
debug
lock
nobsdcomp
novj
novjccomp
nologfd
修改/etc/ppp/chap-secrets文件
# client server secret IP addresses
#username pptpd password *
"username" pptpd "passwd" *
这里是配置VPN的用户帐号,如"china" pptpd "beijing"
启动服务:
/usr/local/sbin/pptpd
然后用netstat查看一下1723端口是不是开的,如果是的话则说明服务已成功启动。
/*********************************** END OF B ********************************
*/
/******************************** C.利用iptables开启NAT功能 *****************
*/
NAT的功能是为了实现数据包的伪装及转发,从LAN里面的用户发出的包通过VPN SERVER的
eth1经报头伪装后转发到连接公网的eth0。
此部分参考鸟哥私房菜的NAT服务器架设篇
#!/bin/bash
#
# ========================================================
# 程式說明:
# 歡迎使用 iptables.rule 這個 script 來建立您的防火牆!
# 這支 script 還需要您的額外設定方可適合您的主機環境!
# 基本規則定義為『拒絕所有,開放特定』的模式!
#
# 強烈建議:
# 不了解 Linux 防火牆機制 iptables 的朋友使用這支 script
# 可能會不太瞭解每個指令列的意義,果真如此的話,
# 歡迎參考底下幾個網頁:
# http://www.study-area.org/linux/servers/linux_nat.htm
# http://linux.vbird.org/linux_server/0240network-secure-1.php
# http://linux.vbird.org/linux_server/0250simple_firewall.php
#
# 使用說明:
# 確定這個程式僅有 Linux 的斷行字元:
# dos2unix iptables.rule
# 請先將這個 scripts 的權限更改為可執行:
# chmod 755 iptables.rule
# 在將這個程式放置在 /usr/local/virus/iptables 目錄下:
# mkdir -p /usr/local/virus/iptables
# mv /完整的路徑/iptables.rule /usr/local/virus/iptables
# 執行測試:
# /usr/local/virus/iptables/iptables.rule
# iptables -L -n (這個動作在檢查防火牆規則)
# 將底下這一行加入 /etc/rc.d/rc.local 當中
# /usr/local/virus/iptables/iptables.rule
# 取消防火牆:
# iptables -F
# iptables -X
# iptables -t nat -F
# iptables -t nat -X
#
# ========================================================
# 版權宣告:
# 這支程式為 GPL 授權,任何人皆可使用,
# 然,若使用本 scripts 發生問題時,本人不負任何責任
# VBird <vbird@mail.vbird.idv.tw>
# ========================================================
#
# 歷史紀錄:
# 2002/08/20 VBird 首次釋出
# 2003/04/26 VBird 加入砍站軟體的相關執行檔案!
# 2003/08/25 VBird 修改 INPUT 的 Policy 成為 DROP
# 2006/09/13 VBird 重新修訂,加入一些核心參數 /proc/sys/net/ipv4/*
# 2006/09/15 VBird 加入關於 NAT 主機後端伺服器的轉址功能。
# 2006/09/30 VBird 加入每個設定項目的英文說明!!
# 2006/11/08 VBird 參考朋友們發現的 PPPoE 導致 MTU 的問題,增加一條規則!
在 NAT 的部分。
#
##############################################################################
#############
# 請先輸入您的相關參數,不要輸入錯誤了!
# English: Please input your networks parameters ( including your LAN NIC )
EXTIF="ppp0" # 這個是可以連上 Public IP 的網路介面
,也可能是 ppp0
# This is your NIC, connect to
internet. Such as ppp0...
INIF="eth1" # 內部 LAN 的連接介面;若無 LAN ,填寫
成 INIF=""
# This is your LAN NIC. If you don't
have a LAN, input INIF="" please.
INNET="192.168.2.0/24" # 若有兩個以上的網域,可以用
INNET="192.168.1.0/24 192.168.100.0/24"
# 若無內部網域介面,請填寫成 INNET=""
# This is your LAN's Network. If you
have to private network,
# input as INNET="192.168.1.0/24
192.168.100.0/24".
export EXTIF INIF INNET
# 個人化設定啊!請自行填寫您自己想要預先啟動的一些基礎資料。
# These settings is about yourself's paramters.
allowname='' # 允許登入本機的 hostname ,必須是 Internet 找
的到的 hostname。
allowip=""
if [ "$allowname" != "" ]; then
for siteiptmp in `echo $allowname`
do
siteip=`/usr/bin/host $siteiptmp 168.95.1.1 | grep address|tail
-n 1 | awk '{print $4}'`
testip=`echo $siteip | grep [^0-9.]`
if [ "$testip" == "" ]; then
allowip="$allowip $siteip"
fi
done
fi
export allowip
# 第一部份,針對本機的防火牆設定!############################################
##
# First, your server's firewall settings.
# 1. 先設定好核心的網路功能:
# 1. the kernel's firewall settings.
# 開啟 TCP Flooding 的 DoS 攻擊抵擋機制,但這個設定不適合 loading 已經很高的
主機!!!
# TCP Flooding's setting. this setting is no good for high loading servers
echo "1" > /proc/sys/net/ipv4/tcp_syncookies
# 取消 ping 廣播的回應;
# unset reply of ping.
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
# 開啟逆向路徑過濾,以符合 IP 封包與網路介面的設定;
#
for i in /proc/sys/net/ipv4/conf/*/rp_filter; do
echo "1" > $i
done
# 開啟記錄有問題的封包
# record some problems packets.
for i in /proc/sys/net/ipv4/conf/*/log_martians; do
echo "1" > $i
done
# 取消來源路由,這個設定值是可以取消的;
for i in /proc/sys/net/ipv4/conf/*/accept_source_route; do
echo "0" > $i
done
# 取消重新宣告路徑的功能。
for i in /proc/sys/net/ipv4/conf/*/accept_redirects; do
echo "0" > $i
done
# 取消傳送重新宣告路徑的功能。
for i in /proc/sys/net/ipv4/conf/*/send_redirects; do
echo "0" > $i
done
# 2. 清除規則、設定預設政策及開放 lo 與相關的設定值
# 2. clear rule, set the policy rule and allow lo connect.
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin; export
PATH
iptables -F
iptables -X
iptables -Z
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state RELATED -j ACCEPT
# 3. 啟動額外的防火牆 script 模組
# 3. other shell scripts, written by VBird.
# 預設抵擋的主機
if [ -f /usr/local/virus/iptables/iptables.deny ]; then
sh /usr/local/virus/iptables/iptables.deny
fi
# 預設開放的主機
if [ -f /usr/local/virus/iptables/iptables.allow ]; then
sh /usr/local/virus/iptables/iptables.allow
fi
# 透過 WWW 砍站軟體分析的抵擋機制
if [ -f /usr/local/virus/httpd-err/iptables.http ]; then
sh /usr/local/virus/httpd-err/iptables.http
fi
iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
# 4. 允許某些類型的 ICMP 封包進入
# 4. allow some types of ICMP
AICMP="0 3 3/4 4 11 12 14 16 18"
for tyicmp in $AICMP
do
iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT
done
# 5. 允許某些服務的進入
# iptables -A INPUT -p TCP -i $EXTIF --dport 22 -j ACCEPT # SSH
# iptables -A INPUT -p TCP -i $EXTIF --dport 25 -j ACCEPT # SMTP
# iptables -A INPUT -p UDP -i $EXTIF --dport 53 -j ACCEPT # DNS
# iptables -A INPUT -p TCP -i $EXTIF --dport 53 -j ACCEPT # DNS
# iptables -A INPUT -p TCP -i $EXTIF --dport 80 -j ACCEPT # WWW
# iptables -A INPUT -p TCP -i $EXTIF --dport 110 -j ACCEPT # POP3
# iptables -A INPUT -p TCP -i $EXTIF --dport 443 -j ACCEPT # HTTPS
# 第二部份,針對後端主機的防火牆設定!########################################
##
# Second, the NAT settings.
# 1. 先載入一些有用的模組
# 1. loading some good modules of iptables.
# modules="ip_tables iptable_nat ip_nat_ftp ip_nat_irc ip_conntrack
ip_conntrack_ftp ip_conntrack_irc"
modules="iptable_nat ip_nat_ftp ip_nat_irc ip_conntrack_ftp ip_conntrack_irc
"
for mod in $modules
do
testmod=`lsmod | grep "^${mod} " | awk '{print $1}'`
if [ "$testmod" == "" ]; then
modprobe $mod
fi
done
# 2. 清除 NAT table 的規則吧!
# 2. clean NAT table's rule
iptables -F -t nat
iptables -X -t nat
iptables -Z -t nat
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT
# 3. 開放成為路由器,且為 IP 分享器!
# 3. NAT server's settings
if [ "$INIF" != "" ]; then
iptables -A INPUT -i $INIF -j ACCEPT
echo "1" > /proc/sys/net/ipv4/ip_forward
if [ "$INNET" != "" ]; then
for innet in $INNET
do
iptables -t nat -A POSTROUTING -s $innet -o $EXTIF -j
MASQUERADE
done
fi
fi
# 如果你的 MSN 一直無法連線,或者是某些網站 OK 某些網站不 OK,可能是 MTU 的
問題,
# 那你可以將底下這一行給他取消註解來啟動 MTU 限制範圍
iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss --mss
1400:1536 -j TCPMSS --clamp-mss-to-pmtu
# 4. NAT 主機後端的 LAN 內對外之伺服器設定
# iptables -t nat -A PREROUTING -p tcp -i $EXTIF --dport 80 -j DNAT --to
192.168.1.210:80 # WWW
/*********************************** END OF C ********************************
*/
按照上面的方法配置完防火墙和NAT规则之后,执行该脚本文件
然后运行
#route -n
查看默认网关是否是ppp0的ip地址,若不是,则手动添加该默认网关,具体如下
用su命令切换到root用户
执行 #route add default gw "ppp0的ip地址(不包括引号)"
至此,服务器的NAT功能开启
/*********************************** D.局域网中客户端的配置 ******************
*/
windows 客户端配置如下
网上邻居--右键-属性--创建一个新的连接--下一步--连接到我的工作场所的网
络--虚拟专用网络连接--公司名(随便写一个,如bupt)
-下一步--vpn服务器选择(写上你要拨入的IP或域名,我们的服务器是59.64.154.187
)--完成
--填入你的用户名和密码(用户名:china,密码:beijing,为方便使用您最好在保存密
码打上勾)--连接。
linux 客户端配置
1.安装pptp client
2.运行pptp client,在图形界面中依次填入服务器名,服务器地址,用户名和密码
3.连接
/*********************************** END OF D ********************************
*/
/********************************** E.Some Tips ******************************
*/
为方便VPN SERVER重启后能自动连接adsl,自动开启NAT服务,自动启动pptpd服务,
可将下列语句加入/etc/rc.d/rc.local文件中
#1.adsl connectting 2009/03/27
/usr/sbin/adsl-start
#2.starting the NAT server and firewall settings 2009/03/27
/usr/local/virus/iptables/iptables.rule
#3.change the level of SElinux to permissive 2009/03/27
/usr/sbin/setenforce 0
#4.starting pptpd server 2009/03/27
/usr/sbin/service pptpd start
%%%%%%%%%%%%%%%%% 注意:SElinux可能会阻止pptpd程序对某些文件的写入,需要将其设
为permissive模式 %%%%%%%%%%%%%%%%%
/********************************** END OF E *********************************
*/ (责任编辑:IT)
实现环境: 预装Linux(kernal version 2.6.27.15,Fedora 10 )的主机一台+双网卡+ADSL VPN SERVER PC in a LAN ADSL modem eth0 eth1 |#####| 公网-----------|######|---------------||----------||------------|#####| |<----PPPoE----->|<---NAT--->|<----PPTP--->| eth0接口用来通过拨号从网通获取公网ip,拨号成功后会在VPN Server上产生一个ppp0接 口,该接口的ip地址就是从公网获取的ip地址 eth1接口是与局域网相连的那块网卡接口,如果此接口已连上开启DHCP服务的路由器,则 它会有一个ip地址 PPPoE:ADSL专用的通讯协议,需要输入电信给你的帐号和密码; PPTP:点对点隧道协议,是一种支持多协议虚拟专用网络的协议。 /******************************** A.VPN Server通过ADSL拨号上网 ****************/ 1.PPPoE软件的安装 1) 在 http://www.roaringpenguin.com/pppoe/#download 下载 2) 安装rp-pppoe。以root身份执行 2.查看与ADSL modem连接的那块网卡是否被系统识别 运行 ifconfig eth0 显示类似如下信息: eth0 Link encap:Ethernet HWaddr 00:E0:4C:50:E1:90 inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::2e0:4cff:fe50:e190/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:11031331 errors:20041 dropped:0 overruns:0 frame:0 TX packets:9151127 errors:0 dropped:0 overruns:0 carrier:0 collisions:24 txqueuelen:1000 RX bytes:2507000230 (2.3 GiB) TX bytes:2096138656 (1.9 GiB) Interrupt:17 Base address:0x4000 /***注意***/:a.不需要特意给eth0填上ip地址,此处的192.168.0.2是由于eth0所连接 的ADSL MODEM具有路由功能,故给eth0分配了ip地址, 但这并不影响我们的配置;b.不需要设置此网卡随开机启动 3.pppoe客户端(VPN Server)拨号设置 运行 #/usr/sbin/adsl-setup >>> Enter your PPPoE user name: ——此处输入拨号帐号的用户名(申请adsl时网 通给分配的) >>> Enter the Ethernet interface connected to the ADSL modem For Solaris, this is likely to be something like /dev/hme0. For Linux, it will be ethn, where 'n' is a number. (default eth0): ——输eth0(与adsl modem连接的那块网卡 接口) >>> Enter the demand value (default no): ——输no >>> Enter the DNS information here: ——输202.106.46.151,202.106.195.68( 网通的DNS) >>> Please enter your PPPoE password: ——输网通用户口令 >>> Choose a type of firewall (0-2): ——输0 >>> Accept these settings and adjust configuration files (y/n)? ——输y 以上的这些设置将会更改以下两个文件 /etc/ppp/pppoe.conf ---涉及与adsl modem连接的接口选择,用户名等 /etc/resolv.conf ---与DNS有关 将用户名和密码存入/etc/ppp/chap-secrets中,以便下次连接时不用再此输入 4.启动ADSL连接 运行 #/usr/sbin/adsl-start 若一切正常,则显示 [root@PCN-CAD ppp]# adsl-start ... Connected! 至此,此VPN Server已经连上公网 5.运行 #ifconfig eth0 Link encap:Ethernet HWaddr 00:E0:4C:50:E1:90 inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0 ........ eth1 Link encap:Ethernet HWaddr 00:08:74:B7:03:C3 inet addr:59.64.154.187 Bcast:59.64.154.255 Mask:255.255.255.0 ......... lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 ......... ppp0 Link encap:Point-to-Point Protocol inet addr:123.119.245.174 P-t-P:123.119.240.1 Mask:255.255.255.255 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1492 Metric:1 RX packets:312 errors:0 dropped:0 overruns:0 frame:0 TX packets:459 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:3 RX bytes:65478 (63.9 KiB) TX bytes:61611 (60.1 KiB) 多出来的那个ppp0就是我们拨号成功后系统虚拟的一个接口 /************************************* END OF A ****************************** */ /************************************ B.VPN Server开启pptpd服务 ***************/ 1.确保如下软件正确安装(版本可能会有出入) kernel_ppp_mppe-0.0.4-2dkms.noarch.rpm (MPPE的内核补丁) dkms-1.12-1.noarch.rpm (升级dkms) pptpd-1.2.1.tar.gz (pptpd服务软件) 2.配置VPN服务器 修改/etc/pptpd.conf文件: ppp /usr/sbin/pppd #指定PPP服务程序 option /etc/ppp/options.pptpd #指定选项配置 localip 59.64.154.187 #指定本地IP remoteip 192.168.2.139-245 #指定分配的远程IP 修改/etc/ppp/options.pptpd文件: name pptpd refuse-pap refuse-chap refuse-mschap require-mschap-v2 require-mppe-128 ms-dns 202.106.46.151 ms-dns 202.106.195.68 proxyarp debug lock nobsdcomp novj novjccomp nologfd 修改/etc/ppp/chap-secrets文件 # client server secret IP addresses #username pptpd password * "username" pptpd "passwd" * 这里是配置VPN的用户帐号,如"china" pptpd "beijing" 启动服务: /usr/local/sbin/pptpd 然后用netstat查看一下1723端口是不是开的,如果是的话则说明服务已成功启动。 /*********************************** END OF B ******************************** */ /******************************** C.利用iptables开启NAT功能 ***************** */ NAT的功能是为了实现数据包的伪装及转发,从LAN里面的用户发出的包通过VPN SERVER的 eth1经报头伪装后转发到连接公网的eth0。 此部分参考鸟哥私房菜的NAT服务器架设篇 #!/bin/bash # # ======================================================== # 程式說明: # 歡迎使用 iptables.rule 這個 script 來建立您的防火牆! # 這支 script 還需要您的額外設定方可適合您的主機環境! # 基本規則定義為『拒絕所有,開放特定』的模式! # # 強烈建議: # 不了解 Linux 防火牆機制 iptables 的朋友使用這支 script # 可能會不太瞭解每個指令列的意義,果真如此的話, # 歡迎參考底下幾個網頁: # http://www.study-area.org/linux/servers/linux_nat.htm # http://linux.vbird.org/linux_server/0240network-secure-1.php # http://linux.vbird.org/linux_server/0250simple_firewall.php # # 使用說明: # 確定這個程式僅有 Linux 的斷行字元: # dos2unix iptables.rule # 請先將這個 scripts 的權限更改為可執行: # chmod 755 iptables.rule # 在將這個程式放置在 /usr/local/virus/iptables 目錄下: # mkdir -p /usr/local/virus/iptables # mv /完整的路徑/iptables.rule /usr/local/virus/iptables # 執行測試: # /usr/local/virus/iptables/iptables.rule # iptables -L -n (這個動作在檢查防火牆規則) # 將底下這一行加入 /etc/rc.d/rc.local 當中 # /usr/local/virus/iptables/iptables.rule # 取消防火牆: # iptables -F # iptables -X # iptables -t nat -F # iptables -t nat -X # # ======================================================== # 版權宣告: # 這支程式為 GPL 授權,任何人皆可使用, # 然,若使用本 scripts 發生問題時,本人不負任何責任 # VBird <vbird@mail.vbird.idv.tw> # ======================================================== # # 歷史紀錄: # 2002/08/20 VBird 首次釋出 # 2003/04/26 VBird 加入砍站軟體的相關執行檔案! # 2003/08/25 VBird 修改 INPUT 的 Policy 成為 DROP # 2006/09/13 VBird 重新修訂,加入一些核心參數 /proc/sys/net/ipv4/* # 2006/09/15 VBird 加入關於 NAT 主機後端伺服器的轉址功能。 # 2006/09/30 VBird 加入每個設定項目的英文說明!! # 2006/11/08 VBird 參考朋友們發現的 PPPoE 導致 MTU 的問題,增加一條規則! 在 NAT 的部分。 # ############################################################################## ############# # 請先輸入您的相關參數,不要輸入錯誤了! # English: Please input your networks parameters ( including your LAN NIC ) EXTIF="ppp0" # 這個是可以連上 Public IP 的網路介面 ,也可能是 ppp0 # This is your NIC, connect to internet. Such as ppp0... INIF="eth1" # 內部 LAN 的連接介面;若無 LAN ,填寫 成 INIF="" # This is your LAN NIC. If you don't have a LAN, input INIF="" please. INNET="192.168.2.0/24" # 若有兩個以上的網域,可以用 INNET="192.168.1.0/24 192.168.100.0/24" # 若無內部網域介面,請填寫成 INNET="" # This is your LAN's Network. If you have to private network, # input as INNET="192.168.1.0/24 192.168.100.0/24". export EXTIF INIF INNET # 個人化設定啊!請自行填寫您自己想要預先啟動的一些基礎資料。 # These settings is about yourself's paramters. allowname='' # 允許登入本機的 hostname ,必須是 Internet 找 的到的 hostname。 allowip="" if [ "$allowname" != "" ]; then for siteiptmp in `echo $allowname` do siteip=`/usr/bin/host $siteiptmp 168.95.1.1 | grep address|tail -n 1 | awk '{print $4}'` testip=`echo $siteip | grep [^0-9.]` if [ "$testip" == "" ]; then allowip="$allowip $siteip" fi done fi export allowip # 第一部份,針對本機的防火牆設定!############################################ ## # First, your server's firewall settings. # 1. 先設定好核心的網路功能: # 1. the kernel's firewall settings. # 開啟 TCP Flooding 的 DoS 攻擊抵擋機制,但這個設定不適合 loading 已經很高的 主機!!! # TCP Flooding's setting. this setting is no good for high loading servers echo "1" > /proc/sys/net/ipv4/tcp_syncookies # 取消 ping 廣播的回應; # unset reply of ping. echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts # 開啟逆向路徑過濾,以符合 IP 封包與網路介面的設定; # for i in /proc/sys/net/ipv4/conf/*/rp_filter; do echo "1" > $i done # 開啟記錄有問題的封包 # record some problems packets. for i in /proc/sys/net/ipv4/conf/*/log_martians; do echo "1" > $i done # 取消來源路由,這個設定值是可以取消的; for i in /proc/sys/net/ipv4/conf/*/accept_source_route; do echo "0" > $i done # 取消重新宣告路徑的功能。 for i in /proc/sys/net/ipv4/conf/*/accept_redirects; do echo "0" > $i done # 取消傳送重新宣告路徑的功能。 for i in /proc/sys/net/ipv4/conf/*/send_redirects; do echo "0" > $i done # 2. 清除規則、設定預設政策及開放 lo 與相關的設定值 # 2. clear rule, set the policy rule and allow lo connect. PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin; export PATH iptables -F iptables -X iptables -Z iptables -P INPUT DROP iptables -P OUTPUT ACCEPT iptables -P FORWARD ACCEPT iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -m state --state RELATED -j ACCEPT # 3. 啟動額外的防火牆 script 模組 # 3. other shell scripts, written by VBird. # 預設抵擋的主機 if [ -f /usr/local/virus/iptables/iptables.deny ]; then sh /usr/local/virus/iptables/iptables.deny fi # 預設開放的主機 if [ -f /usr/local/virus/iptables/iptables.allow ]; then sh /usr/local/virus/iptables/iptables.allow fi # 透過 WWW 砍站軟體分析的抵擋機制 if [ -f /usr/local/virus/httpd-err/iptables.http ]; then sh /usr/local/virus/httpd-err/iptables.http fi iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT # 4. 允許某些類型的 ICMP 封包進入 # 4. allow some types of ICMP AICMP="0 3 3/4 4 11 12 14 16 18" for tyicmp in $AICMP do iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT done # 5. 允許某些服務的進入 # iptables -A INPUT -p TCP -i $EXTIF --dport 22 -j ACCEPT # SSH # iptables -A INPUT -p TCP -i $EXTIF --dport 25 -j ACCEPT # SMTP # iptables -A INPUT -p UDP -i $EXTIF --dport 53 -j ACCEPT # DNS # iptables -A INPUT -p TCP -i $EXTIF --dport 53 -j ACCEPT # DNS # iptables -A INPUT -p TCP -i $EXTIF --dport 80 -j ACCEPT # WWW # iptables -A INPUT -p TCP -i $EXTIF --dport 110 -j ACCEPT # POP3 # iptables -A INPUT -p TCP -i $EXTIF --dport 443 -j ACCEPT # HTTPS # 第二部份,針對後端主機的防火牆設定!######################################## ## # Second, the NAT settings. # 1. 先載入一些有用的模組 # 1. loading some good modules of iptables. # modules="ip_tables iptable_nat ip_nat_ftp ip_nat_irc ip_conntrack ip_conntrack_ftp ip_conntrack_irc" modules="iptable_nat ip_nat_ftp ip_nat_irc ip_conntrack_ftp ip_conntrack_irc " for mod in $modules do testmod=`lsmod | grep "^${mod} " | awk '{print $1}'` if [ "$testmod" == "" ]; then modprobe $mod fi done # 2. 清除 NAT table 的規則吧! # 2. clean NAT table's rule iptables -F -t nat iptables -X -t nat iptables -Z -t nat iptables -t nat -P PREROUTING ACCEPT iptables -t nat -P POSTROUTING ACCEPT iptables -t nat -P OUTPUT ACCEPT # 3. 開放成為路由器,且為 IP 分享器! # 3. NAT server's settings if [ "$INIF" != "" ]; then iptables -A INPUT -i $INIF -j ACCEPT echo "1" > /proc/sys/net/ipv4/ip_forward if [ "$INNET" != "" ]; then for innet in $INNET do iptables -t nat -A POSTROUTING -s $innet -o $EXTIF -j MASQUERADE done fi fi # 如果你的 MSN 一直無法連線,或者是某些網站 OK 某些網站不 OK,可能是 MTU 的 問題, # 那你可以將底下這一行給他取消註解來啟動 MTU 限制範圍 iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1400:1536 -j TCPMSS --clamp-mss-to-pmtu # 4. NAT 主機後端的 LAN 內對外之伺服器設定 # iptables -t nat -A PREROUTING -p tcp -i $EXTIF --dport 80 -j DNAT --to 192.168.1.210:80 # WWW /*********************************** END OF C ******************************** */ 按照上面的方法配置完防火墙和NAT规则之后,执行该脚本文件 然后运行 #route -n 查看默认网关是否是ppp0的ip地址,若不是,则手动添加该默认网关,具体如下 用su命令切换到root用户 执行 #route add default gw "ppp0的ip地址(不包括引号)" 至此,服务器的NAT功能开启 /*********************************** D.局域网中客户端的配置 ****************** */ windows 客户端配置如下 网上邻居--右键-属性--创建一个新的连接--下一步--连接到我的工作场所的网 络--虚拟专用网络连接--公司名(随便写一个,如bupt) -下一步--vpn服务器选择(写上你要拨入的IP或域名,我们的服务器是59.64.154.187 )--完成 --填入你的用户名和密码(用户名:china,密码:beijing,为方便使用您最好在保存密 码打上勾)--连接。 linux 客户端配置 1.安装pptp client 2.运行pptp client,在图形界面中依次填入服务器名,服务器地址,用户名和密码 3.连接 /*********************************** END OF D ******************************** */ /********************************** E.Some Tips ****************************** */ 为方便VPN SERVER重启后能自动连接adsl,自动开启NAT服务,自动启动pptpd服务, 可将下列语句加入/etc/rc.d/rc.local文件中 #1.adsl connectting 2009/03/27 /usr/sbin/adsl-start #2.starting the NAT server and firewall settings 2009/03/27 /usr/local/virus/iptables/iptables.rule #3.change the level of SElinux to permissive 2009/03/27 /usr/sbin/setenforce 0 #4.starting pptpd server 2009/03/27 /usr/sbin/service pptpd start %%%%%%%%%%%%%%%%% 注意:SElinux可能会阻止pptpd程序对某些文件的写入,需要将其设 为permissive模式 %%%%%%%%%%%%%%%%% /********************************** END OF E ********************************* */ (责任编辑:IT) |