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
| https://blog.8086k.cn/archives/104/
1、开启net.ipv4.ip_forward echo "net.ipv4.ip_forward=1">>/etc/sysctl.conf sysctl -p
2、安装wireguard apt install wireguard iptables -y
modprobe wireguard && lsmod | grep wireguard
配置wireguard服务端 公钥私钥生产 cd /etc/wireguard
wg genkey | tee sprivatekey | wg pubkey > spublickey wg genkey | tee cprivatekey | wg pubkey > cpublickey
服务端配置 echo "[Interface] # 服务器的私匙,对应客户端配置中的公匙(自动读取上面刚刚生成的密匙内容) PrivateKey = $(cat sprivatekey) # 本机的内网IP地址,一般默认即可,除非和你服务器或客户端设备本地网段冲突 Address = 192.168.145.1/24 PostUp = iptables -t nat -A PREROUTING -d 10.0.24.11 -p tcp -m multiport --dports 65533,65534 -j ACCEPT; iptables -t nat -A PREROUTING -d 10.0.24.11 -p udp -m multiport --dports 65533,65534 -j ACCEPT; iptables -t nat -A PREROUTING -d 10.0.24.11 -j DNAT --to-destination 10.10.0.34; iptables -t nat -A POSTROUTING -s 10.10.0.34 -j SNAT --to-source 10.0.24.11;
#10.0.24.11替换为你自己的网卡地址 65533为ssh端口 65534为wireguard端口 PostDown = iptables -t nat -D PREROUTING -d 10.0.24.11 -p tcp -m multiport --dports 65533,65534 -j ACCEPT; iptables -t nat -D PREROUTING -d 10.0.24.11 -p udp -m multiport --dports 65533,65534 -j ACCEPT; iptables -t nat -D PREROUTING -d 10.0.24.11 -j DNAT --to-destination 10.10.0.34; iptables -t nat -D POSTROUTING -s 10.10.0.34 -j SNAT --to-source 10.0.24.11;
#10.0.24.11替换为你自己的网卡地址 # 服务端监听端口,可以自行修改 ListenPort = 65534 # 保持默认 MTU = 1420 # [Peer] 代表客户端配置,每增加一段 [Peer] 就是增加一个客户端账号。 [Peer]
# 该客户端账号的公匙,对应客户端配置中的私匙 PublicKey = $(cat cpublickey) # 该客户端账号的内网IP地址 AllowedIPs = 192.168.145.0/24,10.10.0.0/16"|sed '/^#/d;/^\s*$/d' > wg0.conf
修改ssh端口 vim /etc/ssh/sshd_config 修改 Port 22 改为 Port 65533 systemctl restart sshd
服务端启动 wg-quick up wg0
4、routeros配置wireguard echo "[Interface] # 客户端的私匙,对应服务器配置中的客户端公匙(自动读取上面刚刚生成的密匙内容) PrivateKey = $(cat cprivatekey) # 客户端的内网IP地址 Address = 10.0.0.2/24 # 解析域名用的DNS DNS = 8.8.8.8 # 保持默认 MTU = 1420 [Peer] # 服务器的公匙,对应服务器的私匙(自动读取上面刚刚生成的密匙内容) PublicKey = $(cat spublickey) # 服务器地址和端口 Endpoint = X.X.X.X:443 # 因为是客户端,所以这个设置为全部IP段即可 AllowedIPs = 0.0.0.0/0, ::0/0 # 保持连接 PersistentKeepalive = 25"|sed '/^#/d;/^\s*$/d' > client.conf
在routeros终端中输入以下内容 interface/wireguard/add name="wireguard1" mtu=1300 private-key="你的interface私钥" /interface/wireguard/peers/add interface="wireguard1" public-key="你的peers公钥" endpoint-address="服务端ip" endpoint-port=端口 allowed-address="0.0.0.0/0" persistent-keepalive=5 /ip/address/add address="wireguard网卡网段" interface="wireguard1" routing/table/add name="wgt" fib /ip/route/add dst-address="0.0.0.0/0" gateway="wireguard1" routing-table="wgt" /routing/rule/add src-address="本地目标主机" routing-mark=main action=lookup-only-in-table table=wgt /ip/firewall/nat/add chain="srcnat" action=masquerade
|