Configure a WireGuard client by using nmcli

You can configure a WireGuard client by creating a connection profile in NetworkManager. Use this method to let NetworkManager manage the WireGuard connection.

Before you begin

  • You have generated the public and private key for both the server and client.
  • You know the following information:
    • The private key of the client
    • The static tunnel IP addresses and subnet masks of the client
    • The public key of the server
    • The static tunnel IP addresses and subnet masks of the server

About this task

This procedure assumes the following settings:

  • Client:
    • Private key: aPUcp5vHz8yMLrzk8SsDyYnV33IhE/k20e52iKJFV0A=
    • Tunnel IPv4 address: 192.0.2.2/24
    • Tunnel IPv6 address: 2001:db8:1::2/64
  • Server:
    • Public key: UtjqCJ57DeAscYKRfp7cFGiQqdONRn69u249Fa4O6BE=
    • Tunnel IPv4 address: 192.0.2.1/24
    • Tunnel IPv6 address: 2001:db8:1::1/64

Procedure

  1. Add a NetworkManager WireGuard connection profile:
    # nmcli connection add type wireguard con-name client-wg0 ifname wg0

    This command creates a profile named client-wg0 and assigns the virtual interface wg0 to it.

  2. Optional: Configure NetworkManager so that it does not automatically start the client-wg connection:
    # nmcli connection modify client-wg0 autoconnect no
  3. Set the tunnel IPv4 address and subnet mask of the client:
    # nmcli connection modify client-wg0 ipv4.method manual ipv4.addresses 192.0.2.2/24
  4. Set the tunnel IPv6 address and subnet mask of the client:
    # nmcli connection modify client-wg0 ipv6.method manual ipv6.addresses 2001:db8:1::2/64
  5. If you want to route all traffic through the tunnel, set the tunnel IP addresses of the server as the default gateway:
    # nmcli connection modify client-wg0 ipv4.gateway 192.0.2.1 ipv6.gateway 2001:db8:1::1

    Routing all traffic through the tunnel requires that you set, in a later step, the allowed-ips on this client to 0.0.0.0/0;::/0.

    Note that routing all traffic through the tunnel can impact the connectivity to other hosts based on the server routing and firewall configuration.

  6. Add the client’s private key to the connection profile:
    # nmcli connection modify client-wg0 wireguard.private-key "aPUcp5vHz8yMLrzk8SsDyYnV33IhE/k20e52iKJFV0A="
  7. Add peer connection configurations for each server that you want to allow to communicate with this client:
    # nmcli connection modify client-wg0 wireguard.peers 'kEyMNyYhpmn9zPQ4m7iGmU9G7nooS9eeUBe0Y0wO0gM= endpoint=server.example.com:51820 allowed-ips=0.0.0.0/0 ;::/0'

    The wireguard.peers parameter defines the peer section of the client in the NetworkManager connection profile. Use 0.0.0.0/0;::/0; in allowed-ips to allow any remote IPv4 and IPv6 address to communicate with this client. Use this setting to route all traffic through the tunnel and use the WireGuard server as default gateway.

  8. Reactivate the client-wg0 connection:
    # nmcli connection up client-wg0
  9. If you use this host in a network with DHCP or Stateless Address Autoconfiguration (SLAAC), the connection can be vulnerable to being redirected. For details and mitigation steps, see Assigning a VPN connection to a dedicated routing table to prevent the connection from bypassing the tunnel.

Results

  1. Ping the IP addresses of the server:
    # ping 192.0.2.1
    # ping6 2001:db8:1::1
  2. Display the interface configuration of the wg0 device:
    # wg show wg0
    interface: wg0
      public key: bnwfQcC8/g2i4vvEqcRUM2e6Hi3Nskk6G9t4r26nFVM=
      private key: (hidden)
      listening port: 51820
    
    peer: UtjqCJ57DeAscYKRfp7cFGiQqdONRn69u249Fa4O6BE=
      endpoint: server.example.com:51820
      allowed ips: 192.0.2.1/32, 2001:db8:1::1/128
      latest handshake: 1 minute, 41 seconds ago
      transfer: 824 B received, 1.01 KiB sent
      persistent keepalive: every 20 seconds

    To display the private key in the output, use the WG_HIDE_KEYS=never wg show wg0 command.

    Note that the output has only the latest handshake and transfer entries if you have already sent traffic through the VPN tunnel.

  3. Display the IP configuration of the wg0 device:
    # ip address show wg0
    10: wg0: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1420 qdisc noqueue state UNKNOWN group default qlen 1000
        link/none
        inet 192.0.2.2/24 brd 192.0.2.255 scope global noprefixroute wg0
           valid_lft forever preferred_lft forever
        inet6 2001:db8:1::2/64 scope global noprefixroute
           valid_lft forever preferred_lft forever
        inet6 fe80::73d9:6f51:ea6f:863e/64 scope link noprefixroute
           valid_lft forever preferred_lft forever