大部分情况下,pptp协议的VPN服务已经不被支持了(例如iOS下不支持pptp),所以我搭建的VPN用的是L2TP协议的。
操作系统版本:Ubuntu 14.04 LTS
运行环境:LAMP1.5
一、前期工作
1.安装LAMP搭建Web容器服务
wget -c http://soft.vpser.net/lnmp/lnmp1.5.tar.gz && tar zxf lnmp1.5.tar.gz && cd lnmp1.5 && ./install.sh lnmp
如需要安装LNMPA或LAMP,将./install.sh 后面的参数lnmp替换为lnmpa或lamp即可
2.确保完整安装L2TP/IPSec服务,并能正常连接
具体配置L2TP链接如何在Ubuntu下配置L2TP VPN
确认 IPSec 是否工作正常
ipsec verify
配置IPSec共享密钥:
vim /etc/ipsec.secrets
加入
公网IP %any: PSK "somegoodpassword"
启动 IPSec 服务:
/etc/init.d/ipsec start
配置xl2tpd密钥:
vim /etc/xl2tpd/l2tp-secrets
加入
* * exampleforchallengestring
xl2tpd配置文件:
/etc/ppp/options.xl2tpd
配置xl2tpd用户:
vim /etc/ppp/chap-secrets加入
user1 l2tpd chooseagoodpassword * user2 * chooseagoodpassword *
启动xl2tpd:
/etc/init.d/xl2tpd start故障排除分析日志:
<!-- 认证日志 --> tail -f /var/log/auth.log
<!-- 启动及运行时日志 --> tail -f /var/log/syslog
二、部署FreeRadius服务器
1.安装FreeRadius Server服务
apt-get install mysql freeradius freeradius-mysql freeradius-utils
添加测试用户
vim /etc/freeradius/users
Crayon Cleartext-Password := "123456" Reply-Message = "Hello, %{User-Name}"
使用调试模式运行FreeRadius服务器:
freeradius -X
出现以下信息代表运行成功
Listening on authentication address xx.xx.xx.xx port 1812 Listening on accounting address * port 1813 Listening on authentication address 127.0.0.1 port 18120 as server inner-tunnel Listening on proxy address xx.xx.xx.xx port 1814 Ready to process requests.
打开另一个终端,使用我们在上面添加的测试用户测试是否能连接FreeRadius服务器
radtest 用户名 用户口令 服务器IP地址 端口 服务器通信密钥 //在本例子中,我们使用以下命令 radtest Crayon 123456 xxx.xxx.xxx.xxx 1812 testing123
出现以下信息Access-Accept代表连接成功
rad_recv: Access-Accept packet from host...
若出现连接失败等情况:client no response...或Listen to the unknown client...,即FreeRadius无法确认来自客户端的请求来源或FreeRadius通信密钥不正确, 此时应对clients.conf进行配置,添加服务器的公网IP及其通信密钥(若使用MySQL的nas表来加载client,则以下配置可以忽略):
vim /etc/freeradius/clients.conf //添加 client crayon-vpn { ipaddr = xxx.xxx.xxx.xxx #填写公网IP secret = testing123 }
并在host文件中配置对应的IP和主机名
vim /etc/hosts //添加 xxx.xxx.xxx.xxx crayon-vpn #填写公网IP
2.安装RreeRadius Client服务
cd ~ wget -c ftp://ftp.freeradius.org/pub/freeradius/freeradius-client-1.1.7.tar.gz tar -zxvf freeradius-client-1.1.7.tar-gz cd freeradius-client-1.1.7 ./configure make && make install
注释掉radiusclient.conf文件下的radius_deadtime、bindaddr属性
vim /usr/local/etc/radiusclient/radiusclient.conf
... #radius_deadtime 0 ... #bindaddr * ...
在radiusclient.conf文件中配置freeradius相关信息
vim /usr/local/etc/radiusclient/radiusclient.conf #指定freeRadius服务器的IP,将SERVER_IP改为你的服务器IP地址: authserver SERVER_IP acctserver SERVER_IP #指定连接到服务器的配置文件目录: servers /usr/local/etc/radiusclient/servers
注释掉dictionary文件里有关ipv6的行
sed -i '/ipv6/s/^/#/' /usr/local/etc/radiusclient/dictionary
修改servers配置文件
vim /usr/local/etc/radiusclient/servers
加入
xxx.xxx.xxx.xxx testing123 #对应公网IP和密钥,之前FreeRadius已配置成testing123
增加客户端字典
vim /usr/local/etc/radiusclient/servers
加入
INCLUDE /usr/local/etc/radiusclient/dictionary.sip INCLUDE /usr/local/etc/radiusclient/dictionary.ascend INCLUDE /usr/local/etc/radiusclient/dictionary.merit INCLUDE /usr/local/etc/radiusclient/dictionary.compat INCLUDE /usr/local/etc/radiusclient/dictionary.microsoft
若/usr/local/etc/radiusclient/dictionary.microsoft不存在,则下载并移动到该目录下
wget -c http://small-script.googlecode.com/files/dictionary.microsoft mv ./dictionary.microsoft /usr/local/etc/radiusclient/dictionary.microsoft
三、将FreeRadius与MySQL整合
1.配置freeRadius核心配置文件radiusd.conf
vim /etc/freeradius/radiusd.conf
将$INCLUDE sql.conf进行反注释,去掉#
2. 修改配置文件sql.conf
vim /etc/freeradius/sql.conf
配置文件包含了需要访问的数据库的用户名和密码、服务器的IP地址,以及各种默认的表的信息。
修改sql配置项下的database、server、login及password属性值为当前服务器MySQL的登录信息,并反注释掉readclients = yes和nas_table = "nas"属性
sql { ... database = "mysql" #设置为mysql ... driver = "rlm_sql_${database}" # Connection info: server = "localhost" #port = 3306 login = "root" #填写MySQL登录用户名 password = "123456" #填写MySQL登录口令 # Database table configuration for everything except Oracle radius_db = "radius" ... readclients = yes #反注释 nas_table = "nas" #反注释,代表使用MySQL的nas表来加载client配置,不在/etc/freeradius/clients.conf上加载 ... }
3.修改FreeRadius的default文件,开启AAA功能
vim /etc/freeradius/sites-available/default找到authorize {}模块,注释掉files,去掉sql前的#号。
找到preacct {}模块,注释掉files。
找到accounting {}模块,注释掉radutmp,去掉sql前面的#号。
找到session {}模块,注释掉radutmp,去掉sql前面的#号。
找到post-auth {}模块,去掉两行sql前的#号
vim /usr/local/etc/freeradius/sites-enabled/inner-tunnel找到authorize {}模块,注释掉files,去掉sql前的#号。
找到session {}模块,注释掉radutmp,去掉sql前面的#号。
找到post-auth {}模块,去掉 两行sql前的#号
4.创建Radius数据库
mysql -u root -p mysql> create database radius; mysql> use radius;
导入必要的表
mysql> source /etc/freeradius/sql/mysql/admin.sql mysql> source /etc/freeradius/sql/mysql/ippool.sql mysql> source /etc/freeradius/sql/mysql/cui.sql mysql> source /etc/freeradius/sql/mysql/nas.sql mysql> source /etc/freeradius/sql/mysql/wimax.sql mysql> source /etc/freeradius/sql/mysql/schema.sql
导入后查看表信息
mysql>show tables;
5.添加nas记录来加载client
INSERT INTO nas (nasname,shortname,type,ports,secret) VALUES('公网IP','公网IP','other',1812,'testing123');
6.使用MySQL为FreeRadius添加用户组及其组的响应属性
INSERT INTO radgroupreply (groupname,attribute,op,value) VALUES ('user','Auth-Type',':=','Local'); INSERT INTO radgroupreply (groupname,attribute,op,value) VALUES ('user','Service-Type',':=','Framed-User'); INSERT INTO radgroupreply (groupname,attribute,op,value) VALUES ('user','Framed-IP-Address',':=','255.255.255.255'); INSERT INTO radgroupreply (groupname,attribute,op,value) VALUES ('user','Framed-IP-Netmask',':=','255.255.255.0');
7.使用MySQL为FreeRadius添加用户,并设置其用户组
INSERT INTO radcheck (username,attribute,op,value) VALUES ('crayon','Cleartext-Password',':=','123456');
将crayon关联至user组
INSERT INTO radusergroup (username,groupname) VALUES ('crayon','user');
8.进行MySQL与FreeRadius的远程测试
radtest crayon 123456 xxx.xxx.xxx.xxx 1812 testing123 #xxx.xxx.xxx.xxx为公网IP
返回响应Access-Accept
Sending Access-Request of id 110 to xxx.xxx.xxx.xxx port 1812 User-Name = "crayon" User-Password = "123456" NAS-IP-Address = xxx.xxx.xxx.xxx NAS-Port = 1812 Message-Authenticator = 0x00000000000000000000000000000000 rad_recv: Access-Accept packet from host xxx.xxx.xxx.xxx port 1812, id=110, length=44 Service-Type = Framed-User Framed-IP-Address = 255.255.255.255 Framed-IP-Netmask = 255.255.255.0
以上结果证明,FreeRadius与MySQL集成成功!
目前我们已经安装配置好了FreeRadius Server、FreeRadius Client,并将FreeRadius与MySQL进行关联绑定,使用MySQL对FreeRadius进行管理。
四、将FreeRadius与L2TP整合
修改options.xl2tpd配置文件
vim /etc/ppp/options.xl2tp
行尾加入
plugin /usr/lib/pppd/2.4.5/radius.so radius-config-file /usr/local/etc/radiusclient/radiusclient.conf
重启xl2tpd
/etc/init.d/xl2tpd restart
启动FreeRadius
/etc/init.d/freeradius start
若无法启动,可能是之前开启了调试模式没有关闭,使用ps -ef |grep radius和kill -9关闭相应进程即可。
教程到这里已经完成了,现在FreeRadius和MySQL已经集成整合,并有MySQL来管理FreeRadius的配置和用户。而FreeRadius和L2TP也整合完成,使用L2TP进行连接将会调用FreeRadius进行用户认证,并实现FreeRadius+MySQL管理L2TP用户。
如何使用FreeRadius来限制用户使用VPN流量,Ubuntu下配置FreeRadius+L2TP+MySQL,实现限制用户流量和登录人数
如何使用daloRadius来实现web管理VPN,Ubuntu下配置FreeRadius+MySQL+xl2tpd + daloRadius,实现L2TP由web管理
参考资料:
https://linux.cn/article-3409-1.html
https://blog.csdn.net/wuwentao2000/article/details/44807759
http://blog.51cto.com/spencergra/1922113