L2TP/IPsec is a secure Virtual Private Network solution that is well supported on many different platforms.

This article aims to describe in a HOWTO like fashion how to configure and use a L2TP/IPsec client on Arch Linux. This article will cover the installation and setup of several software packages. One of the packages is only available in the AUR, so knowledge of how to build and install AUR packages on your system is required, as I will not cover how to do that.

This guide is primarly for clients connecting to a Windows Server machine. It uses some setting that are specific to the Microsoft implementation of L2TP/IPsec.

This file contains the basic information to establish a secure IPsec tunnel to the VPN server. It enables NAT Traversal for if your machine is behind a NAT'ing router (most people are), and various other options that are necessary to connect correctly to the remote IPsec server. The next file contains your pre-shared key (PSK) for the server.

Create the file /etc/ipsec.secrets: It should contain the following line:

192.168.1.101 68.68.32.79 : PSK "your_pre_shared_key"

Remember to replace the local (192.168.1.101) and remote (68.68.32.79) IP addresses with the correct numbers for your location. The pre-shared key will be supplied by the VPN provider and will need to be placed in this file in cleartext form.

At this point the IPsec configuration is complete and we can move on to the L2TP configuration.

xl2tpd

This file configures xl2tpd with the connection name, server IP address(which again, please remember to change to your servers address) and various options that will be passed to pppd once the tunnel is set up.

At this point the tunnel is up and you should be able to see the interface for it if you type:

$ ip link

You should see a pppX device that represents the tunnel. Right now, nothing is going to get routed through it. You need to add some routing rules to make it work right:

Routing

Routing traffic to a single IP address through the tunnel

This is as easy as adding a routing rule to your kernel table:

#route add xxx.xxx.xxx.xxx gw yyy.yyy.yyy.yyy eth0

Replace xxx.xxx.xxx.xxx with the specific ip address of the server that you wish to communicate with through the tunnel, then replace yyy.yyy.yyy.yyy with the remote IP your PPP connection. The remote IP of a PPP connection can be discovered by issuing:

#ifconfig

and reading the P-t-P address for the PPP interface that corresponds to your tunnel.

Routing all traffic through the tunnel

This is a lot more complex, but all your traffic will travel through the tunnel. Start by adding a special route for the actual VPN server through your current gateway:

#route add 68.68.32.79 gw 192.168.1.1 eth0

This will ensure that once the default gateway is changed to the ppp interface that your network stack can still find the VPN server by routing around the tunnel. If you miss this step you will lose connectivity to the Internet and the tunnel will collapse. Now add a default route that routes to the PPP remote end:

#route add default gw yyy.yyy.yyy.yyy eth0

The remote PPP end can be discovered by following the step in the previous section. Now to ensure that ALL traffic is routing through the tunnel, delete the original default route:

#route delete default gw 192.168.1.1 eth0

To restore your system to the previous state, you can reboot or reverse all of the above steps.

Troubleshooting

Issue: I get a message from pppd saying "Failed to authenticate ourselves to peer" and I've verified my password is correct. What could be wrong?

then you are authenticating against a SonicWALL LNS that does not know how to handle CHAP-style authentication correctly.

The solution to this is to add the following to your options.l2tp.client file:

refuse-chap

This will cause the SonicWALL to default to the next authentication mechanism, namely MSCHAP-v2. This should authenticate successfully, and from this point xl2tpd should successfully construct a tunnel between you and the remote L2TP server.

Tips and Tricks

Script start up and shut down

You can create some scripts either in your home directory or elsewhere(remember where you put them) to bring up the tunnel then shut it back down.

A further script

Above script really help me work. And notice the script use fixed ip, and someone like me may change net vpn addr, i'd like to put my further script below(not sure how to add attachment, so just raw ):