1 n2n简介

 为了满足两个不同局域网的机器进行通信,让不同网段的机器能够进行P2P( 点对点 peer-to-peer ) 通信。

2 n2n源码

https://github.com/ntop/n2n.git

3 n2n名词

3.1 SuperNode 超级节点

SuperNode 相当与注册中心, 它会记录边缘节点的连接信息,告诉各个边缘节点如何去找到其它的边缘节点。如果超级节点发生故障,那么边缘节点之间将不能正常的进行通信。在整个N2N网络中必须至少拥有一个SuperNode。

3.2 Edge 边缘节点

边缘节点是指所有通过 SuperNode 组网而成的节点,无论你处于哪个位置哪种网络环境下,edge节点之间都能进行通信。一台计算机可以拥有多个edge, 局域网根据子网掩码来决定两台机器是否处于同一个网段,而edge需要添加一组账号密码,在N2N 里面称作 GroupName 和 password ,Group0 和 Group 1 里面的 10.0.0.1 是不一样的。

4 n2n配置

版本:
Welcome to n2n v.2.8.0 for Debian

4.1 下载n2n

#创建一个 n2n 的目录,用于存放各版本源码
mkdir n2n#进入n2n 目录
cd n2n
#下载 n2n 包
git clone https://github.com/ntop/n2n.git
#进入n2n-3.1.1目录
cd n2n

4.2 安装依赖

# 安装 openssl
yum install -y openssl-devel  cmake  net-tools  gcc gcc-c++

4.3 进行编译

4.3.1 第一步

# 新建 build 目录
cmake -E make_directory build
# 进入 build 目录
cd build
# 在当前目录生成 makefile
cmake ..
# cmake 的执行结果:
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Build for version: 3.1.1-76-g709590d
-- Configuring done
-- Generating done
-- Build files have been written to: /home/n2n/n2n/build

4.3.2 第二步

# 编译过程中会有一些告警提示,可以忽略
cmake --build . --config Release
#编译过程
/usr/bin/cmake -H/home/n2n/n2n -B/home/n2n/n2n/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/n2n/n2n/build/CMakeFiles /home/n2n/n2n/build/CMakeFiles/progress.marks
/usr/bin/gmake -f CMakeFiles/Makefile2 all
gmake[1]: Entering directory `/home/n2n/n2n/build'
/usr/bin/gmake -f CMakeFiles/doc.dir/build.make CMakeFiles/doc.dir/depend
gmake[2]: Entering directory `/home/n2n/n2n/build'
....................
gmake[2]: Leaving directory `/home/n2n/n2n/build'
/usr/bin/cmake -E cmake_progress_report /home/n2n/n2n/build/CMakeFiles  52
[100%] Built target tests-wire
gmake[1]: Leaving directory `/home/n2n/n2n/build'
/usr/bin/cmake -E cmake_progress_start /home/n2n/n2n/build/CMakeFiles 0

4.3.3 第三步

# 将编译后的执行文件安装到 sbin 或 bin 目录下
make install
# 编译安装目标目录示例
[100%] Built target tests-wire
make[1]: Leaving directory `/home/n2n/n2n/build'
/usr/bin/cmake -E cmake_progress_start /home/n2n/n2n/build/CMakeFiles 0
make -f CMakeFiles/Makefile2 preinstall
make[1]: Entering directory `/home/n2n/n2n/build'
make[1]: Nothing to be done for `preinstall'.
make[1]: Leaving directory `/home/n2n/n2n/build'
Install the project...
/usr/bin/cmake -P cmake_install.cmake
-- Install configuration: ""
-- Installing: /usr/local/sbin/edge
-- Installing: /usr/local/sbin/supernode
-- Installing: /usr/local/bin/n2n-benchmark
-- Installing: /usr/share/man/man8/edge.8.gz
-- Installing: /usr/share/man/man1/supernode.1.gz
-- Installing: /usr/share/man/man7/n2n.7.gz

4.4 设置开机自启及火墙配置

4.4.1 设置开机自启

# 先查询一下这个端口是否被占用
netstat -anp|grep 8864
# 编辑开机自启文件
vi /etc/rc.local

4.4.2 防火墙配置

# 查看防⽕墙的状态
iptables -L -n -v --line-numbers
# 如果有需要可以给端口创建两个入站规则,这个视不同的公有云服务器的安全配置而定
iptables -I INPUT -p tcp  --dport 8864 -j ACCEPT
iptables -I INPUT -p udp  --dport 8864 -j ACCEPT

4.4.3 重启

reboot

4.5 验证

查看进程是否进行

# 检查 supernode 进程是否已运行
netstat -anp|grep `ps -ef|grep supernode|grep -v grep|awk {'print $2'}`

5 Linux 环境 edge 的编译、配置与启动

5.1 配置n2n启动脚本

vi /etc/init.d/n2n
#!/bin/sh
### BEGIN INIT INFO
# Provides:          n2n
# Required-Start:    $network $remote_fs $local_fs
# Required-Stop:     $remote_fs $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start or stop the n2n VPN
# Description:       This script controls the n2n VPN service.
#                    It is called from the boot, halt and reboot scripts.
#                    So far, only 1 PVN is supported by this script.
#                    More can be started via the command line.
### END INIT INFOset -e# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC='n2n P2P VPN'
NAME=n2n
#DAEMON=/usr/sbin/edge
DAEMON=/usr/local/sbin/edge
DAEMON_ARGS=""
# Arguments to run the daemon with
#PIDFILE=/var/run/$NAME-edge.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0# Check config
if [ ! -f "/etc/default/edge.conf" ]
thenecho "Warning: n2n VPN client is not configured, edit config file in /etc/default/edge.conf." 1>&2exit 0
fi# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
. /lib/lsb/init-functions## Make sure /var/run/n2n exists.
#if [ ! -e /var/run/$NAME ] ; then
#   mkdir -p /var/run/$NAME
#   chown proxy:proxy /var/run/$NAME
#   chmod 755 /var/run/$NAME
#fi# Function that starts the daemon/service
#
do_start()
{if [ -r /sys/class/net/edge0 ]; thenecho edge node is already running.exit 0fi# Return#   0 if daemon has been started#   1 if daemon was already running#   2 if daemon could not be startedstart-stop-daemon --start --quiet --user nobody --exec $DAEMON --test > /dev/null \|| return 1start-stop-daemon --start --quiet --user nobody --exec $DAEMON -- \/etc/default/edge.conf \|| return 2
}#
# Function that stops the daemon/service
#
do_stop()
{# Return#   0 if daemon has been stopped#   1 if daemon was already stopped#   2 if daemon could not be stopped#   other if a failure occurredstart-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user nobody --exec $DAEMONRETVAL="$?"[ "$RETVAL" = 2 ] && return 2# Wait for children to finish too if this is a daemon that forks# and if the daemon is only ever run from this initscript.# If the above conditions are not satisfied then add some other code# that waits for the process to drop all resources that could be# needed by services started subsequently.  A last resort is to# sleep for some time.start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON[ "$?" = 2 ] && return 2# Many daemons don't delete their pidfiles when they exit.rm -f $PIDFILEreturn "$RETVAL"
}#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {## If the daemon can reload its configuration without# restarting (for example, when it is sent a SIGHUP),# then implement that here.#start-stop-daemon --stop --signal 1 --quiet --name $NAMEreturn 0
}case "$1" instart)[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC " "$NAME"do_startcase "$?" in0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;esac# 修改虚拟网卡速度,n2n默认是10M/S,修改为1000M/Sethtool -s edge0 speed 1000 duplex full autoneg off;;stop)[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"do_stopcase "$?" in0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;esac;;status)status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?;;#reload|force-reload)## If do_reload() is not implemented then leave this commented out# and leave 'force-reload' as an alias for 'restart'.##log_daemon_msg "Reloading $DESC" "$NAME"#do_reload#log_end_msg $?#;;restart|force-reload)## If the "reload" option is implemented then remove the# 'force-reload' alias#log_daemon_msg "Restarting $DESC" "$NAME"do_stopcase "$?" in0|1)do_startcase "$?" in0) log_end_msg 0 ;;1) log_end_msg 1 ;; # Old process is still running*) log_end_msg 1 ;; # Failed to startesac;;*)# Failed to stoplog_end_msg 1;;esac;;*)N=/etc/init.d/$NAME#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2exit 3;;
esacexit 0

5.2 配置edge.conf脚本

在/etc/default/目录下创建edge.conf配置文件:

#
#         The configuration file is similar to the command line, with one option per line. An equal
#        sign '=' should be used between key and value. Example: -c=mynetwork or --community=mynetwork
#        This file contains a basic configuration example, please refer to the help (-h) for the full
#        list of available options.
#
#       -d|--tun-device
#        Specifies the name of the TUN interface.
#
#-d=tap0
#
#       -c|--community
#        Specifies the n2n community name the edge belongs to.
#
-c=
#
#       -k
#        Sets the encryption key (ASCII). The environment variable N2N_KEY=<key> can also be used.
#
-k=
#
#       -m
#        Specified the MAC address for the TAP interface (random otherwise).
#
# -m=5E:57:77:58:7F:77
#
#       -a
#        Sets the interface address. For DHCP use '-r -a dhcp:0.0.0.0'.
#
-a=16.16.16.25
#
#       -p
#        Sets the local UDP port to a fixed port.
#
-p=60000
#
#       -l|--supernode-list
#        Specifies the supernode IP and port.
#
-l=168.324.678.8:8864
#
#       -z1 ... -z2| compress outgoing data packets,
#        -z1 = lzo1x,
#        disabled by default
#
-z1
#
#
-bHEerejer

5.3 n2n服务启动

#n2n 服务启动
systemctl daemon-reload
#n2n 服务启动
systemctl start n2n#n2n 服务状态查询
systemctl status n2n#n2n 服务停止
systemctl stop n2n

6 windows环境部署

7 常见问题

问题1:开机启动后/etc/rc.local supdernode没有生效

排查方法:
是rc.local没有执行权限导致

解决方法:
第一种方法:直接加权限

[root@master ~]# ls -l   /etc/rc.local
lrwxrwxrwx. 1 root root 13 Sep 17 19:58 /etc/rc.local -> rc.d/rc.local
[root@master ~]#
[root@master ~]#
[root@master ~]# chmod +x /etc/rc.local

第二种方法:
rc-local服务配置路径为/usr/lib/systemd/system/rc-local.service。在[Unit]模块中添加或修改Requires和After项值为network-online.target。
cat /usr/lib/systemd/system/rc-local.service |grep -v “^#” #过滤掉注释

01 | n2n虚拟局域网相关推荐

  1. 高德地图发布中国高速公路出行大数据报告:2017年假期高速拥堵里程同比2016上升22.5%

    9月5日,高德地图联合交通运输部规划研究院和交通运输部科学研究院,权威发布了<2017上半年中国高速公路出行大数据报告>.该报告以高德地图交通大数据发布平台.大数据开放平台.阿里云ODPS ...

  2. 局域网联机游戏找不到服务器,N2N组建虚拟局域网联机遇到搜不到房间的问题一例...

    N2N搭建虚拟局域网联机玩游戏,朋友老是说搜不到我建的房间,但是我这边却可以搜到他的. 环境 电脑A:192.168.10.1 电脑B:192.168.10.2 这里以CS为例,其他游戏通用 A建好房 ...

  3. N2N组建虚拟局域网

    简介 N2N是一个多平台点对点的穿透工具,非常适合用来远程办公,远程局域网游戏联机. 本文使用的N2N版本是V3版本 文章目录 简介 工作原理 一.两个概念 二.使用方法 1.使用EasyN2N(推荐 ...

  4. 使用N2N搭建虚拟局域网进行游戏联机(服务端及客户端)

    目前主流的格斗游戏一般都是采用的P2P方式进行对战,国内的网络环境参差不齐,游戏对战体验极差,主流的加速器也仅仅是加速了大厅,对于对局并没有加速效果.最终在大佬的推荐下找到了N2N! 版本选择 N2N ...

  5. mcjava盗版联机_使用N2N组建虚拟局域网联机游戏(我的世界 Java版)

    有网友反映组建虚拟局域网后,我的世界无法顺利联机,本文就记录一下联机过程 一.准备 我的世界 Java版 ,版本:1.16.2 HMCL启动器(fabric),版本:3.3.173 二.搭建虚拟局域网 ...

  6. UA MATH563 概率论的数学基础 中心极限定理17 0-1律的应用

    UA MATH563 概率论的数学基础 中心极限定理17 0-1律的应用 第14讲到第16讲我们介绍了Kolmogorov非常著名的几大定理(如下),事实上Kolmogorov开发出这些定理的目标是证 ...

  7. 计算机网络学习笔记 01(更新)

    计算机网络02 https://www.kuangstudy.com/topic/to-update/1419862886657818625 计算机网络 电路交换 分组交换 分组交换将数据划分成为更小 ...

  8. 搞一下 车载以太网实战 | 01 车载以太网帧结构详解

    前言 搞SOA.搞 AP & CP AUTOSAR.搞异构SoC.搞车载以太网.搞车载OS等就找搞一下汽车电子. 全系内容可在<搞一下汽车电子>后台回复 "系列" ...

  9. 使用Hook拦截sendto函数解决虚拟局域网部分游戏联机找不到房间的问题——以文明6为例

    正文 重要提醒(2023-02-13):本文部分内容存在bug,目前正在调试修改,会在一段时间之后更新 重要提醒(2023-02-14):目前已修复主要bug,会在一段时间之后更新,本文计划重写大部分 ...

最新文章

  1. Exception in thread http-bio-80exec-1 java.lang.OutOfMemoryError: PermGen s解决方案
  2. LeetCode OJ -- Binary Tree Paths
  3. ITK:多路输出相同类型的
  4. Django 应用分库,数据迁移成功,数据库没有生成表
  5. c++开发安卓_诺基亚科普,安卓更新为什么那么慢?
  6. ubuntu sudo apt-get update 失败 解决方法
  7. LINUX PATH环境变量
  8. 去除Word文档中的页眉横线
  9. java程序的开发流程
  10. 如何利用OriginPro8画图
  11. UVALive 4487 - Exclusive-OR (加权并查集+异或运算的性质)
  12. excel小写转大写公式_不要眨眼!中英文、大小写转换,一秒就搞定!
  13. 国内比较好的云服务提供商有哪些?
  14. 最后一个问题得到解决
  15. 批量添加Exchange邮件联系人
  16. 关于CPU平台的一些常识
  17. 有意思的复活节彩蛋[摘]
  18. 如何用技术改变生活 哪个瞬间你突然觉得读书真有用?
  19. php如果单数前面加0,php左边用0填充补齐的两种方法
  20. inotify实时同步数据

热门文章

  1. 2023年金砖部分试题与答案(自用)
  2. 打印论文中,抽空更新下
  3. 基于Android studio个人健康养生系统java
  4. 网络协议 一 路由的概念、简述数据包的传输过程、网络常见概念、公网、私网、NAT(私网转公网)
  5. IG厂和N厂的全面真实测评
  6. 你有一个免费的微信商户待领取
  7. Ubuntu挂载阿里云盘
  8. 我的世界有宠物系统的服务器,我的世界:养宠物,玩科技,全新的游戏玩法等你来...
  9. 用python计算贷款_python for android : 贷款月月还款额计算
  10. Windows 11快捷键功能大全