CentOSサーバで簡単にL2TP VPNを設定するスクリプトを作成した。
このL2TP VPNサーバを使って、MacやiPhoneなどと接続し、セキュアな通信が可能だとなる。
- サーバOS: CentOS7
1. 以下のスクリプトをサーバ上で実行する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
#!/bin/bash -x # Description:L2TP/IPsec for CentOS7 64bit # 2018/05/03 @pattyhama ( ## setting cat << _SECRETS_ > /tmp/SECRETS_TMP.txt #============================================== # username auth_server password auth_ipaddress "<ユーザ名(1)>" "xl2tpd" "<パスワード>" * "<ユーザ名(2)>" "xl2tpd" "<パスワード>" * #============================================== _SECRETS_ PSK_SECRETS='<シークレット>' COLOR_LIGHT_GREEN='\033[1;32m' COLOR_LIGHT_BLUE='\033[1;34m' COLOR_YELLOW='\033[1;33m' COLOR_RED='\033[0;31m' COLOR_WHITE='\033[1;37m' COLOR_DEFAULT='\033[0m' IPADDR_GLOBAL=$(/sbin/ip addr show eth0 2>/dev/null | /bin/grep 'inet ' | /bin/sed -e 's/.*inet \([^ ]*\)\/.*/\1/') # サーバの内部IPアドレス(クライアント向きのIP) VPN_LOCAL_IPADDRESS='10.0.0.2' # クライアントに払い出す内部IPアドレスの範囲 VPN_REMOTE_IPADDRESS='10.0.0.151-200' ## リポジトリ追加:EPEL yum install -y epel-release ## パッケージ追加 yum update -y yum install -y xl2tpd-1.3.8-3.el7 libreswan-3.20-5.el7_4 lsof-4.87-4.el7 ## L2TP セットアップ sed -i.org -e "s/; listen-addr.*/listen-addr = ${IPADDR_GLOBAL}/g" -e "s/ip range.*/ip range = ${VPN_REMOTE_IPADDRESS}/g" -e "s/local ip.*/local ip = ${VPN_LOCAL_IPADDRESS}/g" /etc/xl2tpd/xl2tpd.conf # crtscts, lockはコメントアウトする。でないと接続できない sed -i.org -e "s/^ms-dns/# ms-dns/g" -e "s/^noccp/# noccp/g" -e "s/^mtu/# mtu/g" -e "s/^mru/# mru/g" -e "s/^crtscts/# crtscts/g" -e "s/^lock/# lock/g" -e "s/^name/# name/g" /etc/ppp/options.xl2tpd cat << _XL2TPDCONF_ >> /etc/ppp/options.xl2tpd # google dns ms-dns 8.8.8.8 ms-dns 8.8.4.4 name xl2tpd refuse-pap refuse-chap refuse-mschap require-mschap-v2 persist logfile /var/log/xl2tpd.log _XL2TPDCONF_ # 注意:以下を設定しないと接続できない # mtu, mruのデフォルトのサイズ1401から修正する mtu 1200 mru 1200 # nameを"/etc/ppp/chap-secrets"に合わせ修正する name xl2tpd ## IPsec セットアップ sed -i.org -e "s/^#include/include/g" /etc/ipsec.conf cat << _IPSECCONF_ > /etc/ipsec.d/l2tp-ipsec.conf conn L2TP-PSK-NAT rightsubnet=0.0.0.0/0 dpddelay=10 dpdtimeout=20 dpdaction=clear forceencaps=yes also=L2TP-PSK-noNAT conn L2TP-PSK-noNAT authby=secret pfs=no auto=add keyingtries=3 rekey=no ikelifetime=8h keylife=1h type=transport left=${IPADDR_GLOBAL} leftprotoport=17/1701 right=%any rightprotoport=17/%any _IPSECCONF_ cat /tmp/SECRETS_TMP.txt >> /etc/ppp/chap-secrets rm /tmp/SECRETS_TMP.txt echo -e ": PSK \"${PSK_SECRETS}\"" > /etc/ipsec.d/default.secrets ## firewalld セットアップ firewall-cmd --permanent --add-service=ipsec firewall-cmd --permanent --add-port=1701/udp firewall-cmd --permanent --add-port=4500/udp firewall-cmd --permanent --add-masquerade firewall-cmd --reload ## IP_FORWARD 設定 cat << _SYSCTLCONF_ > /etc/sysctl.d/60-sysctl_ipsec.conf net.ipv4.ip_forward = 1 net.ipv4.conf.all.accept_redirects = 0 net.ipv4.conf.all.rp_filter = 0 net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.accept_redirects = 0 net.ipv4.conf.default.rp_filter = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.conf.eth0.accept_redirects = 0 net.ipv4.conf.eth0.rp_filter = 0 net.ipv4.conf.eth0.send_redirects = 0 net.ipv4.conf.eth1.accept_redirects = 0 net.ipv4.conf.eth1.rp_filter = 0 net.ipv4.conf.eth1.send_redirects = 0 net.ipv4.conf.eth2.accept_redirects = 0 net.ipv4.conf.eth2.rp_filter = 0 net.ipv4.conf.eth2.send_redirects = 0 net.ipv4.conf.ip_vti0.accept_redirects = 0 net.ipv4.conf.ip_vti0.rp_filter = 0 net.ipv4.conf.ip_vti0.send_redirects = 0 net.ipv4.conf.lo.accept_redirects = 0 net.ipv4.conf.lo.rp_filter = 0 net.ipv4.conf.lo.send_redirects = 0 net.ipv4.conf.ppp0.accept_redirects = 0 net.ipv4.conf.ppp0.rp_filter = 0 net.ipv4.conf.ppp0.send_redirects = 0 _SYSCTLCONF_ systemctl restart network ## プロセス起動 systemctl enable ipsec systemctl enable xl2tpd systemctl restart ipsec systemctl restart xl2tpd ## Finish echo -e "${COLOR_WHITE}L2TP/IPsec SERVER IP : ${COLOR_LIGHT_GREEN}${IPADDR_GLOBAL}${COLOR_DEFAULT}" echo -e "${COLOR_WHITE}L2TP/IPsec USER/PASSWORD : \n${COLOR_LIGHT_GREEN}$(/bin/cat /etc/ppp/chap-secrets)${COLOR_DEFAULT}" echo -e "${COLOR_WHITE}L2TP/IPsec PSK SECRETS : ${COLOR_LIGHT_GREEN}${PSK_SECRETS}${COLOR_DEFAULT}" echo -e "${COLOR_WHITE}Install log : ${COLOR_LIGHT_GREEN}/var/log/L2TP_IPsec-installer.log${COLOR_DEFAULT}" ) 2>&1 | tee /var/log/L2TP_IPsec-installer.log |
2. サーバのFWのインバウンドで以下を許可する
udp:1701; udp:500; udp:4500
3. MacやiPhoneでこのサーバとVPN接続出来ることを確認する