Hướng dẫn cài đặt Wireguard trên CentOS 7

30/12/2020

Tổng quan

WireGuard là giải pháp VPN miễn phí, mã nguồn mở được phát triển nhằm thấy thế giải pháp IPSec. WireGuard được phát triển như là module của Kernel với mục tiêu kế thừa các tính năng sẵn có của Kernel Linux, từ đó tối ưu hiệu năng giải pháp.
Hiện nay giải pháp WireGuard đang dần trở nên phổ biến. Và đặc biệt hơn, WireGuard đã chính thức được phát hành cùng Kernel version 5.6 vào tháng 3 năm 2020, tức tất cả phiên bản, distro Linux sử dụng Kernel từ verion 5.6 trở đi sẽ có sẵn giải pháp WireGuard.

Chuẩn bị

Trước khi bắt đầu chúng ta cần chuẩn bị 2 VM bao gồm:

  • Server VPN WireGuard:
    • OS: CentOS 7
    • Cấu hình: 2 CPU / 2 GB RAM / 25 GB Disk
    • IP: 123.123.123.123 (IP Public – eth0)
  • Client:
    • OS: CentOS 7
    • Cấu hình: 2 CPU / 2 GB RAM / 25 GB Disk
    • IP: 10.10.10.99 (IP LAN – eth0)
  • Cả Server và Client đều đã tắt Firewalld, SELinux

Phần 1: Cài đặt Wireguard tools

Thực hiện trên cả ServerClient với quyền root

Bước 1: Cài đặt Wireguard tools

yum install -y epel-release https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm yum install -y yum-plugin-elrepo yum install -y kmod-wireguard wireguard-tools

Bước 2: Khởi động lại OS

init 6

Kiểm tra

Thực hiện trên Server VPN

[root@vpnserver ~]# modinfo wireguard filename:       /lib/modules/3.10.0-1127.19.1.el7.x86_64/weak-updates/wireguard/wireguard.ko intree:         Y alias:          net-pf-16-proto-16-family-wireguard alias:          rtnl-link-wireguard version:        1.0.20200908 author:         Jason A. Donenfeld <[email protected]> description:    WireGuard secure network tunnel license:        GPL v2 retpoline:      Y rhelversion:    7.8 srcversion:     D75F0FE0090ED5C45DB6EF7 depends:        udp_tunnel,ip6_udp_tunnel vermagic:       3.10.0-1127.el7.x86_64 SMP mod_unload modversions  signer:         The ELRepo Project (http://elrepo.org): ELRepo.org Secure Boot Key sig_key:        F3:65:AD:34:81:A7:B2:0E:34:27:B6:1B:2A:26:63:5B:83:FE:42:7B sig_hashalgo:   sha256

Thực hiện trên Client

[root@client1099 ~]# modinfo wireguard filename:       /lib/modules/3.10.0-1127.19.1.el7.x86_64/weak-updates/wireguard/wireguard.ko intree:         Y alias:          net-pf-16-proto-16-family-wireguard alias:          rtnl-link-wireguard version:        1.0.20200908 author:         Jason A. Donenfeld <[email protected]> description:    WireGuard secure network tunnel license:        GPL v2 retpoline:      Y rhelversion:    7.8 srcversion:     D75F0FE0090ED5C45DB6EF7 depends:        udp_tunnel,ip6_udp_tunnel vermagic:       3.10.0-1127.el7.x86_64 SMP mod_unload modversions  signer:         The ELRepo Project (http://elrepo.org): ELRepo.org Secure Boot Key sig_key:        F3:65:AD:34:81:A7:B2:0E:34:27:B6:1B:2A:26:63:5B:83:FE:42:7B sig_hashalgo:   sha256

Phần 2: Sinh cặp khóa Public / Private tại Server và Client

Bước 1: Sinh cặp khóa Public / Private tại Server VPN WireGuard

Thực hiện tại node Server VPN WireGuard

Tạo thư mục chứa cấu hình WireGuard

sudo mkdir -p /etc/wireguard/

Sinh cặp khóa Public / Private

wg genkey | sudo tee /etc/wireguard/server_private.key | wg pubkey | sudo tee /etc/wireguard/server_public.key

Kết quả

[root@vpnserver ~]# wg genkey | sudo tee /etc/wireguard/server_private.key | wg pubkey | sudo tee /etc/wireguard/server_public.key 1D4UVRkKVRufEXT2YcVmiUpV+r2io9iwSSB+uwh33GE=

Kiểm tra

[root@vpnserver ~]# cat /etc/wireguard/server_private.key wCa2SRefk/XUSfopmn6FTThCINCng/+EoTF8qEgg0XU=  [root@vpnserver ~]# cat /etc/wireguard/server_public.key  1D4UVRkKVRufEXT2YcVmiUpV+r2io9iwSSB+uwh33GE=

Lưu ý:

  • Khóa Private Server: wCa2SRefk/XUSfopmn6FTThCINCng/+EoTF8qEgg0XU=
  • Khóa Public Server: 1D4UVRkKVRufEXT2YcVmiUpV+r2io9iwSSB+uwh33GE=

Bước 2: Sinh cặp khóa Public / Private tại Client

Thực hiện tại node Client

Tạo thư mục chứa cấu hình WireGuard

sudo mkdir -p /etc/wireguard/

Sinh cặp khóa Public / Private

wg genkey | sudo tee /etc/wireguard/client_private.key | wg pubkey | sudo tee /etc/wireguard/client_public.key

Kết quả

[root@client1099 ~]# wg genkey | sudo tee /etc/wireguard/client_private.key | wg pubkey | sudo tee /etc/wireguard/client_public.key JDllguLLGT1uctWL2uLK72XRU53XikLG5KY1HsebSFs=

Kiểm tra

[root@client1099 ~]# cat /etc/wireguard/client_private.key AEy7jE1bWj262W4oPOrRniqWRxGrSJPKoZ8tk9gzH3Y= [root@client1099 ~]# cat /etc/wireguard/client_public.key JDllguLLGT1uctWL2uLK72XRU53XikLG5KY1HsebSFs=

Lưu ý:

  • Khóa Private Client: AEy7jE1bWj262W4oPOrRniqWRxGrSJPKoZ8tk9gzH3Y=
  • Khóa Public Client: JDllguLLGT1uctWL2uLK72XRU53XikLG5KY1HsebSFs=

Phần 2: Cấu hình WireGuard

Bước 1: Cấu hình tại Server VPN WireGuard

Thực hiện tại node Server VPN WireGuard

Tạo mới file /etc/wireguard/wg0.conf

Cú pháp

[Interface] Address = <VPN_SERVER_IP>/<PREFIX> ListenPort = 51820 PrivateKey = <SERVER_PRIVATE_KEY>  PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -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; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE  [Peer] PublicKey = <CLIENT_PUBLIC_KEY> AllowedIPs = <VPN_CLIENT_IP>/<PREFIX>

Lưu ý section [Interface]:

  • Address: IP VPN của Wireguard Server cũng như là range cấp VPN cho các client
  • PrivateKey: Private key của VPN Server, nằm tại /etc/wireguard/server_private.key
  • ListenPort: Port UDP phục vụ cho Client kết nối tới
  • PostUp, PostDown: NAT traffic interface wg0 tới Interface eth0, khi đó client sẽ sử dụng Gateway của VPN Server để ra Internet

Lưu ý section [Peer]:

  • PublicKey: Public key của Client, nằm tại /etc/wireguard/client_public.key (trên node Client)
  • AllowedIPs: IP VPN của Client khi kết nối tới

File config trong bài /etc/wireguard/wg0.conf

[Interface] Address = 30.0.0.1/24 ListenPort = 51820 PrivateKey = wCa2SRefk/XUSfopmn6FTThCINCng/+EoTF8qEgg0XU=  PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -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; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE  [Peer] PublicKey = JDllguLLGT1uctWL2uLK72XRU53XikLG5KY1HsebSFs= AllowedIPs = 30.0.0.2/32

Thay đổi file permission

sudo chmod 600 /etc/wireguard/ -R

Bước 2: Cấu hình tại Client VPN

Thực hiện tại node Client

Tạo mới file /etc/wireguard/wg-client0.conf

Cú pháp

[Interface] Address = <VPN_CLIENT_IP>/<PREFIX> PrivateKey = <CLIENT_PRIVATE_KEY>  [Peer] PublicKey = <SERVER_PUBLIC_KEY> AllowedIPs = 0.0.0.0/0 Endpoint = <SERVER_PUBLIC_IP>:<PORT> PersistentKeepalive = 25

Lưu ý section [Interface]:

  • Address: IP VPN khi Client kết nối tới
  • PrivateKey: Khóa Private của Client, tại thư mục /etc/wireguard/client_private.key

Lưu ý section [Peer]:

  • PublicKey: Khóa Public của VPN Server, tại thư mục /etc/wireguard/server_public.key
  • AllowedIPs: Giá trị 0.0.0.0/0 tức toàn bộ traffic sẽ đi qua Interface VPN để ra Internet
  • Endpoint: Địa của Public + Port của Server VPN
  • PersistentKeepalive: Tạo kết nối dạng Keep alive

File config trong bài

[Interface] Address = 30.0.0.2/24 PrivateKey = AEy7jE1bWj262W4oPOrRniqWRxGrSJPKoZ8tk9gzH3Y=  [Peer] PublicKey = 1D4UVRkKVRufEXT2YcVmiUpV+r2io9iwSSB+uwh33GE= AllowedIPs = 0.0.0.0/0 Endpoint = 123.123.123.123:51820 PersistentKeepalive = 25

Thay đổi file permission

sudo chmod 600 /etc/wireguard/ -R

Phần 3: Bổ sung cấu hình IP Forwarding trên VPN Server

Thực hiện tại node Server VPN WireGuard

Thực hiện

echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf sysctl -p

Kết quả

[root@vpnserver ~]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf [root@vpnserver ~]# sysctl -p net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 net.ipv4.ip_forward = 1 net.ipv4.ip_forward = 1

Phần 4: Khởi động VPN Server

Khởi tạo Server VPN Wireguard

Thực hiện tại node Server VPN WireGuard

Cách 1: Sử dụng CMD wg-quick

Khởi tạo tiến trình

wg-quick up /etc/wireguard/wg0.conf

Tắt tiến trình

wg-quick down /etc/wireguard/wg0.conf

Cách 2: Sử dụng Systemd

systemctl start [email protected] systemctl enable [email protected] systemctl status [email protected]

Kết nối VPN tại Client

Thực hiện tại node Client

Khởi động VPN Client

systemctl start [email protected]

Phần 5: Kiểm tra kết nối

Thực hiện tại Server VPN

Kiểm tra các interface hiện có

ip a

Kết quả

[root@vpnserver ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00     inet 127.0.0.1/8 scope host lo        valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000     link/ether fa:16:3e:01:a3:23 brd ff:ff:ff:ff:ff:ff     inet 123.123.123.123/24 brd 103.124.94.255 scope global noprefixroute dynamic eth0        valid_lft 78847sec preferred_lft 78847sec 8: wg0: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1420 qdisc noqueue state UNKNOWN group default qlen 1000     link/none      inet 30.0.0.1/24 scope global wg0        valid_lft forever preferred_lft forever

Kiểm tra kết nối VPN

wg

Kết quả

[root@vpnserver ~]# wg interface: wg0   public key: 1D4UVRkKVRufEXT2YcVmiUpV+r2io9iwSSB+uwh33GE=   private key: (hidden)   listening port: 51820  peer: JDllguLLGT1uctWL2uLK72XRU53XikLG5KY1HsebSFs=   endpoint: 321.321.321.321:45728   allowed ips: 30.0.0.0/24   latest handshake: 47 seconds ago   transfer: 388 B received, 300 B sent

Tới đây, Peer client đã kết nối thành công tới Server VPN

Thực hiện tại Client

Kiểm tra các interface hiện có

ip a

Kết quả

[root@client1099 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00     inet 127.0.0.1/8 scope host lo        valid_lft forever preferred_lft forever     inet6 ::1/128 scope host         valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000     link/ether 52:54:00:a0:f8:ad brd ff:ff:ff:ff:ff:ff     inet 10.10.10.99/24 brd 10.10.10.255 scope global noprefixroute eth0        valid_lft forever preferred_lft forever     inet6 fe80::5054:ff:fea0:f8ad/64 scope link noprefixroute         valid_lft forever preferred_lft forever 7: wg-client0: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1420 qdisc noqueue state UNKNOWN group default qlen 1000     link/none      inet 30.0.0.2/24 scope global wg-client0        valid_lft forever preferred_lft forever     inet6 fe80::2b19:6952:1135:d007/64 scope link flags 800         valid_lft forever preferred_lft forever

Kiểm tra kết nối VPN

wg

Kết quả

[root@client1099 ~]# wg interface: wg-client0   public key: JDllguLLGT1uctWL2uLK72XRU53XikLG5KY1HsebSFs=   private key: (hidden)   listening port: 47074   fwmark: 0xca6c  peer: 1D4UVRkKVRufEXT2YcVmiUpV+r2io9iwSSB+uwh33GE=   endpoint: 123.123.123.123:51820   allowed ips: 0.0.0.0/0   latest handshake: 1 minute, 5 seconds ago   transfer: 300 B received, 420 B sent   persistent keepalive: every 25 seconds

Kiểm tra địa chỉ IP Public, client sẽ sử dụng ra Interface

curl ipinfo.io

Kết quả

[root@client1099 ~]# curl ipinfo.io {   "ip": "123.123.123.123",   "city": "Cần Thơ",   "region": "Can Tho",   "country": "VN",   "loc": "10.0371,105.7883",   "org": "AS131353 NhanHoa Software company"   "postal": "94150",   "timezone": "Asia/Ho_Chi_Minh",   "readme": "https://ipinfo.io/missingauth" }

Lưu ý, tại đây chúng ta thấy được Gateway client đã được redirect tới VPN Server

Tới đây, mình đã hướng dẫn xong các bạn cách cấu hình Wireguard VPN cơ bản trên CentOS 7.

Nguồn

https://www.wireguard.com/quickstart/

https://www.linuxbabe.com/centos/wireguard-vpn-server-centos

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

BigBlueButton + Moodle : Sự kết hợp hoàn hảo cho các đơn vị đào tạo trực tuyến.

Đào tạo trực tuyến – E Learning đang là giải pháp bắt buộc đối với các đơn vị đào...
30/12/2020

Jitsi [Part 4] – Hướng dẫn các thao tác sử dụng trong phòng họp trực tuyến

Trong bài viết trước, Onet đã hướng dẫn cách tạo và thiết lập cơ bản cho phòng họp trên Jitsi. Hôm...
30/12/2020

CentOS Versus Ubuntu

Your server needs a stable operating system that you can depend on, and both CentOS and Ubuntu are popular choices that...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

SỰ KHÁC BIỆT GIỮA RESIDENTIAL PROXY VÀ PROXY DATACENTER
17/02/2024

Mua Proxy v6 US Private chạy PRE, Face, Insta, Gmail
07/01/2024

Mua shadowsocks và hướng dẫn sữ dụng trên window
05/01/2024

Tại sao Proxy Socks lại được ưa chuộng hơn Proxy HTTP?
04/01/2024

Mua thuê proxy v4 nuôi zalo chất lượng cao, kinh nghiệm tránh quét tài khoản zalo
02/01/2024