CentOS Stream 9
Sponsored Link

WireGuard : Configure Client (CentOS)2022/07/08

 
Install WireGuard which is the simple yet fast and modern VPN software.
This example is based on the environment like follows.
First, it needs to configure IP masquerade setting on your router that UDP packets to global IP address of WireGuard server from WireGuard client via internet are forwared to local IP address of WireGuard server.
  +------------------------+
  | [  WireGuard Server  ] |172.16.100.1 (VPN IP)
  |      dlp.srv.world     +--------+
  |                        |wg0     |
  +-----------+------------+        |
        enp1s0|10.0.0.30/24         |
              |                     |
              |       Local Network |
       +------+-----+               |
-------|  Router#1  |---------------|-----
       +------+-----+               |
              |                     |
    Internet  |  Internet           |
              |                     |
       +------+-----+               |
-------|  Router#2  |---------------|-----
       +------+-----+               |
              |       Local Network |
              |                     |
        enp1s0|192.168.10.30/24     |
  +-----------+------------+        |
  |  [ WireGuard Client ]  |wg0     |
  |                        +--------+
  |                        |172.16.100.5 (VPN IP)
  +------------------------+

[1]
Transfer files or notify contents of [Private key for client] and [Public key for server] generated on WireGuard server to target Client computer.
[2] Install WireGuard.
[root@client ~]#
dnf -y install wireguard-tools
[3] Configure WireGuard.
[root@client ~]#
umask 077
# create a new config
# [wg0.conf] ⇒ [(VPN interface name).conf]
# VPN interface name ⇒ any name you like

[root@client ~]#
vi /etc/wireguard/wg0.conf
[Interface]
# specify private key for client generated on WireGuard server
PrivateKey = COqnE0Bl+XF87zvlyGi6GZxaQDqLNacMn9qNY8s3emY=
# IP address for VPN interface
Address = 172.16.100.5

[Peer]
# specify public key for server generated on WireGuard server
PublicKey = Vac7AdQZGSkhUhcKZjhhlEBrGV2046QtmRriZsEgonI=
# IP addresses you allow to connect
# on the example below, set WireGuard server's VPN IP address and real local network
AllowedIPs = 172.16.100.1, 10.0.0.0/24
# specify server's global IP address:port
# (acutually, example of IP below is for private range, replace to your own global IP)
EndPoint = 172.29.10.100:51820

# start up VPN interface

[root@client ~]#
wg-quick up wg0

[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 172.16.100.5 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] ip -4 route add 172.16.100.1/32 dev wg0
[#] ip -4 route add 10.0.0.0/24 dev wg0

[root@client ~]#
ip addr

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: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:f6:f1:b2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.30/24 brd 192.168.0.255 scope global noprefixroute enp1s0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fef6:f1b2/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: wg0: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1420 qdisc noqueue state UNKNOWN group default qlen 1000
    link/none
    inet 172.16.100.5/32 scope global wg0
       valid_lft forever preferred_lft forever

# confirm connection state

[root@client ~]#
wg show

interface: wg0
  public key: Y6i2512HGnH2mQLYrCUZo2TGOkhKWcaR83eUn3cmVFI=
  private key: (hidden)
  listening port: 50819

peer: Vac7AdQZGSkhUhcKZjhhlEBrGV2046QtmRriZsEgonI=
  endpoint: 172.29.10.100:51820
  allowed ips: 172.16.100.1/32, 10.0.0.0/24
[4] After VPN session is successfully established, Verify access to local network of WireGuard server.
[root@client ~]#
ping -c 3 10.0.0.30

PING 10.0.0.30 (10.0.0.30) 56(84) bytes of data.
64 bytes from 10.0.0.30: icmp_seq=1 ttl=64 time=1.66 ms
64 bytes from 10.0.0.30: icmp_seq=2 ttl=64 time=1.71 ms
64 bytes from 10.0.0.30: icmp_seq=3 ttl=64 time=1.74 ms

--- 10.0.0.30 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 1.658/1.702/1.739/0.033 ms

[root@client ~]#
ping -c 3 10.0.0.10

PING 10.0.0.10 (10.0.0.10) 56(84) bytes of data.
64 bytes from 10.0.0.10: icmp_seq=1 ttl=63 time=1.31 ms
64 bytes from 10.0.0.10: icmp_seq=2 ttl=63 time=2.09 ms
64 bytes from 10.0.0.10: icmp_seq=3 ttl=63 time=1.87 ms

--- 10.0.0.10 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 1.311/1.758/2.090/0.328 ms
Matched Content