一键设置L2TP脚本-Ubuntu14.04LTS

2023-11-20

亲测在VultrUltraVPSUbuntu 14.04 LTS成功搭建L2TPVPN

本方法使用Linux自带的账户认证作为L2TP的认证。用户名默认为vpn_user,密码在脚本执行过程中,由执行者手动设定密码;PSKpsk,开机自动启动。

本脚本必须使用root账户执行。

#!/bin/bash
# Referring from https://raymii.org/s/tutorials/IPSEC_L2TP_vpn_with_Ubuntu_14.04.html

if [[ $EUID -ne 0 ]]; then
    echo 'Error:This script must be run as root!'
    exit 1
fi

apt-get install -y openswan xl2tpd ppp lsof vim rng-tools curl
SERVERIP=`curl -s -4 icanhazip.com`
iptables -t nat -A POSTROUTING -j SNAT --to-source $SERVERIP -o eth0

echo 'net.ipv4.ip_forward = 1' |  tee -a /etc/sysctl.conf
echo 'net.ipv4.conf.all.accept_redirects = 0' |  tee -a /etc/sysctl.conf
echo 'net.ipv4.conf.all.send_redirects = 0' |  tee -a /etc/sysctl.conf
echo 'net.ipv4.conf.default.rp_filter = 0' |  tee -a /etc/sysctl.conf
echo 'net.ipv4.conf.default.accept_source_route = 0' |  tee -a /etc/sysctl.conf
echo 'net.ipv4.conf.default.send_redirects = 0' |  tee -a /etc/sysctl.conf
echo 'net.ipv4.icmp_ignore_bogus_error_responses = 1' |  tee -a /etc/sysctl.conf

for vpn in /proc/sys/net/ipv4/conf/*; do echo 0 > $vpn/accept_redirects; echo 0 > $vpn/send_redirects; done
sysctl -p

# start VPN onstart
cp /etc/rc.local{,.bak}
sed -i '/exit 0/d' /etc/rc.local
cat >> /etc/rc.local<<EOF
for vpn in /proc/sys/net/ipv4/conf/*; do echo 0 > $vpn/accept_redirects; echo 0 > $vpn/send_redirects; done
iptables -t nat -A POSTROUTING -j SNAT --to-source $SERVERIP -o eth0

exit 0
EOF

# config ipsec
cp /etc/ipsec.conf{,.bak}
cat >/etc/ipsec.conf <<EOF
version 2 # conforms to second version of ipsec.conf specification

config setup
    dumpdir=/var/run/pluto/
    #in what directory should things started by setup (notably the Pluto daemon) be allowed to dump core?

    nat_traversal=yes
    #whether to accept/offer to support NAT (NAPT, also known as 'IP Masqurade') workaround for IPsec

    virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v6:fd00::/8,%v6:fe80::/10
    #contains the networks that are allowed as subnet= for the remote client. In other words, the address ranges that may live behind a NAT router through which a client connects.

    protostack=netkey
    #decide which protocol stack is going to be used.

    force_keepalive=yes
    keep_alive=60
    # Send a keep-alive packet every 60 seconds.

conn L2TP-PSK-noNAT
    authby=secret
    #shared secret. Use rsasig for certificates.

    pfs=no
    #Disable pfs

    auto=add
    #the ipsec tunnel should be started and routes created when the ipsec daemon itself starts.

    keyingtries=3
    #Only negotiate a conn. 3 times.

    ikelifetime=8h
    keylife=1h

    ike=aes256-sha1,aes128-sha1,3des-sha1
    phase2alg=aes256-sha1,aes128-sha1,3des-sha1
    # https://lists.openswan.org/pipermail/users/2014-April/022947.html
    # specifies the phase 1 encryption scheme, the hashing algorithm, and the diffie-hellman group. The modp1024 is for Diffie-Hellman 2. Why 'modp' instead of dh? DH2 is a 1028 bit encryption algorithm that modulo's a prime number, e.g. modp1028. See RFC 5114 for details or the wiki page on diffie hellmann, if interested.

    type=transport
    #because we use l2tp as tunnel protocol

    left=$SERVERIP
    #fill in server IP above

    leftprotoport=17/1701
    right=%any
    rightprotoport=17/%any

    dpddelay=10
    # Dead Peer Dectection (RFC 3706) keepalives delay
    dpdtimeout=20
    #  length of time (in seconds) we will idle without hearing either an R_U_THERE poll from our peer, or an R_U_THERE_ACK reply.
    dpdaction=clear
    # When a DPD enabled peer is declared dead, what action should be taken. clear means the eroute and SA with both be cleared.
EOF

#config ipsec.secrets
cp /etc/ipsec.secrets{,.bak}
cat >/etc/ipsec.secrets <<EOF
$SERVERIP  %any:   PSK 'psk' #<<<<<<<<<<PSK>>>>>>>>>>#
EOF

ipsec verify

cp /etc/xl2tpd/xl2tpd.conf{,.bak}
cat > /etc/xl2tpd/xl2tpd.conf <<EOF
[global]
ipsec saref = yes
saref refinfo = 30

;debug avp = yes
;debug network = yes
;debug state = yes
;debug tunnel = yes

[lns default]
ip range = 172.16.1.30-172.16.1.100
local ip = 172.16.1.1
require authentication = yes
unix authentication = yes
;ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes
EOF

# config options.xl2tpd
cp /etc/ppp/options.xl2tpd{,.bak}
cat >/etc/ppp/options.xl2tpd <<EOF
ms-dns 8.8.8.8
ms-dns 8.8.4.4
auth
mtu 1200
mru 1000
crtscts
hide-password
modem
name l2tpd
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
login
EOF

#config pam.d
cp  /etc/pam.d/ppp{,.bak}
sed -i '/^auth/c\
auth    required        pam_nologin.so\
auth    required        pam_unix.so\
account required        pam_unix.so\
session required        pam_unix.so
' /etc/pam.d/ppp

#config pap-secrets
cp /etc/ppp/pap-secrets{,.bak}
cat >>/etc/ppp/pap-secrets <<EOF
*       l2tpd           ''              *
EOF

useradd vpn_user
echo -n 'set password for [vpn_user]:'
passwd vpn_user
/etc/init.d/ipsec restart
/etc/init.d/xl2tpd restart

echo 'IP:' $SERVERIP
echo 'UserName(set by useradd vpn_user): vpn_user'
echo 'PSK(set by /etc/ipsec.secrets): psk'

exit 0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

一键设置L2TP脚本-Ubuntu14.04LTS 的相关文章

  • 在 bash 函数中生成后台进程

    我正在编写一个 Bash 函数来启动需要从某个文件夹启动的服务器 但我不希望启动该服务器影响我当前的工作 我写了以下内容 function startsrv pushd cd TRUNK SERVERCOMMAND popd 我的变量都已设
  • 模拟用户输入以使用不同参数多次调用脚本

    我必须使用提供的脚本 该脚本在脚本运行时接受用户输入而不是参数 我无法解决这个问题 脚本的一个例子是 bin bash echo param one read one doSomething echo param two read two
  • 快速 shell 命令删除文本文件中的停用词

    我有一个 2GB 的文本文件 我正在尝试从此文件中删除经常出现的英语停用词 我有 stopwords txt 包含这样的 a an the for and I 使用 shell 命令 例如 tr sed 或 awk 执行此操作的快速方法是什
  • 如何替换每行中出现的所有字符串?

    我想在以下脚本中将所有出现的 用户名 替换为 但它仅替换第一次出现的情况 ls al sed s username 这就是 sed 默认情况下的工作方式吗 提前致谢 你需要g 全局 修饰符 sed s username g
  • 代码::块 - 警告:GDB:无法设置控制终端:不允许操作

    我已经通过官方存储库在 Ubuntu 14 04 中安装了 Code Blocks 13 12 当我编译时 一切正常 但是当我调试时 shell 中会显示以下消息 警告 GDB 无法设置控制终端 操作不正确 允许的 程序执行到断点 但当我执
  • 寻找下一个开放端口

    有没有什么办法 使用基本的 Unix 命令 找到下一个未使用的端口号 从端口 4444 开始向上 我通过 ssh 通过 openssh 进入 Windows XP 计算机 运行 Cygwin 工具并使用 bash shell 谢谢 戴夫 尝
  • 在 XAMPP 上设置虚拟主机

    我已经在 Ubuntu 上的 opt lampp 目录中安装了 XAMPP 并且想要设置一些虚拟主机 Apache 虚拟主机教程说明放置
  • bash双括号问题

    我对 bash 脚本非常陌生 在使用双括号时遇到了问题 我似乎无法让它们在 Ubuntu Server 11 10 中工作 我的下面的脚本位于 if test sh 中 bin bash if 14 14 then echo FOO fi
  • 如何将文件中的值分配给 UNIX sh shell 中的变量?

    我一直在搜索这个网站 试图找到这个问题的答案 并发现了几个非常好的答案 不幸的是 它们都不适合我 这是我正在使用的脚本 VALUE cat szpfxct tmp export VALUE echo gt gt LGFILE echo te
  • 使用 sed 将 old-link-url 替换为 new-link-url

    我正在 bash 中编写一个脚本 将 old link url 替换为 new link url 我的问题是 sed 由于斜杠而无法替换 url 如果我只输入一些文字就可以了 my code sed e s old link new lin
  • 在 Ubuntu 上纯粹通过 bash 脚本安装 mysql 5.7

    我想要一个无需任何手动输入即可安装 MySQL 5 7 实例的 bash 脚本 我正在关注数字海洋教程 https www digitalocean com community tutorials how to install mysql
  • 在 Ubuntu 16.04 中创建虚拟主机

    我已经开始在 laravel 中工作并使用 lampp 我看过很多使用虚拟主机来制作用户友好的 url 的教程 我想在 Ubuntu 16 04 上执行此操作 以下教程对我不起作用 https ourcodeworld com articl
  • 使用脚本自动输入 SSH 密码

    我需要创建一个自动向 OpenSSH 输入密码的脚本ssh client 假设我需要通过 SSH 进入myname somehost用密码a1234b 我已经尝试过 bin myssh sh ssh myname somehost a123
  • 我如何公开我的IP,外部可以访问我的本地主机

    我只是想让我的IP公开 这样就可以从任何地方访问它 我正在使用ubuntu 18 04 已经安装了apache2和PHP 索引文件位于 var www html example com public html index php 在本地主机
  • Bash 脚本 - 迭代 find 的输出

    我有一个 bash 脚本 其中需要迭代 find 命令输出的每一行 但似乎我正在迭代 find 命令中的每个单词 以空格分隔 到目前为止我的脚本看起来像这样 folders find maxdepth 1 type d for i in f
  • 在 shell 脚本中查找和替换

    是否可以使用 shell 在文件中搜索然后替换值 当我安装服务时 我希望能够在配置文件中搜索变量 然后在该值中替换 插入我自己的设置 当然 您可以使用 sed 或 awk 来完成此操作 sed 示例 sed i s Andrew James
  • 在powershell中检查文件是否可读且正常

    我是 powershell 新手 我想检查文件是否可读且正常 在 unix 中 我们可以使用 f 和 r 在一行中完成此操作 例如 以下 shell 脚本函数接受文件名作为参数并检查文件的可读性和规律性 与此等效的 powershell 是
  • Ubuntu 16.04 - Apache 2.4.18 - 请求 URI 太长

    我试图从 Google 图片搜索结果中保存图像 但是当我在查询字符串参数中发送图像的 src 时 会出现以下错误 Request URI Too Long The requested URL s length exceeds the cap
  • saber sd 如何在没有 SPL 的情况下直接从 uboot 启动

    sabre sd 基于 imx 6 最大内部 RAM 约为 150Kb 然而 uboot 足够大 可以容纳在这个空间中 在这个场景中事情是如何进行的 https community freescale com docs DOC 95015
  • 如何在 Xcode 4.2 中创建 Shell 脚本目标?

    我想知道是否有人知道 XCode 4 1 如何创建 shell 脚本 该选项不存在 但在最新版本中可能会被称为其他名称 塔 在导航器中选择您的项目 单击 添加目标 为空目标选择 聚合 添加构建阶段 gt 添加运行脚本 单击构建阶段并编辑运行

随机推荐