# mac在Debian安装wireguard和使用 URL: https://chengjinfang.com/posts/2018/manual-wireguard/ Author: 程锦方 Date: 2018-10-07 Tags: #note #manual Summary: 配置 Debian 服务端与 macOS 客户端,记录 WireGuard 的安装与连接。 ------------------------------------------------------------------------ ## Debian服务器端 1、在服务器安装wireguard ``` echo "deb http://deb.debian.org/debian/ unstable main" > /etc/apt/sources.list.d/unstable.list printf 'Package: *\nPin: release a=unstable\nPin-Priority: 150\n' > /etc/apt/preferences.d/limit-unstable apt update apt install wireguard ``` 2、生成公钥、私钥、共享密钥 ``` sudo mkdir -p /etc/wireguard && sudo chmod 0777 /etc/wireguard cd /etc/wireguard umask 077 wg genkey | tee private.key | wg pubkey > public.key | wg genpsk > preshared.key ``` 输出私钥 ``` cat private.key ``` 输出公钥 ``` cat public.key ``` 输出共享密钥 ``` cat preshared.key ``` 3、编辑配置文件 ``` sudo vi wg0.conf ----------------------------- [Interface] Address = 192.168.1.1/24服务器上的接口 ListenPort = 端口 PrivateKey = 服务器私钥 PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE SaveConfig = true [Peer] PublicKey = 连接节点公钥(由客户端生成) AllowedIPs = 192.168.1.2/32 (VPN 隧道的内网 IP 段) ``` 设置服务器的 NAT 流量转发 ``` sudo vi /etc/sysctl.conf ------------------------------ net.ipv4.ip_forward = 1 ------------------------------ sudo sysctl -p ``` 在服务器端添加虚拟网卡 wg0,设置隧道 IP 和 iptables 规则 ``` sudo ip link add dev wg0 type wireguard sudo ip address add dev wg0 192.168.1.1/24 sudo ip link set wg0 up sudo wg setconf wg0 /etc/wireguard/wg0.conf sudo iptables -A FORWARD -i wg0 -j ACCEPT sudo iptables -A FORWARD -o wg0 -j ACCEPT sudo iptables -t nat -A POSTROUTING -o wg0 -j MASQUERADE sudo iptables -t nat -A POSTROUTING -o ens4 -j MASQUERADE ``` 如果遇到提示 RTNETLINK answers: Operation not supported很可能就是wireguard模块没有加载 查看模块是否启动 ``` lsmod | grep wireguard ``` 先检查是否升级到最新系统再启动模块,如果报错需要重启 ``` modprobe wireguard ``` 检查wg设置是否正常 ``` sudo wg show ``` 4、设置开启启动 ``` systemctl enable wg-quick@wg0 ``` ## mac os端 1、安装 ``` /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" brew install wireguard-tools ``` 2、生成公钥、私钥 ``` wg genkey | tee private.key | wg pubkey > public.key ``` 输出私钥 ``` cat private.key ``` 输出公钥 ``` cat public.key ``` 3、编辑配置文件wg0.conf ``` [Interface] Address = 192.168.1.3/32 本地接口地址 PrivateKey = 本地客户端私钥 ListenPort = 端口 DNS = 1.1.1.1 [Peer] PublicKey = 服务器端公司(由服务器端生成) AllowedIPs = 0.0.0.0/0,::0/0 (VPN隧道的内网IP段) Endpoint = 远程服务器公网IP和端口 ``` 4、启动/关闭 ``` wg-quick up wg0 wg-quick down wg0 ``` 参考:https://www.wireguard.com/quickstart/ ------------------------------------------------------------------------ Site: https://chengjinfang.com/ · All Index: https://chengjinfang.com/all/ · RSS: https://chengjinfang.com/rss.xml