Skip to content

Instantly share code, notes, and snippets.

Last active Aug 4, 2022
What would you like to do?
Wireguard and iptables restrictions for multiple users

Wireguard and iptables restrictions for multiple users

If you don't know what Wireguard is, well, you should. It's fast, easy to setup and highly configurable. We will configure Wireguard for multiple users with various restrictions using iptables.


This should fit most setups (not mine though 😉)

  • LAN network: ( =>
  • LAN DNS server address:
  • Wireguard is installed (kernel and tools) on a Linux host (it should also work on other platforms though).
  • The Linux host address:
  • The Linux host main interface: enp4s0 (find it with ip a)

Initial server setup

  1. Ensure your iptables firewall has its FORWARD table policy set to DROP:

    iptables -P FORWARD -j DROP
  2. Generate a Wireguard private key

    echo "Private key: $(wg genkey)"
    Private key: OM5BUrGVAswOm/r8asLtdUgJB8rrXflD6TVFL5aGAHk=
  3. Create a file /etc/wireguard/wg0.conf (or any other name than wg0) with content:

    Address =
    ListenPort = 51820
    PrivateKey = OM5BUrGVAswOm/r8asLtdUgJB8rrXflD6TVFL5aGAHk=
    PostUp = /etc/wireguard/
    PostDown = /etc/wireguard/

    Don't forget to replace the PrivateKey value with the one you generated

  4. Create a file /etc/wireguard/ with content:

    # Add a WIREGUARD_wg0 chain to the FORWARD chain
    iptables -N $CHAIN_NAME
    iptables -A FORWARD -j $CHAIN_NAME
    # Accept related or established traffic
    iptables -A $CHAIN_NAME -o $WIREGUARD_INTERFACE -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
    # Accept traffic from any Wireguard IP address connected to the Wireguard server
    # Drop everything else coming through the Wireguard interface
    # Return to FORWARD chain
    iptables -A $CHAIN_NAME -j RETURN
  5. Create a file /etc/wireguard/ with content:

    # Remove and delete the WIREGUARD_wg0 chain
    iptables -D FORWARD -j $CHAIN_NAME
    iptables -F $CHAIN_NAME
    iptables -X $CHAIN_NAME

First admin client

Let's setup a client with full access to Internet and your LAN through Wireguard.

  1. Install Wireguard on your client device

  2. On your client device, create a configuration file client.conf with content:

    Address =
    DNS =
    AllowedIPs =, ::/0
    Endpoint =
    PersistentKeepalive = 25
  3. Replace in the configuration above YOUR_SERVER_PUBLIC_KEY with the key shown using wg show wg0 public-key on your server. For example: p6aGalk69yCM8vNhbvC5mEH/HhJr1c8f55UaeJSChX0=.

  4. Generate keys for your client:

    priv=`wg genkey` && printf "Private key: $priv\nPublic key: `echo "$priv" | wg pubkey`\n" && unset -v priv
    Private key: 2L4L8YwusK4Ot4jVoo/1wwQfLAeRM6kJ/WWxzfnWKm4=
    Public key: GmVyaj+K36xEk7ko/8jijMB9XX9dFgi4mJxsAEFMHmA=
  5. Replace in client.conf YOUR_CLIENT_PRIVATE_KEY with the private key just generated above.

  6. Use the public key shown above to add the following block to /etc/wireguard/wg0.conf on your server:

    # Your first admin client
    PublicKey = GmVyaj+K36xEk7ko/8jijMB9XX9dFgi4mJxsAEFMHmA=
    AllowedIPs =
  7. On your server, start Wireguard: wg-quick up wg0.

  8. You should now be able to connect to the Wireguard server from your client. You can check on the server with wg and it should show a latest handshake line.

LAN only user

Let's add a user who should only have access to the LAN.

  1. Repeat steps 1 to 5 from the First admin client section above.

  2. Use the public key shown in step 4 to add the following block to /etc/wireguard/wg0.conf on your server:

    # LAN only user
    PublicKey = 7GneIV/Od7WEKfTpIXr+rTzPf3okaQTBwsfBs5Eqiyw=
    AllowedIPs =
  3. Shutdown Wireguard: wg-quick down wg0

  4. Modify /etc/wireguard/

    • Limit full access to our first admin client only by changing:



    • Add a new line to allow our new user to LAN access only

      iptables -A $CHAIN_NAME -s -i $WIREGUARD_INTERFACE -d -j ACCEPT
  5. Start Wireguard: wg-quick up wg0

  6. Note that the client should set its AllowedIPs =, ::/0 to AllowedIPs = so it tunnels only to when trying to reach an address on the LAN.

Restrict the user to a port

Let's re-use the user we previously created. Let's only allow him to access port 445 (Samba) on a server for example.

  1. Shutdown Wireguard: wg-quick down wg0

  2. Modify



    iptables -A $CHAIN_NAME -s -i $WIREGUARD_INTERFACE -d -p tcp --dport 445 -j ACCEPT
  3. Start Wireguard: wg-quick up wg0

Restrict the user to limited Web browsing only

That's useful for our friends who want to stream restricted content on Netflix without allowing them on your LAN 😉

  1. Shutdown Wireguard: wg-quick down wg0

  2. Modify

    iptables -A $CHAIN_NAME -s -i $WIREGUARD_INTERFACE -d -p tcp --dport 445 -j ACCEPT


    # DNS
    iptables -A $CHAIN_NAME -s -i $WIREGUARD_INTERFACE -d -p udp --dport 53 -j ACCEPT
    # Drop traffic to your any private IP address
    iptables -A $CHAIN_NAME -s -i $WIREGUARD_INTERFACE -d,, -j DROP
    # Accept outgoing connections to HTTP(S) ports to any IP address (public because of rule above)
    iptables -A $CHAIN_NAME -s -i $WIREGUARD_INTERFACE -d -p tcp -m multiport --dports 80,443 -j ACCEPT
  3. Start Wireguard: wg-quick up wg0

  4. Don't forget to set back the client to AllowedIPs =, ::/0

More rules

Feel free to comment, I'll try my best to make you a rule 😉


Copy link

Darkyere commented May 19, 2022

Is it possible in your exambles above.

To add a config that will allow acces only too specific and multiple IP's and Ports.
While at the same time allowing the person to reach the internet?

I would like to allow friends acces to certain computers on certain ports.
But i would allso like them to be able to acces the public internet while retraining acces to my LAN.

Best Regards,

Copy link

int2018 commented May 30, 2022


I am not sure if this is the right place to ask, so please be kind if not.

I have setup Wireguard on a Raspberry and it handles the remote access to the Qnap NAS in the network. So far everything is good.
The "problem" is, that multiple users can open the same file (e.g. Excel sheet) without a warning. This means there is the potential of data loss since the last one who saves the file "wins".
Is this something that can be handled in wireguard?

Thanks in advance

Copy link

curiousercreative commented May 30, 2022

The "problem" is, that multiple users can open the same file (e.g. Excel sheet) without a warning. This means there is the potential of data loss since the last one who saves the file "wins". Is this something that can be handled in wireguard?

@int2018 No, nothing Wireguard can do about this. You can look into file locking. Some applications (Adobe InDesign, darktable, KeePassXC) will handle this, but I believe SMB and NFS can perhaps be configured with similar functionality.

Copy link

ImDaBigBoss commented Jun 18, 2022

Hello, is there a way I can stop the wireguard server starting a connection to one of the clients?
Say I have these clients and these permissions:

  • Server: Can start connections to
  • Admin client: Can start connections to everything
  • Client: Can start connecions to

Thanks in advance

Copy link

nikkadim commented Aug 4, 2022

@qdm12 my wireguard stopped working some time ago and it's taken me until now to debug it. Turns out, there's an always reject forwarded packets policy last and since we're appending our chain, my forwarded packets are rejected before our chain is entered and packets are accepted. To resolve this for myself, I've changed iptables -A FORWARD -j $CHAIN_NAME to iptables -I FORWARD -j $CHAIN_NAME which will insert rather than append (first rather than last).

Copy link

int2018 commented Aug 4, 2022

Hi all,

my wireguard is all set up and running since months (wireguard on raspi, qnap nas/ smb server and windows clients.
It is working well (despite the issue mentioned before) but I still experience a strange behavior.
When I work with Windows Explorer and doubleclick on a folder it opens, I open another folder, and so on. So far so good, everything works as expected.
But then (no exact time) I doubleclick another folder or I doubleclick to open a file (e.g. pdf) the connection "hangs". I doubleclick again and the Explorer window tile changes to ... (not responding). Again after some time (seems 5-10 seconds) the connection is working again and the click I made earlier gets handled, the folder opens or the pdf file or whatever I clicked.
This is just a description of what I see on the (remote) client side. I talked to colleagues and they have the same phenomena. And it also happens when there is only one client connected. It does not seem to be a problem of too much load, the raspberry seems mostly in idle.

So the question is how/where can I check/tweak/debug what the problem is?
Can it be related to the Qnap NAS with SMB (standard setup) ?

Thanks for a hint...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment