wireguard搭建


1.服務端安裝(推薦linux)

以下三種方式,任選其一

Method 1: a signed module is available as built-in to CentOS's kernel-plus:

$ sudo yum install yum-utils epel-release $ sudo yum-config-manager --setopt=centosplus.includepkgs=kernel-plus --enablerepo=centosplus --save $ sudo sed -e 's/^DEFAULTKERNEL=kernel$/DEFAULTKERNEL=kernel-plus/' -i /etc/sysconfig/kernel $ sudo yum install kernel-plus wireguard-tools $ sudo reboot 

Method 2: users wishing to stick with the standard kernel may use ELRepo's pre-built module:

$ sudo yum install epel-release elrepo-release $ sudo yum install yum-plugin-elrepo $ sudo yum install kmod-wireguard wireguard-tools 

Method 3: users running non-standard kernels may wish to use the DKMS package instead:

$ sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm $ sudo curl -o /etc/yum.repos.d/jdoss-wireguard-epel-7.repo https://copr.fedorainfracloud.org/coprs/jdoss/wireguard/repo/epel-7/jdoss-wireguard-epel-7.repo $ sudo yum install wireguard-dkms wireguard-tools 

2.生成密鑰對

cd /etc/wireguard # 如果目錄不存在請手動創建:mkdir /etc/wireguard # 開始生成 密匙對(公匙+私匙)。 wg genkey | tee privatekey-server | wg pubkey > publickey-server # 生成服務端密鑰對 wg genkey | tee privatekey-client | wg pubkey > publickey-client # 生成客戶端密鑰對 

3.配置服務端文件

vim /etc/wireguard/wg0.conf輸入以下內容

[Interface] Address = 10.100.0.1/16 # 這里指的是使用 10.100.0.1,網段大小是 16 位 #######填寫eth0的網卡地址,或者公網地址
SaveConfig = true ListenPort = 51820 # 監聽的 UDP 端口 PrivateKey = < 這里填寫 Server 上 privatekey 的內容 > # 下面這兩行規則允許訪問服務器的內網,注意替換`eth0` PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE # Client,可以有很多 Peer [Peer] PublicKey = < 這里填寫 Client 上 publickey 的內容 > AllowedIPs = 10.100.0.2/32 # 這個 Peer 只能是 10.100.0.2/32 #######允許訪問的ip,網段跟上面填寫的保持一致就可以 # 如果想把所有流量都通過服務器的話,這樣配置: # AllowedIPs = 0.0.0.0/0, ::/0

4.啟停服務端

wg-quick up wg0    #啟動服務端 wg-quick down wg0 #停止服務端 wg #查看節點列表 wg syncconf wg0 <(wg-quick strip wg0) #重載配置文件,不影響已有連接. 

5.配置客戶端文件

[Interface] PrivateKey = < 這里填寫 Client 上 privatekey 的內容 > Address = 10.100.0.2/32 ############這個網段填寫服務器的網段一致 DNS = 8.8.8.8 # 連接后使用的 DNS, 如果要防止 DNS 泄露,建議使用內網的 DNS 服務器 [Peer] PublicKey = < 這里填寫 Server 上 publickey 的內容 > Endpoint = 1.1.1.1:51820 # 服務端公網暴露地址,51280 是上面指定的 AllowedIPs = 10.100.0.0/16,172.17.0.11/20 # 指定要訪問的服務端網段,或者設置0.0.0.0/0來進行全局代理.必須包含服務器的網段 PersistentKeepalive = 25 

然后復制文件內容到各客戶端即可.

FAQ

1.無法訪問服務端內網

# 開啟內核轉發功能 echo 1 > /proc/sys/net/ipv4/ip_forward echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf sysctl -p


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM