现在流行的ipsec来自openswan,来自xd。本文将以这两个软件包进行讲解如何配置。当提及openswan的时候就是指ipsec,当说到xd的时候就是指。希望读者能够明白。
/!\重要:笔者一开始在Ubuntu 11.04上进行实验,Kernel 3.0,IPSec版本2.6.23。这里有几个严重问题:
因此,请至少使用openswan 2.6.37的版本,当前最新版本是2.6.38。源代码可在这里下载:http://download.openswan.org/openswan/,之后需要通过编译完成安装。对于openswan的安装我们将会提供两种不同的方法。
apt-get install python-software-properties add-apt-repository ppa:openswan/ppa apt-get update apt-get install openswan
如果出现Do you want to create a RSA public/private keypair for this host?。我们选择No,这种模式的IPSec通过证书进行验证,而非PSK。
获取最新软件包:
wget http://download.openswan.org/openswan/openswan-2.6.38.tar.gz tar -xzf openswan-2.6.38.tar.gz
编译不同的版本可能会依赖不同的库文件,因此如果你正在编译更高版本的openswan,请务必先阅读README文件。这里我们安装如下库文件:
apt-get install libgmp3-dev flex bison
安装这些库会占用大量空间,而且在服务器上编译二进制文件通常不是明智之举!但是有时候服务器架构会与你的测试机完全不同,因此可能你仍然需要在服务器上做这些工作。接下来我们开始编译:
make programs install
如果一切顺利,编译工作完成后配置文件及服务会自动安装到合适的地方,也就是和通过apt-get安装没什么两样了。
Ubuntu中xd的版本目前没有发现严重问题。因此安装起来也比较方便:
apt-get install xd
配置工作分为三部分:IPSec配置、L2TP配置、PPP配置
# /etc/ipsec.conf config setup nat_traversal=yes 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=off protostack=netkey conn L2TP-PSK-NAT rightsubnet=vhost:%priv also=L2TP-PSK-noNAT conn L2TP-PSK-noNAT authby=secret pfs=no auto=add keyingtries=3 # we cannot rekey for %any, let client rekey rekey=no # Apple iOS doesn't send delete notify so we need dead peer detection # to detect vanishing clients dpddelay=10 dpdtimeout=90 dpdaction=clear # Set ikelifetime and keylife to same defaults windows has ikelifetime=8h keylife=1h # -over-ipsec is transport mode type=transport # left=$IP # # For updated Windows 2000/XP clients, # to support old clients as well, use leftprotoport=17/%any leftprotoport=17/1701 # # The remote user. # right=%any # Using the magic port of "%any" means "any one single port". This is # a work around required for Apple OSX clients that use a randomly # high port. rightprotoport=17/%any conn passthrough-for-non- type=passthrough left=$IP leftnexthop=$GATEWAY right=0.0.0.0 rightsubnet=0.0.0.0/0 auto=route
其中,$IP改成你主机以太网卡的IP,$GATEWAY改成该IP所在网段的网关地址,下同。对于下面的密钥文件,你需要将密钥引在引号中。
# /etc/ipsec.secrets $IP %any: PSK "Your Preshared Key Here"
最后我们需要修改某些网络策略,让ipsec正常运行:
for each in /proc/sys/net/ipv4/conf/* do echo 0 > $each/accept_redirects echo 0 > $each/send_redirects done
# /etc/xd/xd.conf [global] ipsec saref = yes [lns default] ip range = 10.10.20.100-10.10.20.254 local ip = 10.10.20.1 require chap = yes refuse pap = yes require authentication = yes ppp debug = yes pppoptfile = /etc/ppp/options.xd length bit = yes
# /etc/ppp/options.xd refuse-mschap-v2 refuse-mschap ms-dns 8.8.8.8 ms-dns 8.8.4.4 asyncmap 0 auth lock hide-password local #debug name d proxyarp lcp-echo-interval 30 lcp-echo-failure 4 mtu 1404 mru 1404
echo 1 > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
对于OpenVZ的主机,可能不支持MASQUERADE,此时需要使用SNAT:
iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source $IP
其中$IP就是你主机的eth0接口的IP地址。
我们通过执行ipsec verify
命令进行验证ipsec服务是否工作正常:
Checking your system to see if IPsec got installed and started correctly: Version check and ipsec on-path [OK] Linux Openswan U2.6.38/K(no kernel code presently loaded) Checking for IPsec support in kernel [FAILED] SAref kernel support [N/A] Checking that pluto is running [FAILED] whack: Pluto is not running (no "/var/run/pluto/pluto.ctl") Checking for 'ip' command [OK] Checking /bin/sh is not /bin/dash [WARNING] Checking for 'iptables' command [OK] Opportunistic Encryption Support [DISABLED]
这是因为你还没有启动ipsec服务!
/etc/init.d/ipsec start
Checking your system to see if IPsec got installed and started correctly:
Version check and ipsec on-path [OK]
Linux Openswan U2.6.38/K2.6.32-38-generic-pae (netkey)
Checking for IPsec support in kernel [OK]
SAref kernel support [N/A]
NETKEY: Testing XFRM related proc values [FAILED]
Please disable /proc/sys/net/ipv4/conf/*/send_redirects
or NETKEY will cause the sending of bogus ICMP redirects!
[FAILED]
Please disable /proc/sys/net/ipv4/conf/*/accept_redirects
or NETKEY will accept bogus ICMP redirects!
[OK]
Checking that pluto is running [OK]
Pluto listening for IKE on udp 500 [OK]
Pluto listening for NAT-T on udp 4500 [OK]
Checking for 'ip' command [OK]
Checking /bin/sh is not /bin/dash [WARNING]
Checking for 'iptables' command [OK]
Opportunistic Encryption Support [DISABLED]
你可能忘记修改网络策略:
for each in /proc/sys/net/ipv4/conf/* do echo 0 > $each/accept_redirects echo 0 > $each/send_redirects done
Pluto listening for IKE on udp 500 [FAILED] Cannot execute command "lsof -i UDP:500": No such file or directory Pluto listening for NAT-T on udp 4500 [FAILED] Cannot execute command "lsof -i UDP:4500": No such file or directory
那么你需要安装lsof:
apt-get install lsof
编辑/etc/ppp/chap-secrets文件,加入如下行:
# Secrets for authentication using CHAP # client server secret IP addresses guest d password *
该行创建一个用户,用户名为guest,密码为password(明文)
server字段与options.xd配置文件中的name属性对应。还可以星号(*)代替,表示不限调用服务。
IP address表示来源IP,这里星号(*)表示接受所有连接。
对来源IP的过滤应该由iptables防火墙来完成。读者可以查阅本博客关于iptables的相关文章进行设置。
现在就可以尝试连接,如果连接失败,需要通过查看日志信息排错。
ipsec日志记录在/var/log/auth中,如果发现该行:
STATE_MAIN_R3: sent MR3, ISAKMP SA established
则表示IPSec认证或连接没有问题,可以检查其他日志。但是有时候即使出现established,也会在稍后的行中出现错误信息,这是需要用户留意的地方。
xd和pppd的日志记录在/var/log/syslog中。