2014-05-23 06:42:09 -04:00
---
2015-04-10 16:17:45 -04:00
layout: doc
2014-05-23 06:42:09 -04:00
title: VPN
2016-02-20 16:15:30 -05:00
permalink: /doc/vpn/
2015-10-11 03:04:59 -04:00
redirect_from:
2016-02-20 16:15:30 -05:00
- /doc/privacy/vpn/
2015-10-28 18:14:40 -04:00
- /en/doc/vpn/
2015-10-11 03:04:59 -04:00
- /doc/VPN/
- /wiki/VPN/
2014-05-23 06:42:09 -04:00
---
2014-05-23 08:47:14 -04:00
How To make a VPN Gateway in Qubes
2016-09-24 16:12:37 -04:00
==================================
2014-05-23 06:42:09 -04:00
2017-04-02 13:19:38 -04:00
Although setting up a VPN connection is not by itself Qubes specific, Qubes includes a number of tools that can make the client-side setup of your VPN more versatile and secure. This document is a Qubes-specific outline for choosing the type of VM to use, and shows how to prepare a ProxyVM for either NetworkManager or a set of fail-safe VPN scripts.
2014-05-23 06:42:09 -04:00
2017-04-01 15:09:04 -04:00
Please refer to your guest OS and VPN service documentation when considering the specific steps and parameters for your connection(s); The relevant documentation for the Qubes default guest OS (Fedora) is [Establishing a VPN Connection. ](https://docs.fedoraproject.org/en-US/Fedora/23/html/Networking_Guide/sec-Establishing_a_VPN_Connection.html )
2014-05-23 06:42:09 -04:00
### NetVM
2016-01-31 22:11:18 -05:00
The simplest case is to set up a VPN connection using the NetworkManager service inside your NetVM. Because the NetworkManager service is already started, you are ready to set up your VPN connection. However this has some disadvantages:
2014-05-23 06:42:09 -04:00
2018-01-30 21:41:56 -05:00
- You have to place (and probably save) your VPN credentials inside the NetVM, which is directly connected to the outside world
- All your AppVMs which are connected to the NetVM will be connected to the VPN (by default)
2014-05-23 06:42:09 -04:00
2014-05-23 10:16:18 -04:00
### AppVM
2016-01-31 22:11:18 -05:00
While the NetworkManager service is not started here (for a good reason), you can configure any kind of VPN client in your AppVM as well. However this is only suggested if your VPN client has special requirements.
2014-05-23 10:16:18 -04:00
2014-05-23 06:42:09 -04:00
### ProxyVM
2017-04-01 15:09:04 -04:00
One of the best unique features of Qubes OS is its special type of VM called a ProxyVM. The special thing is that your AppVMs see this as a NetVM (or uplink), and your NetVMs see it as a downstream AppVM. Because of this, you can place a ProxyVM between your AppVMs and your NetVM. This is how the default sys-firewall VM functions.
2014-05-23 06:42:09 -04:00
2015-07-23 07:19:16 -04:00
Using a ProxyVM to set up a VPN client gives you the ability to:
2014-05-23 06:42:09 -04:00
2018-01-30 21:41:56 -05:00
- Separate your VPN credentials from your NetVM.
- Separate your VPN credentials from your AppVM data.
- Easily control which of your AppVMs are connected to your VPN by simply setting it as a NetVM of the desired AppVM.
2014-05-23 06:42:09 -04:00
2016-09-15 08:14:54 -04:00
Set up a ProxyVM as a VPN gateway using NetworkManager
------------------------------------------------------
2014-05-23 06:42:09 -04:00
2018-01-30 21:41:56 -05:00
1. Create a new VM, name it, click the ProxyVM radio button, and choose a color and template.
2014-05-23 08:15:00 -04:00
2018-01-30 21:41:56 -05:00
![Create\_New\_VM.png ](/attachment/wiki/VPN/Create_New_VM.png )
2014-05-23 08:15:00 -04:00
2018-01-30 21:41:56 -05:00
2. Add the `network-manager` service to this new VM.
2014-05-23 08:15:00 -04:00
2018-01-30 21:41:56 -05:00
![Settings-services.png ](/attachment/wiki/VPN/Settings-services.png )
2014-05-23 08:18:21 -04:00
2018-01-30 21:41:56 -05:00
3. Set up your VPN as described in the NetworkManager documentation linked above.
2014-05-23 08:15:00 -04:00
2018-06-26 06:37:28 -04:00
4. (Optional) Make your VPN start automatically.
2018-06-26 06:29:55 -04:00
Edit `/rw/config/rc.local` and add these lines:
```bash
2018-06-29 21:02:42 -04:00
# Automatically connect to the VPN once Internet is up
2018-12-28 01:42:53 -05:00
while ! ping -c 1 -W 1 1.1.1.1; do
2018-07-27 23:17:23 -04:00
sleep 1
done
PWDFILE="/rw/config/NM-system-connections/secrets/passwd-file.txt"
nmcli connection up file-vpn-conn passwd-file $PWDFILE
2018-06-26 06:29:55 -04:00
```
You can find the actual "file-vpn-conn" in `/rw/config/NM-system-connections/` .
Create directory `/rw/config/NM-system-connections/secrets/` (You can put your `*.crt` and `*.pem` files here too).
Create a new file `/rw/config/NM-system-connections/secrets/passwd-file.txt` :
```
vpn.secrets.password:XXXXXXXXXXXXXX
```
And substitute "XXXXXXXXXXXXXX" for the actual password.
The contents of `passwd-file.txt` may differ depending on your VPN settings. See the [documentation for `nmcli up` ](https://www.mankier.com/1/nmcli#up ).
2018-06-26 06:37:28 -04:00
5. (Optional) Make the network fail-close for the AppVMs if the connection to the VPN breaks.
2018-06-26 06:29:55 -04:00
Edit `/rw/config/qubes-firewall-user-script` and add these lines:
```bash
2018-07-27 23:17:23 -04:00
# Block forwarding of connections through upstream network device
# (in case the vpn tunnel breaks)
2018-06-26 06:29:55 -04:00
iptables -I FORWARD -o eth0 -j DROP
iptables -I FORWARD -i eth0 -j DROP
2019-02-20 17:32:32 -05:00
ip6tables -I FORWARD -o eth0 -j DROP
ip6tables -I FORWARD -i eth0 -j DROP
2018-06-26 06:29:55 -04:00
```
6. Configure your AppVMs to use the new VM as a NetVM.
2015-11-04 15:14:02 -05:00
2018-01-30 21:41:56 -05:00
![Settings-NetVM.png ](/attachment/wiki/VPN/Settings-NetVM.png )
2016-01-26 05:01:14 -05:00
2018-06-26 06:29:55 -04:00
7. Optionally, you can install some [custom icons ](https://github.com/Zrubi/qubes-artwork-proxy-vpn ) for your VPN
2016-05-25 16:55:27 -04:00
2016-09-15 08:14:54 -04:00
Set up a ProxyVM as a VPN gateway using iptables and CLI scripts
----------------------------------------------------------------
2016-05-25 16:55:27 -04:00
2018-01-30 21:41:56 -05:00
This method is more involved than the one above, but has anti-leak features that also make the connection _fail closed_ should it be interrupted.
2020-02-07 01:53:19 -05:00
It has been tested with Fedora 30 and Debian 10 templates.
2016-05-25 16:55:27 -04:00
2020-02-07 01:53:19 -05:00
Before proceeding, you will need to download a copy of your VPN provider's configuration file(s) and have your VPN login information handy.
1. Create a new VM, name it, choose "provides network", and choose a color and template.
2016-06-06 05:39:39 -04:00
2018-01-30 21:41:56 -05:00
![Create\_New\_VM.png ](/attachment/wiki/VPN/Create_New_VM.png )
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
Note: Do not enable NetworkManager in the ProxyVM, as it can interfere with the scripts' DNS features.
If you enabled NetworkManager or used other methods in a previous attempt, do not re-use the old ProxyVM...
Create a new one according to this step.
2018-01-29 08:10:53 -05:00
2020-02-07 01:53:19 -05:00
If your choice of TemplateVM doesn't already have the VPN client software, you'll need to install the software in the template before proceeding. The 'openvpn' package comes installed in the Fedora template, and in Debian it can be installed with the following command:
sudo apt-get install openvpn
2018-01-30 21:41:56 -05:00
Disable any auto-starting service that comes with the software package.
For example for OpenVPN.
2018-01-29 08:10:53 -05:00
2018-01-25 16:11:34 -05:00
sudo systemctl disable openvpn.service
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
2. Set up and test the VPN client.
Make sure the VPN VM and its TemplateVM is not running.
Run a terminal (CLI) in the VPN VM -- this will start the VM.
2020-02-17 00:34:55 -05:00
Then create a new `/rw/config/vpn` folder with:
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
sudo mkdir /rw/config/vpn
2018-01-25 16:11:34 -05:00
2020-02-07 01:53:19 -05:00
Copy your VPN configuration files to `/rw/config/vpn` .
Your VPN config file should be named `openvpn-client.ovpn` so you can use the scripts below as is without modification. Otherwise you would have to replace the file name. Files accompanying the main config such as `*.crt` and `*.pem` should also be placed in the `/rw/config/vpn` folder.
2018-01-25 16:11:34 -05:00
2020-02-07 01:53:19 -05:00
Check or modify configuration file contents using a text editor:
2018-01-25 16:11:34 -05:00
2020-02-07 01:53:19 -05:00
sudo gedit /rw/config/vpn/openvpn-client.ovpn
2018-01-25 16:11:34 -05:00
2020-02-07 01:53:19 -05:00
Files referenced in `openvpn-client.ovpn` should not use absolute paths such as `/etc/...` .
The config should route all traffic through your VPN's interface after a connection is created; For OpenVPN the directive for this is `redirect-gateway def1` .
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
Make sure it already includes or add:
2018-01-25 16:11:34 -05:00
redirect-gateway def1
2020-02-07 01:53:19 -05:00
The VPN client may not be able to prompt you for credentials when connecting to the server, so we'll add a reference to a file containing the VPN username and password.
For example for OpenVPN, add or modify `auth-user-pass` like so:
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
auth-user-pass pass.txt
2018-01-29 08:10:53 -05:00
2020-02-07 01:53:19 -05:00
Save the `/rw/config/vpn/openvpn-client.ovpn` file.
Now make sure a `/rw/config/vpn/pass.txt` file actually exists.
2018-01-29 08:10:53 -05:00
2020-02-07 01:53:19 -05:00
sudo gedit /rw/config/vpn/pass.txt
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
Add:
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
username
password
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
Replace `username` and `password` with your actual username and password.
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
**Test your client configuration:**
Run the client from a CLI prompt in the 'vpn' folder, preferably as root.
For example:
2018-01-29 08:10:53 -05:00
2018-01-25 16:11:34 -05:00
sudo openvpn --cd /rw/config/vpn --config openvpn-client.ovpn
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
Watch for status messages that indicate whether the connection is successful and test from another VPN VM terminal window with `ping` .
2018-01-25 16:11:34 -05:00
2020-02-17 00:34:55 -05:00
ping 1.1.1.1
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
`ping` can be aborted by pressing the two keys `ctrl` + `c` at the same time.
DNS may be tested at this point by replacing addresses in `/etc/resolv.conf` with ones appropriate for your VPN (although this file will not be used when setup is complete).
Diagnose any connection problems using resources such as client documentation and help from your VPN service provider.
Proceed to the next step when you're sure the basic VPN connection is working.
3. Create the DNS-handling script.
2020-02-07 01:53:19 -05:00
sudo gedit /rw/config/vpn/qubes-vpn-handler.sh
2018-01-30 21:41:56 -05:00
2020-02-07 01:53:19 -05:00
Add the following:
2018-01-30 21:41:56 -05:00
~~~
#!/bin/bash
set -e
export PATH="$PATH:/usr/sbin:/sbin"
case "$1" in
up)
# To override DHCP DNS, assign DNS addresses to 'vpn_dns' env variable before calling this script;
# Format is 'X.X.X.X Y.Y.Y.Y [...]'
if [[ -z "$vpn_dns" ]] ; then
# Parses DHCP foreign_option_* vars to automatically set DNS address translation:
for optionname in ${!foreign_option_*} ; do
option="${!optionname}"
unset fops; fops=($option)
if [ ${fops[1]} == "DNS" ] ; then vpn_dns="$vpn_dns ${fops[2]}" ; fi
done
fi
iptables -t nat -F PR-QBS
if [[ -n "$vpn_dns" ]] ; then
# Set DNS address translation in firewall:
for addr in $vpn_dns; do
iptables -t nat -A PR-QBS -i vif+ -p udp --dport 53 -j DNAT --to $addr
iptables -t nat -A PR-QBS -i vif+ -p tcp --dport 53 -j DNAT --to $addr
done
su - -c 'notify-send "$(hostname): LINK IS UP." --icon=network-idle' user
else
su - -c 'notify-send "$(hostname): LINK UP, NO DNS!" --icon=dialog-error' user
fi
;;
down)
su - -c 'notify-send "$(hostname): LINK IS DOWN !" --icon=dialog-error' user
;;
esac
~~~
Save the script.
Make it executable.
2018-01-25 16:11:34 -05:00
sudo chmod +x /rw/config/vpn/qubes-vpn-handler.sh
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
4. Configure client to use the DNS handling script. Using openvpn as an example, edit the config.
2018-01-25 16:11:34 -05:00
2020-02-07 01:53:19 -05:00
sudo gedit /rw/config/vpn/openvpn-client.ovpn
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
Add the following.
2018-01-25 16:11:34 -05:00
2018-01-30 21:41:56 -05:00
script-security 2
up 'qubes-vpn-handler.sh up'
down 'qubes-vpn-handler.sh down'
2016-09-15 08:14:54 -04:00
2018-01-30 21:41:56 -05:00
Remove other instances of lines starting with `script-security` , `up` or `down` should there be any others.
Save the script.
**Restart the client and test the connection again** ...this time from an AppVM!
2016-09-15 08:14:54 -04:00
2018-01-30 21:41:56 -05:00
5. Set up iptables anti-leak rules.
Edit the firewall script.
2018-01-29 08:10:53 -05:00
2020-02-07 01:53:19 -05:00
sudo gedit /rw/config/qubes-firewall-user-script
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
Clear out the existing lines and add:
~~~
#!/bin/bash
# Block forwarding of connections through upstream network device
# (in case the vpn tunnel breaks):
iptables -I FORWARD -o eth0 -j DROP
iptables -I FORWARD -i eth0 -j DROP
2018-07-01 16:57:45 -04:00
ip6tables -I FORWARD -o eth0 -j DROP
ip6tables -I FORWARD -i eth0 -j DROP
2018-01-30 21:41:56 -05:00
2020-02-17 00:34:55 -05:00
# Accept traffic to VPN
iptables -P OUTPUT ACCEPT
2018-01-30 21:41:56 -05:00
iptables -F OUTPUT
# Add the `qvpn` group to system, if it doesn't already exist
if ! grep -q "^qvpn:" /etc/group ; then
2016-09-15 08:14:54 -04:00
groupadd -rf qvpn
sync
2018-01-30 21:41:56 -05:00
fi
sleep 2s
2020-02-17 00:34:55 -05:00
# Block non-VPN traffic to clearnet
iptables -I OUTPUT -o eth0 -j DROP
2018-01-30 21:41:56 -05:00
# Allow traffic from the `qvpn` group to the uplink interface (eth0);
# Our VPN client will run with group `qvpn` .
iptables -I OUTPUT -p all -o eth0 -m owner --gid-owner qvpn -j ACCEPT
~~~
2016-09-15 08:14:54 -04:00
2018-01-30 21:41:56 -05:00
Save the script.
Make it executable.
2018-01-25 16:11:34 -05:00
sudo chmod +x /rw/config/qubes-firewall-user-script
2016-09-15 08:14:54 -04:00
2018-01-30 21:41:56 -05:00
5. Set up the VPN's autostart.
2016-06-06 05:39:39 -04:00
2020-02-07 01:53:19 -05:00
sudo gedit /rw/config/rc.local
2018-01-25 16:11:34 -05:00
2018-01-30 21:41:56 -05:00
Clear out the existing lines and add:
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
~~~
#!/bin/bash
VPN_CLIENT='openvpn'
VPN_OPTIONS='--cd /rw/config/vpn/ --config openvpn-client.ovpn --daemon'
su - -c 'notify-send "$(hostname): Starting $VPN_CLIENT..." --icon=network-idle' user
groupadd -rf qvpn ; sleep 2s
sg qvpn -c "$VPN_CLIENT $VPN_OPTIONS"
~~~
2018-01-25 16:11:34 -05:00
2018-01-30 21:41:56 -05:00
If you are using anything other than OpenVPN, change the `VPN_CLIENT` and `VPN_OPTIONS` variables to match your VPN software.
Save the script.
Make it executable.
2018-01-25 17:50:43 -05:00
2018-03-01 23:02:44 -05:00
sudo chmod +x /rw/config/rc.local
2018-01-29 08:10:53 -05:00
2018-01-30 21:41:56 -05:00
6. Restart the new VM!
The link should then be established automatically with a popup notification to that effect.
2016-09-15 08:14:54 -04:00
Usage
-----
2016-05-25 16:55:27 -04:00
2016-09-15 08:14:54 -04:00
Configure your AppVMs to use the VPN VM as a NetVM...
2016-05-25 16:55:27 -04:00
2016-09-17 05:49:39 -04:00
![Settings-NetVM.png ](/attachment/wiki/VPN/Settings-NetVM.png )
2016-05-25 16:55:27 -04:00
2020-02-17 00:34:55 -05:00
If you want to update your TemplateVMs through the VPN, you can enable the `qubes-updates-proxy` service for your new VPN VM and configure the [qubes-rpc policy ](https://www.qubes-os.org/doc/software-update-domu/#updates-proxy ).
2017-01-11 22:50:31 -05:00
2017-01-11 20:38:02 -05:00
2016-09-15 08:14:54 -04:00
Troubleshooting
---------------
2016-06-06 05:39:39 -04:00
2016-09-15 08:14:54 -04:00
* Always test your basic VPN connection before adding scripts.
* Test DNS: Ping a familiar domain name from an appVM. It should print the IP address for the domain.
* Use `iptables -L -v` and `iptables -L -v -t nat` to check firewall rules. The latter shows the critical PR-QBS chain that enables DNS forwarding.