這是本人第一篇博客,也算是本身的嘗試,之前學習東西,通常都是以文檔的方式記錄。文檔有時候容易丟失,因此開始嘗試用博客記錄學習歷程。畢業在即,在此記錄畢設的一部份內容。php
內容:Ubuntu下搭建L2TP 虛擬專用網服務器(校園網Intranet ×××)
html
1 查看服務器主機網絡信息linux
:$ ifconfig eth0 Link encap:Ethernet HWaddr fa:16:3e:a0:64:0c inet addr:172.16.0.61 Bcast:172.16.255.255 Mask:255.255.0.0(IP地址)
2 測試服務器主機可否訪問Internetubuntu
此步驟主要是驗證服務器主機是否能做爲×××的網絡出口。vim
:~$ ping baidu.com PING baidu.com (111.13.101.208) 56(84) bytes of data. 64 bytes from 111.13.101.208: icmp_seq=1 ttl=45 time=52.2 ms 64 bytes from 111.13.101.208: icmp_seq=2 ttl=45 time=53.1 ms 64 bytes from 111.13.101.208: icmp_seq=3 ttl=45 time=56.9 ms
由終端結果可知,服務器主機是能夠ping通Internet的,故當×××客戶端(校園網中的I區即Internet區)鏈接到×××服務器(校園網中的Non-I區即非Internet區)時,是能夠實現×××隧道而訪問互聯網的。
c#
3 切換到root用戶,得到root權限windows
ubuntu@XXX:~$ sudo -i sudo: unable to resolve host XXX(本身主機名)-*** root@XXX:~#
4安裝L2TP bash
安裝中若遇到unable to locate package能夠使用apt-get update解決,若實在不行也能夠使用apt-get upgrade解決,更新源,再安裝。服務器
安裝L2TP網絡
:~# sudo apt-get install openswan ppp xl2tpd -y sudo: unable to resolve host zhantengfei-*** Reading package lists... Done Building dependency tree ............
5 配置相關文件
5.1 修改ipsec.conf文件
:~# vim /etc/ipsec.conf //使用vim命令查看相關文件,使用ipsec進行加密
編輯好配置文件後「ESC+:wq」保存,如下爲編輯後的ipsec.conf
config setup # Do not set debug options to debug configuration issues! # plutodebug / klipsdebug = "all", "none" or a combation from below: # "raw crypt parsing emitting control klips pfkey natt x509 dpd private" # eg: # plutodebug="control parsing" # Again: only enable plutodebug or klipsdebug when asked by a developer # # enable to get logs per-peer # plutoopts="--perpeerlog" # # Enable core dumps (might require system changes, like ulimit -C) # This is required for abrtd to work properly # Note: incorrect SElinux policies might prevent pluto writing the core dumpdir=/var/run/pluto/ # # NAT-TRAVERSAL support, see README.NAT-Traversal nat_traversal=yes # exclude networks used on server side by adding %v4:!a.b.c.0/24 # It seems that T-Mobile in the US and Rogers/Fido in Canada are # using 25/8 as "private" address space on their 3G network. # This range has not been announced via BGP (at least upto 2010-12-21) virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v4:25.0.0.0/8,%v6:fd00::/8,%v6:fe80::/10 # OE is now off by default. Uncomment and change to on, to enable. oe=off # which IPsec stack to use. auto will try netkey, then klips then mast protostack=netkey force_keepalive=yes keep_alive=1800 conn L2TP-PSK-NAT rightsubnet=vhost:%priv also=L2TP-PSK-noNAT leftnexthop=%defaultroute rightnexthop=%defaultroute conn L2TP-PSK-noNAT authby=secret pfs=no auto=add keyingtries=3 rekey=no ikelifetime=8h keylife=1h type=transport left=172.16.0.61//設置爲服務端的外網ip地址 leftprotoport=17/1701 right=%any rightprotoport=17/%any dpddelay=40 dpdtimeout=130 dpdaction=clear # Use this to log to a file, or disable logging on embedded systems (like openwrt)
5.2 使用vi命令修改ipsec.secrets文件
:/etc# vi ipsec.secrets #include /var/lib/openswan/ipsec.secrets.inc #注意這一行要註釋掉 172.25.11.223%any:PSK"1110005440" //設置爲服務器內網的地址,並設置密碼(能夠自定義設置) include /var/lib/openswan/ipsec.secrets.inc
5.3 使用命令修改sysctl文件
:~# vi /etc/sysctl.conf # /etc/sysct.conf # only values specific for ipsec/l2tp functioning are shown here. merge with # existing file net.ipv4.ip_forward = 1 net.ipv4.conf.default.rp_filter = 0 net.ipv4.conf.default.accept_source_route = 0 net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.icmp_ignore_bogus_error_responses = 1
5.4 執行以下腳本
for *** in /proc/sys/net/ipv4/conf/*; do echo 0 > $***/accept_redirects; echo 0 > $***/send_redirects; done sysctl -p //使修改生效
5.5 使用命令修改xl2tp.conf文件
:~# vi /etc/xl2tpd/xl2tpd.conf
寫入如下信息
auth file = /etc/ppp/chap-secrets port = 1701 [lns default] ip range = 10.0.0.10-10.0.4.254 local ip = 10.0.0.9 refuse chap = yes refuse pap = yes require authentication = yes name = L2TP××× ppp debug = yes pppoptfile = /etc/ppp/options.xl2tpd length bit = yes
5.6修改options.xl2tpd 文件
:~# vi /etc/ppp# vi options.xl2tpd
#require-pap #require-chap #require-mschap ipcp-accept-local ipcp-accept-remote require-mschap-v2 ms-dns 114.114.114.114 ms-dns 8.8.4.4 asyncmap 0 auth crtscts lock hide-password modem debug name l2tpd proxyarp lcp-echo-interval 30 lcp-echo-failure 4 mtu 1400 noccp connect-delay 5000 http://rootmanager.com/ubuntu-ipsec-l2tp-windows-domain-auth/setting-up-openswan-xl2tpd-with-native-windows-clients-lucid.html
5.7修改chap-secrets文件
:~# vi /etc/ppp/chap-secrets
# Secrets for authentication using CHAP # client server secret IP addresses Spencer l2tpd YourPassWord *
5.8最後配置防火牆
#iptables -t nat -A POSTROUTING -s 10.0.0.0/22 -o eth0 -j MASQUERADE //決定客戶端是否可訪問Internet
iptables --table nat --append POSTROUTING --jump MASQUERADE iptables -t nat -A POSTROUTING -s 10.0.0.0/22 -o eth0 -j MASQUERADE iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -I FORWARD -s 10.0.0.0/22 -j ACCEPT iptables -I FORWARD -d 10.0.0.0/22 -j ACCEPT iptables -A FORWARD -j REJECT iptables -A INPUT -p udp -m state --state NEW -m udp --dport 1701 -j ACCEPT iptables -A INPUT -p udp -m state --state NEW -m udp --dport 500 -j ACCEPT iptables -A INPUT -p udp -m state --state NEW -m udp --dport 4500 -j ACCEPT /etc/rc.d/init.d/iptables save /etc/rc.d/init.d/iptables restart
其中這兩句寫入/etc/rc.local:
for *** in /proc/sys/net/ipv4/conf/*; do echo 0 > $***/accept_redirects; echo 0 > $***/send_redirects; done iptables --table nat --append POSTROUTING --jump MASQUERADE
5.9驗證配置是否成功:
:~# ipsec verify
Checking if IPsec got installed and started correctly: Version check and ipsec on-path [OK] Openswan U2.6.49/K3.13.0-65-generic (netkey) See `ipsec --copyright' for copyright information. Checking for IPsec support in kernel [OK] NETKEY: Testing XFRM related proc values ICMP default/send_redirects [OK] ICMP default/accept_redirects [OK] XFRM larval drop [OK] Hardware random device check [N/A] Two or more interfaces found, checking IP forwarding [OK] Checking rp_filter [OK] Checking that pluto is running [OK] Pluto listening for IKE on udp 500 [OK] Pluto listening for IKE on tcp 500 [NOT IMPLEMENTED] Pluto listening for IKE/NAT-T on udp 4500 [OK] Pluto listening for IKE/NAT-T on tcp 4500 [NOT IMPLEMENTED] Pluto listening for IKE on tcp 10000 (cisco) [NOT IMPLEMENTED] Checking NAT and MASQUERADEing [TEST INCOMPLETE] Checking 'ip' command [OK] Checking 'iptables' command [OK]
以上只要不出現Failed就行,因爲L2TP使用1701端口,是UDP傳輸,故關於TCP的項無所謂
最終查看鏈接或者調試錯誤時查看日誌 /var/log/syslog :
tail -f /var/log/syslog
6 客戶端鏈接×××服務器
首先須要鏈接到內網,使客戶機與服務器處於同一個LAN中。
因爲我在安裝Openswan時,使用的是證書安裝,因此在客戶端鏈接時,須要使用L2TP/IPsec證書鏈接。
7 訪問Internet
鏈接到×××服務器後,能夠訪問因特網。
References:
1)http://lesca.me/archives/how-to-setup-l2tp-over-ipsec-on-ubuntu.htm l#comments