ubuntu环境下系统开发
1.vmware安装
1.1网络配置:
1)win8通过ADSL PPPoE拨号上网,(通过用户名及密码拨号完成)
2)在控制面板的网络连接里进入-〉宽带连接-〉属性-〉共享,internel连接共享
3)选用VMware Network Adapter VMnet8作为双网卡共享代理, 配置ip地址及DNS;
ipaddr=192.168.137.1
netmask=255.255.255.0
gateway=192.168.137.2
DNS可以根据PPPoE拨号得到的结果设置,具体用cmd〉ipconfig -all查看
4)在vmwre中进入edit-〉virtual network editor选择VMnet8(跟win8上的网络接口一致)配置为NAT模式
subnet IP=192.168.137.0,netmask=255.255.255.0
进入NAT Setting配置gateway=192.168.137.2
5)宽带进行重新拨号,以重新生成win8刚才的配置。
6)进入ubuntu系统,配置网络接口通过dhcp方式获得地址
7)一切ok后,网络就可以通了,可以ping测试,或web上网。
2.ubuntu安装
3.配置管理
3.1系统配置
3.1.1 /etc/environment
系统环境变量管理,用户的环境变量配置在这里。
root@ubuntu-snmp:~# cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
3.1.2 /etc/profile
此文件为系统为每个用户设置环境信息,当用户第一次登录时,该文件被执行. 并从/etc/profile.d目录的配置文件中搜集shell的设置。
3.1.3 /etc/hosts
hosts文件是Linux系统上一个负责ip地址与域名快速解析的文件;hosts文件包含了ip地址与主机名之间的映射,还包括主机的别名。优先级:dns缓存 > hosts > dns服务。
root@ubuntu-snmp:/home/chenchao# cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 ubuntu-snmp
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
3.1.4 当前工作用户配置
在当前用户的工作目录(~/)下,用户登入时执行的配置文件,主要有以下:
~/.bashrc
~/.profile
~/.bash_logout
~/.bash_history
3.2网络配置
3.2.1 /etc/network/interfaces
所有的网络接口参数都在这里配置。
1)基本的接口配置如下:
root@ubuntu-snmp:/home/chenchao# cat /etc/network/interfaces
# The loopback network interface
auto lo //系统启动时自动配置该接口
iface lo inet loopback //将该接口设置成一个环回接口
# The primary network interface
auto eth0 //系统启动时自动配置该接口
iface eth0 inet static //将该接口配置成静态IP
address 192.168.100.189
netmask 255.255.255.0
gateway 192.168.100.1
dns-nameservers 192.168.100.1
2)桥接路由配置如下:
root@ac:/home/admin# cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth5
iface eth5 inet dhcp //该接口为WAN接口
iface eth0 inet manual //该接口配置为LAN接口
iface eth1 inet manual
iface eth2 inet manual
iface eth3 inet manual
iface eth4 inet manual
auto br0
iface br0 inet static
bridge_ports eth0 eth1 eth2 eth3 eth4 //配置桥接
address 192.168.100.1
broadcast 192.168.100.0
netmask 255.255.255.0
bridge_waitport 0 # no delay before a port becomes avaiable
bridge_fd 0 # no forwarding delay
路由信息查看
root@ac:/home/admin# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.10.1 0.0.0.0 UG 0 0 0 eth5
192.168.10.0 * 255.255.255.0 U 0 0 0 eth5
192.168.100.0 * 255.255.255.0 U 0 0 0 br0
结合上下文,eth5获取的IP为192.168.10.X,即WAN口使用DHCP获取网络信息。
3.2.2网络配置命令
1.配置IP地址:
#ifconfig eth0 10.10.10.10 netmask 255.255.255.0 up
2.增加一个IP地址:
#ifconfig eth0:0 192.168.1.100
#ip addr add 192.168.1.20/24 dev eth0
3.配置默认路由:
route add default gw 192.168.1.1
route del default gw 192.168.1.1
4.关闭IPv6功能:
配置文件添加:/etc/sysctl.conf
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
#sudo sysctl -p
3.3路由配置
3.3.1路由表查看
root@ac:/home/admin# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.10.1 0.0.0.0 UG 0 0 0 eth5
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth5
192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 br0
root@ac:/home/admin# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.10.1 0.0.0.0 UG 0 0 0 eth5
192.168.10.0 * 255.255.255.0 U 0 0 0 eth5
192.168.100.0 * 255.255.255.0 U 0 0 0 br0
各个字段含义:
字段 | 含义 |
---|---|
Destination | 目标网络或目标主机,0.0.0.0时为default默认网关所有的数据都发到这个网关 |
Gateway | 网关地址,0.0.0.0 表示当前记录对应的 Destination 跟本机在同一个网段,通信时不需要经过网关 |
Genmask | Destination 字段的网络掩码, 255.255.255.255为主机, 0.0.0.0是默认路由 |
Flags | U 活动路由,H 目标是个主机,G 需要经过网关,R 恢复动态路由产生的表项,! 拒绝路由 |
Metric | 路由距离,到达指定网络所需的跳数 |
Ref | 路由项引用次数 |
Use | 使用次数 |
Iface | 通过接口 |
3.3.2主机路由
路由表中指向单个 IP 地址或主机名的路由记录,其 Flags 字段标记 H。
192.168.10.22通过eth0接口出去,通过网关设备192.168.100.1转发。
root@ubuntu-snmp:/home/chenchao# route
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.10.22 192.168.100.1 255.255.255.255 UGH 0 0 0 eth0
3.3.3网络路由
主机可以到达的网络。通过网关跳转路由,Flags标记G。
192.168.10.X的IP通过eth0接口出去,通过网关192.168.100.1转发。
root@ubuntu-snmp:/home/chenchao# route
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.10.0 192.168.100.1 255.255.255.0 UG 0 0 0 eth0
3.3.4默认路由
当目标主机的 IP 地址或网络不在路由表中时,数据包就被发送到默认路由(默认网关)上。
root@ubuntu-snmp:/home/chenchao# route
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.100.1 0.0.0.0 UG 0 0 0 eth0
3.3.5路由跟踪
root@ubuntu-snmp:/home/chenchao# traceroute 192.168.10.22
traceroute to 192.168.10.22 (192.168.10.22), 30 hops max, 60 byte packets
1 192.168.100.1 (192.168.100.1) 0.500 ms 0.463 ms 0.367 ms
2 192.168.10.22 (192.168.10.22) 0.790 ms 0.935 ms 1.200 ms
3.3.6路由命令
1)添加路由:
route add -net 192.168.10.0 netmask 255.255.255.0 gw 192.168.100.1 dev eth0
2)删除路由:
route del -net 192.168.10.0 netmask 255.255.255.0
3)屏蔽路由:
route add -net 224.0.0.1 netmask 240.0.0.1 reject
4)添加默认网关:
route add default gw 192.168.100.1
5)删除默认网关:
route del default gw 192.168.100.1
6)添加路由,并指定优先级为89:
route add -net 192.168.10.0 netmask 255.255.255.0 gw 192.168.100.1 dev eth0 metric 89
3.4 软件管理
1.查看已安装的软件包
#dpkg --list
2.删除软件包:
#sudo apt-get --purge remove <包名>
3.安装软件包:
#sudo apt-get install <包名>
4.ubuntu服务安装:
4.1 ssh server安装
#apt-get install openssh-server;查看服务:ps -e |grep ssh;启动服务:需要/etc/init.d/ssh start
4.2 TFTP server安装
#sudo apt-get install xinetd;sudo apt-get install tftp tftpd。再编辑配置文件vi /etc/xinetd.d/tftp
service tftp
{
socket_type =dgram
protocol =udp
wait =yes
user =root
server =/usr/sbin/in.tftpd
server_args = -s /tftpboot
disable =no
per_source =11
cps = 100 2
flags = IPv4
}
注意配置文件格式,及在根目录下创建tftpboot目录。输入sudo /etc/init.d/xinetd restart命令,重新启动服务。此时可以用tftp 客户端进行测试。在/tftpboot目录下存入a文件,使用tftp 127.0.0.1去取文件,>get a。如果正常,将a文件下载到当前工作目录下。
4.3 samba 安装
安装,卸载操作:
#apt-get install samba samba-common //安装
#sudo apt-get autoremove samba //卸载
配置配置文件:/etc/samba/smb.conf
[chenchao]
comment = Shared Folder with username and password
path = /home/chenchao
public = no
writable = yes
valid users = chenchao
create mask = 0777
directory mask = 0777
available = yes
browseable = yes
重启服务:
#sudo /etc/init.d/samba stop
sudo /etc/init.d/samba start
4.4 PPPoE服务器安装
4.4.1.安装pppoe软件
sudo apt-get install pppoe
4.4.2.配置pppoe相关配置文件
1.配置文件 /etc/ppp/options,为使用CHAP认证方式修改以下参数
-pap
+chap
ms-dns 114.114.114.114 //DNS配置
2.配置文件/etc/ppp/chap-secrets,增加用户名密码。
# client server secret IP addresses
admin * admin *
3.配置文件/etc/ppp/pppoe-server-options,当不存在时创建。
auth
require-chap
logfile /var/log/pppoe.log
4.4.3运行PPPoE服务
使用eth0接口通信,PPPoE本地服务器IP为10.1.1.1,远端客户端IP地址为10.1.1.100开始递增,允许20个客户端接入。
sudo pppoe-server -I eth0 -L 10.1.1.1 -R 10.1.1.100 -N 20
4.4.4win7配置PPPoE接入
当成功连接后,客户端信息
服务器端信息:
ppp0 Link encap:Point-to-Point Protocol
inet addr:10.1.1.1 P-t-P:10.1.1.101 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1480 Metric:1
RX packets:103 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:7454 (7.4 KB) TX bytes:70 (70.0 B)
4.4.5 配置PPPoE业务上网
1.查看PPPoE服务器上网络配置,ip=192.168.100.189;gw=192.168.100.1,dns=192.168.10.1。
chenchao@ubuntu-snmp:~$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.100.189
netmask 255.255.255.0
gateway 192.168.100.1
dns-nameservers 192.168.10.1
2.配置iptables
由于eth0接口是可以上网的,现在增加配置为,pppoe链路(10.1.1.0/24)上的数据转发到eth0上出去。
chenchao@ubuntu-snmp:~$ sudo vim /etc/sysctl.conf //配置使能IP转发
net.ipv4.ip_forward=1
chenchao@ubuntu-snmp:~$ sudo sysctl -p
net.ipv4.ip_forward = 1
chenchao@ubuntu-snmp:~$ sudo iptables -t nat -A POSTROUTING -s 10.1.1.0/24 -o eth0 -j MASQUERADE
3.配置/etc/ppp/options下的dns为192.168.10.1,重启pppoe服务。
chenchao@ubuntu-snmp:~$sudo killall pppoe-server
chenchao@ubuntu-snmp:~$ sudo pppoe-server -I eth0 -L 10.1.1.1 -R 10.1.1.100 -N 20
4.PC拨号测试。
此时当PC拨号成功后,PC获取的网络信息为:
IP地址:10.1.1.100
IP网关:10.1.1.1(不填的话:10.1.1.100)
dns:192.168.10.1
可以ping 通www.baidu.com。
5.系统管理:
1.用户名与密码:一般Linux系统的用户名与密码管理通过配置文件完成,以下两者配置配合完成。
/etc/passwd:文件中配置管理系统的用户的执行情况;
/etc/shadow:文件中配置管理系统的用户名对应的密码,及一些执行权限等情况。
6.虚拟专用网络服务器环境搭建
6.1 PPTP服务器配置
1.安装pptpd软件,即虚拟专用网络服务器软件。
#apt-get install pptpd
2.编辑配置文件/etc/pptpd.conf:
option /etc/ppp/pptpd-options
localip 192.168.0.1
remoteip 192.168.0.234-238,192.168.0.245
3.编辑配置项文件/etc/ppp/pptpd-options:(打开DNS配置)
ms-dns 8.8.8.8
ms-dns 114.114.114.114
4.配置连接的用户名密码:/etc/ppp/chap-secrets
#user pptpd password *
admin pptpd 12345678 *
5.配置完成后,重启生效:
/etc/init.d/pptpd restart
6.检查pptpd服务是否运行正常:有以下端口服务信息证明运行正常
chenchao@ubuntu-snmp:~$ sudo netstat -anp |grep pptpd
tcp 0 0 0.0.0.0:1723 0.0.0.0:* LISTEN 20869/pptpd
unix 2 [ ] DGRAM 66961 20869/pptpd
7.防火墙查看,如果配置了防火墙,需要让1723的tcp 端口号通过。(根据自己系统确认)
iptables -L
#或添加一个规则
iptables -I INPUT -p tcp --dport 1723 -j ACCEPT
8.查看客户端连接情况:10.10.10.99为连接的客户端
chenchao@ubuntu-snmp:~$ sudo netstat -anp |grep pptpd
tcp 0 0 0.0.0.0:1723 0.0.0.0:* LISTEN 20869/pptpd
tcp 0 0 10.10.10.189:1723 10.10.10.99:60920 ESTABLISHED 21037/pptpd [10.10.
raw 0 0 10.10.10.189:47 10.10.10.99:* 1 21037/pptpd [10.10.
9.到目前为止,证明,客户端拨号到服务器的协议过程已经完成,证明搭建的服务也是正常的。但此时还不能保证对网络的后续访问是正常的。这个需要根据自己系统的网络情况进行配置。
通过客户端的情况查看:(本次是用openwrt提供的pptp进行测试)
ppp0 Link encap:Point-to-Point Protocol
inet addr:192.168.1.200 P-t-P:192.168.1.234 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1496 Metric:1
RX packets:7 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:116 (116.0 B) TX bytes:84 (84.0 B)
ppp1 Link encap:Point-to-Point Protocol
inet addr:192.168.1.200 P-t-P:192.168.1.235 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1496 Metric:1
RX packets:7 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:116 (116.0 B) TX bytes:84 (84.0 B)
此时,可以在服务器端ping通192.168.1.234/235;同样在客户端ping 192.168.1.200也是通的。对于openwrt系统,支持ssh登入,此时在服务器端ssh 192.168.1.234也可以正常访问。
10.测试:目前的测试结果为:
PPTP服务器生成的接口pppx可以通过接口分别ping通ppp客户端,这里不加指定接口也可以ping通。证明PPTP服务器有能力自动处理。
第三方标准PC,未有PPP连接,同样可以ping通PPTP服务器的IP;PPTP服务器ping出也是正常。
PPTP客户端之间ping不通。
第三方标准PC,ping PPTP客户端不通,同样反向也ping不通。
11.配置隧道(上网)
这里可以参考PPPoE的配置方法,配置IP forward及iptables。如发现不能上网,需配置好正确的dns,在/etc/ppp/pptpd-options中配置。
6.2 L2TP服务器配置
1.安装pptpd软件,即虚拟专用网络服务器软件。
#apt-get install xl2tpd ppp
2.配置xl2tpd.conf配置文件/etc/xl2tpd/xl2tpd.conf,配置文件信息如下:
[global]
ipsec saref = no
[lns default]
ip range = 10.10.10.200-10.10.10.220
local ip = 192.168.10.168
refuse chap = yes
refuse pap = yes
require authentication = yes
; name = L2TP×××
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes
主要关注ip地址,及配置文件,其他默认。
3. 配置认证方式及DNS相关信息,/etc/ppp/options.xl2tpd。不存在时创建,跟xl2tpd.conf配置参数里的配置对应。配置信息如下:
require-chap
ms-dns 114.114.114.114
lcp-echo-interval 10
lcp-echo-failure 3
auth
这里配置的是CHAP认证方式。
4.添加链接的账户信息,配置用户名密码。配置文件/etc/ppp/chap-secrets,添加信息如下:
# Secrets for authentication using CHAP
# client server secret IP addresses
username * password *
第一个参数为Clinet的用户名,第二个参数为服务内容(即PPTP,L2TP等),第三个参数为密码,第四个参数对应的IP地址。*代表所有信息。根据3描述的是配置CHAP认证方式,及配置chap-secrets;也可以指定PAP认证方式,配置pap-secrets。
5.启动xl2tpd服务:
/etc/init.d/xl2tpd start #启动
/etc/init.d/xl2tpd stop #停止
/etc/init.d/xl2tpd restart #重新启动
启动后再查看下服务是否正常启动:
chenchao@ubuntu:~$ sudo netstat -anp |grep l2tp
udp 0 0 0.0.0.0:1701 0.0.0.0:* 18040/xl2tpd
unix 2 [ ] DGRAM 2009048 18040/xl2tpd
此时证明,服务已经正常启动。
6.查看客户端连接情况:
ppp0 Link encap:Point-to-Point Protocol
inet addr:192.168.8.168 P-t-P:192.168.8.200 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:152 errors:0 dropped:0 overruns:0 frame:0
TX packets:173 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:8367 (8.3 KB) TX bytes:9509 (9.5 KB)
ppp1 Link encap:Point-to-Point Protocol
inet addr:192.168.8.168 P-t-P:192.168.8.201 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:7 errors:0 dropped:0 overruns:0 frame:0
TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:93 (93.0 B) TX bytes:99 (99.0 B)
此时可以看到有两个客户端的连接,分别是ppp0,ppp1。对应分配的IP地址为:192.168.8.200;192.168.8.201.
测试Clinet端的连通性。通过各自的接口去ping通Client端,均能够正常ping通。
chenchao@ubuntu:~$ ping -I ppp0 192.168.8.200
PING 192.168.8.200 (192.168.8.200) from 192.168.8.168 ppp0: 56(84) bytes of data.
64 bytes from 192.168.8.200: icmp_seq=1 ttl=64 time=2.27 ms
64 bytes from 192.168.8.200: icmp_seq=2 ttl=64 time=1.28 ms
^C
--- 192.168.8.200 ping statistics ---
3 packets transmitted, 2 received, 33% packet loss, time 2020ms
rtt min/avg/max/mdev = 1.281/1.776/2.272/0.497 ms
chenchao@ubuntu:~$ ping -I ppp1 192.168.8.201
PING 192.168.8.201 (192.168.8.201) from 192.168.8.168 ppp1: 56(84) bytes of data.
64 bytes from 192.168.8.201: icmp_seq=1 ttl=64 time=1.85 ms
64 bytes from 192.168.8.201: icmp_seq=2 ttl=64 time=0.971 ms
64 bytes from 192.168.8.201: icmp_seq=3 ttl=64 time=0.927 ms
^C
--- 192.168.8.201 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2020ms
rtt min/avg/max/mdev = 0.927/1.249/1.850/0.426 ms
chenchao@ubuntu:~$
6.3 PPTP与L2TP区别
1、PPTP要求互联网络为IP网络。L2TP只要求隧道媒介提供面向数据包的点对点的连接。L2TP可以在IP(使用UDP),帧中继永久虚拟电路(PVCs)、X.25虚拟电路(VCs)或ATM网络上使用;
2、PPTP只能在两端点间建立单一隧道。L2TP支持在两端点间使用多隧道。使用L2TP,用户可以针对不同的服务质量创建不同的隧道
3、L2TP自身不提供隧道验证,从L2TP的包头可以看出。隧道认证是由PPP(pap或chap)协议提供!而PPTP则支持隧道验证,PPTP自身就是PPP的扩展!但是当L2TP或PPTP与IPsec共同使用时,可以由IPsec提供隧道验证,不需要在第2层协议上验证隧道.
6.4 IPsec
安装参考:https://cloud.tencent.com/developer/article/1545155
安装IPsec,使用参考脚本安装,脚本ipsec.sh如下:
脚本文件setup.sh
服务器配置:
一般需要配置以下参数:(客户端使用以下参数配置)
Server IP: 192.168.100.189 //IPsec服务器IP
IPsec PSK: test //密钥
Username: admin //用户名
Password: 12345678 //密码
# cat /etc/ipsec.conf
...
conn shared
left=%defaultroute
leftid=192.168.100.189
#cat /etc/ipsec.secrets
%any %any : PSK "test"
#cat /etc/ppp/chap-secrets
"admin" l2tpd "12345678" *
客户端测试:
1)android客户端:选择配置类型为IPsec Xauth PSK或者L2TP/IPSec PSK;在对应的配置项上配置服务器参数,没有的不用填写。
2)windows 7客户端:
选项中去除“包括 windows 登录域(W)",
安全中选择类型为L2TP/IPsec,并在高级配置中配置密钥。
windows拨号的时候,可能会出现809错误。解决方法为运行reg配置,配置如下:
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\RasMan\Parameters] "ProhibitIpSec"=dword:00000001 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PolicyAgent] "AssumeUDPEncapsulationContextOnSendRule"=dword:00000002
用txt编辑,修改为.reg文件,点击运行即可。如还有其他问题可以参考链接。
当客户端成功链接上服务器后,在客户端与服务器分别建立一个接口。
windows客户端接口
IPsec服务器端接口
ppp0 Link encap:Point-to-Point Protocol
inet addr:192.168.42.1 P-t-P:192.168.42.10 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1280 Metric:1
RX packets:4645 errors:0 dropped:0 overruns:0 frame:0
TX packets:5166 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:538702 (538.7 KB) TX bytes:3879516 (3.8 MB)
3)win10测试未通???
4)Linux客户端
6.5.虚拟专用网络客户端测试
7.ubuntu搭建路由
7.1模拟场景
使用vmware安装搭建ubuntu路由来模拟网络环境。具体实现环境见图。
这里用一台win7系统安装vmware软件虚拟出四个ubuntu系统,分别为上图的A,B,C,D。按照拓扑,AD分别是测试PC,BC分别是路由器。AB/CD就是一个完整的路由过程。
7.2vmware配置过程
1.分别创建四个ubuntu虚拟系统,可以创建一个,克隆出三个。
2.添加虚拟网卡,由于router需要两个接口。
VMnet2,配置为主机模式,子网IP:10.1.0.0
VMnet3,配置为桥接模式,绑定物理网卡
VMnet4,配置为主机模式,子网IP:10.2.0.0
3.将虚拟网卡配置到虚拟机的网络适配器中。
IPsecA 配置网络适配器 VMnet2
IPsecB 配置网络适配器 VMnet2
IPsecB 配置网络适配器2 VMnet3
IPsecC 配置网络适配器 VMnet4
IPsecC 配置网络适配器2 VMnet3
IPsecD 配置网络适配器 VMnet4
3.配置ubuntu网络配置,/etc/network/interfaces,分别如下:
1)IPsecA
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 10.1.0.10
netmask 255.255.255.0
gateway 10.1.0.1
dns-nameservers 10.1.0.1
2)IPsecB
# The loopback network interface
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 10.1.0.1
netmask 255.255.255.0
gateway 10.1.0.254 #gw & dns 未使用
#dns-nameservers 10.1.0.254
auto eth1
iface eth1 inet static
address 192.168.0.1
netmask 255.255.255.0
gateway 192.168.0.254
#dns-nameservers 192.168.0.254
3)IPsecC
# The loopback network interface
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 10.2.0.1
netmask 255.255.255.0
gateway 10.2.0.254
#dns-nameservers 10.1.0.254
auto eth1
iface eth1 inet static
address 192.168.0.2
netmask 255.255.255.0
gateway 192.168.0.254
#dns-nameservers 192.168.0.254
4)IPsecD
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 10.2.0.10
netmask 255.255.255.0
gateway 10.2.0.1
dns-nameservers 10.2.0.1
4.添加配置规则:
1)使能路由IP forward。
在/etc/sysctl.conf
net.ipv4.ip_forward = 1
或使用命令:
sudo echo 1 > /proc/sys/net/ipv4/ip_forward
2)配置iptables规则。
sudo iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
sudo iptables -A FORWARD -i eth1 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
查看iptables -L,发现在FORWARD中有DROP动作,目前仅为了配置网络正常通信,将DROP的rule删除。有需要再根据需要配置DROP规则。
7.3测试验证
如果一切配置正常,测试结果如下:
1)WIN7可以ping通IPsecA,IPsecB,IPsecC,IPsecD。
2)IPsecA与IPsecD ping不通,因为分别在各自的内网中相互隔离。
7.4搭建IPsec(strongswan)
参考:https://blog.csdn.net/puppylpg/article/details/64918562
在以上的环境中,IPsecA与IPsecD是ping不通的,也就是网络无法通信,访问;这就需要IPsec来搭建隧道。隧道搭建需在IPsecB与IPsecC之间搭建。配置文件下载:https://www.strongswan.org/testing/testresults/ikev2/net2net-psk/index.html
7.4.1 搭建strongswan
1)安装strongSwan,分别在IPsecB与IPsecC上安装
sudo apt-get install strongswan
2)配置IPsecB配置文件:/etc/ipsec.conf,/etc/ipsec.secrets及/etc/strongswan.conf
root@IPsecB:/home/chenchao# cat /etc/ipsec.conf
# /etc/ipsec.conf - strongSwan IPsec configuration file
config setup
conn %default
ikelifetime=60m
keylife=20m
rekeymargin=3m
keyingtries=1
authby=secret
keyexchange=ikev2
mobike=no
conn net-net
left=192.168.0.1
leftsubnet=10.1.0.0/16
leftid=@moon.strongswan.org
leftfirewall=yes
right=192.168.0.2
rightsubnet=10.2.0.0/16
rightid=@sun.strongswan.org
auto=add
root@IPsecB:/home/chenchao# cat /etc/ipsec.secrets
# /etc/ipsec.secrets - strongSwan IPsec secrets file
@moon.strongswan.org @sun.strongswan.org : PSK 0sv+NkxY9LLZvwj4qCC2o/gGrWDF2d21jL
@moon.strongswan.org %any : PSK 0x45a30759df97dc26a15b88ff
@sun.strongswan.org : PSK "This is a strong password"
: PSK 'My "home" is my "castle"!'
192.168.0.1 : PSK "Andi's home"
root@IPsecB:/home/chenchao# cat /etc/strongswan.conf
# /etc/strongswan.conf - strongSwan configuration file
#charon {
# load = random nonce aes sha1 sha2 curve25519 hmac stroke kernel-netlink socket-default updown
# multiple_authentication = no
#}
starter {
load_warning = no
}
在/etc/ipsec.conf中的left及right配置可以使用魔术字表示:
left=%defaultroute
right=%any
3)配置IPsecC配置文件:/etc/ipsec.conf,/etc/ipsec.secrets及/etc/strongswan.conf
root@IPsecC:/home/chenchao# cat /etc/ipsec.conf
# /etc/ipsec.conf - strongSwan IPsec configuration file
config setup
conn %default
ikelifetime=60m
keylife=20m
rekeymargin=3m
keyingtries=1
authby=secret
keyexchange=ikev2
mobike=no
conn net-net
left=192.168.0.2
leftsubnet=10.2.0.0/16
leftid=@sun.strongswan.org
leftfirewall=yes
right=192.168.0.1
rightsubnet=10.1.0.0/16
rightid=@moon.strongswan.org
auto=add
root@IPsecC:/home/chenchao# cat /etc/ipsec.secrets
# /etc/ipsec.secrets - strongSwan IPsec secrets file
@moon.strongswan.org @sun.strongswan.org : PSK 0sv+NkxY9LLZvwj4qCC2o/gGrWDF2d21jL
root@IPsecC:/home/chenchao# cat /etc/strongswan.conf
# /etc/strongswan.conf - strongSwan configuration file
#charon {
# load = random nonce aes sha1 sha2 curve25519 hmac stroke kernel-netlink socket-default updown
# multiple_authentication = no
#}
starter {
load_warning = no
}
4)分别在IPsecB与IPsecC上运行ipsec
root@IPsecB:/home/chenchao# ipsec restart
root@IPsecC:/home/chenchao# ipsec restart
5)在IPsecC上运行net-net连接来建立隧道
root@IPsecC:/home/chenchao# ipsec up net-net
initiating IKE_SA net-net[1] to 192.168.0.1
generating IKE_SA_INIT request 0 [ SA KE No N(NATD_S_IP) N(NATD_D_IP) ]
sending packet: from 192.168.0.2[500] to 192.168.0.1[500] (1212 bytes)
received packet: from 192.168.0.1[500] to 192.168.0.2[500] (440 bytes)
parsed IKE_SA_INIT response 0 [ SA KE No N(NATD_S_IP) N(NATD_D_IP) N(MULT_AUTH) ]
authentication of 'sun.strongswan.org' (myself) with pre-shared key
establishing CHILD_SA net-net
generating IKE_AUTH request 1 [ IDi N(INIT_CONTACT) IDr AUTH SA TSi TSr N(MULT_AUTH) N(EAP_ONLY) ]
sending packet: from 192.168.0.2[500] to 192.168.0.1[500] (396 bytes)
received packet: from 192.168.0.1[500] to 192.168.0.2[500] (220 bytes)
parsed IKE_AUTH response 1 [ IDr AUTH SA TSi TSr N(AUTH_LFT) ]
authentication of 'moon.strongswan.org' with pre-shared key successful
IKE_SA net-net[1] established between 192.168.0.2[sun.strongswan.org]...192.168.0.1[moon.strongswan.org]
scheduling reauthentication in 3386s
maximum IKE_SA lifetime 3566s
connection 'net-net' established successfully
6)当成功建立net-net隧道后,会在IPsecB与IPsecC建立两个转发规则。
root@IPsecB:/home/chenchao# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 10.2.0.0/16 10.1.0.0/16 policy match dir in pol ipsec reqid 1 proto esp
ACCEPT all -- 10.1.0.0/16 10.2.0.0/16 policy match dir out pol ipsec reqid 1 proto esp
root@IPsecC:/home/chenchao# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 10.1.0.0/16 10.2.0.0/16 policy match dir in pol ipsec reqid 1 proto esp
ACCEPT all -- 10.2.0.0/16 10.1.0.0/16 policy match dir out pol ipsec reqid 1 proto esp
7)此时,IPsecA与IPsecD之间可以互通通信,可以ping通了。
8)如果只是配置IPsecB与IPsecC进行协议调试的话,需要将配置文件ipsec.conf中的rightsubnet=,及leftsubnet=的配置行删除,否则IPsec间协议可能会协商失败。
9)查看ipsec状态
root@IPsecCli:/home/chenchao# ipsec statusall
Status of IKE charon daemon (strongSwan 5.9.0, Linux 4.4.0-142-generic, x86_64):
uptime: 47 minutes, since Nov 09 20:18:47 2020
malloc: sbrk 1351680, mmap 0, used 274928, free 1076752
worker threads: 11 of 16 idle, 5/0/0/0 working, job queue: 0/0/0/0, scheduled: 6
loaded plugins: charon aes des rc2 sha2 sha1 md5 random nonce x509 revocation constraints pubkey pkcs1 pkcs7 pkcs8 pkcs12 pgp dnskey sshkey pem fips-prf curve25519 xcbc cmac hmac drbg attr kernel-netlink resolve socket-default stroke vici updown xauth-generic counters
Listening IP addresses:
192.168.100.63
Connections:
net-net: 192.168.100.63...192.168.100.62 IKEv2
net-net: local: [sun.strongswan.org] uses pre-shared key authentication
net-net: remote: [moon.strongswan.org] uses pre-shared key authentication
net-net: child: dynamic === dynamic TUNNEL
Security Associations (1 up, 0 connecting):
net-net[2]: ESTABLISHED 17 seconds ago, 192.168.100.63[sun.strongswan.org]...192.168.100.62[moon.strongswan.org]
net-net[2]: IKEv2 SPIs: 95e3699cd2a9e7f9_i* 8dd0fa21b4fdbd8d_r, pre-shared key reauthentication in 53 minutes
net-net[2]: IKE proposal: AES_CBC_128/HMAC_SHA2_256_128/PRF_AES128_XCBC/CURVE_25519
net-net{3}: INSTALLED, TUNNEL, reqid 1, ESP SPIs: c52a2159_i c0202b6d_o
net-net{3}: AES_CBC_128/HMAC_SHA2_256_128, 2496 bytes_i (36 pkts, 0s ago), 3060 bytes_o (19 pkts, 17s ago), rekeying in 14 minutes
net-net{3}: 192.168.100.63/32 === 192.168.100.62/32
7.4.2 搭建strongMan
strongMan为strongswan的GUI管理页面。
1.下载安装:
chenchao@IPsecB:~/vpn/#git clone https://github.com/strongswan/strongMan.git
cd strongMan
./setup.py install
2.建立配置环境:
在/etc/strongswan.d下建立strongMan.conf文件:
charon {
start-scripts {
strongman = /home/chenchao/vpn/strongMan/configloader.py
}
}
3.运行strongMan:
chenchao@IPsecB:~/vpn/strongMan$ ./run.py
4.登入界面:
http://192.168.100.60:1515/
http://localhost:1515
用户名:John,密码:Lennon@1940
5.web界面查看配置参数配置
7.5IPsec 参考
参考配置信息:https://zh.opensuse.org/index.php?title=SDB:Setup_Ipsec_VPN_with_Strongswan&variant=zh
8.TCPDUMP抓包
详见(转载):https://blog.csdn.net/lgh1117/article/details/80213397
9. Linux下配置vlan
增加VLAN=100的虚接口在eth0上,#vconfig add eth0 100
在eth0.100虚接口上配置IP地址,#ifconfig eth0.100 10.10.10.20
使用ifconfig查看配置是否生效,#ifconfig
删除刚才配置的eth0.100虚接口,#vconfig rem eth0.100