2020-08-05 17:14:30 -04:00
<!--
2020-12-03 06:29:52 -05:00
Title: How to self-host hardened strongSwan IKEv2/IPsec VPN server for iOS and macOS
Description: Learn how to self-host hardened strongSwan IKEv2/IPsec VPN server for iOS and macOS.
2020-08-05 17:14:30 -04:00
Author: Sun Knudsen < https: / / github . com / sunknudsen >
Contributors: Sun Knudsen < https: / / github . com / sunknudsen >
2020-09-10 10:07:07 -04:00
Reviewers:
2020-08-05 17:14:30 -04:00
Publication date: 2020-07-31T12:39:56.680Z
2020-11-10 07:13:35 -05:00
Listed: true
2020-08-05 17:14:30 -04:00
-->
2020-12-03 06:29:52 -05:00
# How to self-host hardened strongSwan IKEv2/IPsec VPN server for iOS and macOS
2020-08-05 17:14:30 -04:00
2021-02-11 17:44:27 -05:00
[](https://www.youtube.com/watch?v=HY3F_vHuTFQ "How to self-host hardened strongSwan IKEv2/IPsec VPN server for iOS and macOS")
2020-08-17 19:34:17 -04:00
2020-12-10 07:25:27 -05:00
> Heads-up: when following this guide on servers with upstream IPv4-only networks (which is totally fine if one knows what one is doing), it’ s likely IPv6 traffic will leak on iOS when clients are connected to carriers or ISPs running dual stack (IPv4 + IPv6) networks. Leaks can be mitigated on iOS (cellular-only) and on macOS by following this [guide](../how-to-disable-ipv6-on-ios-cellular-only-and-macos).
2020-08-05 17:14:30 -04:00
## Requirements
2020-08-10 19:36:08 -04:00
- Virtual private server (VPS) or dedicated server running Debian 10 (buster) with public IPv4 address
2020-08-05 17:14:30 -04:00
- Computer running macOS Mojave or Catalina
- Phone running iOS 12 or 13
2020-08-10 19:36:08 -04:00
## Caveats
- When copy/pasting commands that start with `$` , strip out `$` as this character is not part of the command
- When copy/pasting commands that start with `cat << "EOF"` , select all lines at once (from `cat << "EOF"` to `EOF` inclusively) as they are part of the same (single) command
2020-08-05 17:14:30 -04:00
## Guide
2020-12-05 10:14:29 -05:00
### Step 1: create SSH key pair (on Mac)
2020-08-05 17:14:30 -04:00
When asked for file in which to save key, enter `vpn-server` .
2020-11-11 17:44:16 -05:00
When asked for passphrase, use output from `openssl rand -base64 24` (and store passphrase in password manager).
2020-08-16 09:26:06 -04:00
Use `vpn-server.pub` public key when setting up server.
2020-08-05 17:14:30 -04:00
```console
2021-05-10 08:25:40 +02:00
$ mkdir ~/.ssh
2020-08-14 15:23:58 -04:00
2020-08-05 17:14:30 -04:00
$ cd ~/.ssh
2020-08-14 15:23:58 -04:00
2020-08-05 17:14:30 -04:00
$ ssh-keygen -t rsa -C "vpn-server"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/sunknudsen/.ssh/id_rsa): vpn-server
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in vpn-server.
Your public key has been saved in vpn-server.pub.
The key fingerprint is:
2020-12-10 07:25:27 -05:00
SHA256:KJ8pRZUCVtFh5JEUprW+iFolSYJoA4KxdIcK2puBQaE vpn-server
2020-08-05 17:14:30 -04:00
The key's randomart image is:
+---[RSA 3072]----+
2020-12-10 07:25:27 -05:00
|*=..++o=@= |
|Xo.o. .B+o |
|E*o . o.o |
|+.+o o o |
| ++ + S |
| o B + . |
| + = . |
| o . |
| . |
2020-08-05 17:14:30 -04:00
+----[SHA256]-----+
2020-08-16 09:26:06 -04:00
$ cat vpn-server.pub
2020-12-10 07:25:27 -05:00
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDACoPiTWsEeNyp/aGvCgeAU2qryRDPWr6MniXMLmZCSw0p68IkppYDLC+xDJCNIdwe8X4d892ROvEpCc7JVVfSDhF5lTS8qMJPMlmcJHjVRBwd/+VItD386LlltQMNtl0v8D2NY9ho+6yWJPuMBggu2zMe6ubnNaiJmdenqWktjawru0HsoRmz33jtNSk12e/yFw9xyXK9cYbrCBj17Gcts7mvhndHB0bfeSSozDx2cd/QRw3RE088AZpyUCVG3UpfyhcZK62btT/OaPTBkIYiLst7VuPtNwm7tAVDdy+JoFN+/mPlnQIG+Dmn36IFlK8gBTIu7ahR2omKYBuYfQwfqE24QTfHsVEYIR/fI1EHg0yAu+QTtyv8pUX7vs6qgQVYLVMdPsO510SOZyfNvLQfPmCCk5yif53V74x9Ft56ja92ktw/IXgal48LUNNybO4l0Q8Jf1j2DvTaq6VGBxqE3p6pcDlaNyWGFitdvBzPJrGc/hdJ7HLdDQS47xQM3SM= vpn-server
2020-08-05 17:14:30 -04:00
```
2020-08-10 19:36:08 -04:00
### Step 2: log in to server as root
2020-08-05 17:14:30 -04:00
Replace `185.193.126.203` with IP of server.
2021-03-02 12:58:46 -05:00
When asked for passphrase, enter passphrase from [step 1 ](#step-1-create-ssh-key-pair-on-mac ).
2020-12-05 10:14:29 -05:00
2020-08-05 17:14:30 -04:00
```shell
ssh root@185 .193.126.203 -i ~/.ssh/vpn-server
```
2020-12-03 06:29:52 -05:00
### Step 3: disable root bash history
```shell
echo "HISTFILESIZE=0" >> ~/.bashrc
source ~/.bashrc
```
### Step 4: set root password
When asked for password, use output from `openssl rand -base64 24` (and store password in password manager).
```shell
passwd
```
### Step 5: create vpn-server-admin user
2020-08-05 17:14:30 -04:00
2020-11-11 17:44:16 -05:00
When asked for password, use output from `openssl rand -base64 24` (and store password in password manager).
All other fields are optional, press < kbd > enter< / kbd > to skip them and then press < kbd > Y< / kbd > .
2020-08-05 17:14:30 -04:00
2020-08-10 19:36:08 -04:00
```console
$ adduser vpn-server-admin
Adding user `vpn-server-admin' ...
Adding new group `vpn-server-admin' (1000) ...
Adding new user `vpn-server-admin' (1000) with group ` vpn-server-admin' ...
Creating home directory `/home/vpn-server-admin' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for vpn-server-admin
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] Y
```
2020-12-03 06:29:52 -05:00
### Step 6: copy root `authorized_keys` file to vpn-server-admin home folder
2020-08-05 17:14:30 -04:00
```shell
mkdir /home/vpn-server-admin/.ssh
cp /root/.ssh/authorized_keys /home/vpn-server-admin/.ssh/authorized_keys
chown -R vpn-server-admin:vpn-server-admin /home/vpn-server-admin/.ssh
```
2020-12-03 06:29:52 -05:00
### Step 7: log out
2020-08-05 17:14:30 -04:00
```shell
exit
```
2020-12-03 06:29:52 -05:00
### Step 8: log in as vpn-server-admin
2020-08-05 17:14:30 -04:00
Replace `185.193.126.203` with IP of server.
2021-03-02 12:58:46 -05:00
When asked for passphrase, enter passphrase from [step 1 ](#step-1-create-ssh-key-pair-on-mac ).
2020-12-05 10:14:29 -05:00
2020-08-05 17:14:30 -04:00
```shell
2020-08-17 16:44:39 -04:00
ssh vpn-server-admin@185 .193.126.203 -i ~/.ssh/vpn-server
2020-08-05 17:14:30 -04:00
```
2020-12-03 06:29:52 -05:00
### Step 9: disable vpn-server-admin bash history
```shell
sed -i -E 's/^HISTSIZE=/#HISTSIZE =/' ~/.bashrc
sed -i -E 's/^HISTFILESIZE=/#HISTFILESIZE =/' ~/.bashrc
echo "HISTFILESIZE=0" >> ~/.bashrc
source ~/.bashrc
```
### Step 10: switch to root
2020-08-05 17:14:30 -04:00
When asked, enter root password.
```shell
su -
```
2020-12-03 06:29:52 -05:00
### Step 11: disable root login and password authentication
2020-08-05 17:14:30 -04:00
```shell
sed -i -E 's/(#)?PermitRootLogin (prohibit-password|yes)/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i -E 's/(#)?PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
systemctl restart ssh
```
2020-12-03 06:29:52 -05:00
### Step 12: update APT index and upgrade packages
2020-08-05 17:14:30 -04:00
2020-11-12 11:42:14 -05:00
#### Update APT index
2020-08-11 07:16:17 -04:00
2020-08-05 17:14:30 -04:00
```shell
apt update
2020-08-11 07:16:17 -04:00
```
#### Upgrade packages
```shell
2020-08-05 17:14:30 -04:00
apt upgrade -y
```
2020-12-03 06:29:52 -05:00
### Step 13: install and configure Vim
2020-08-11 07:16:17 -04:00
#### Install Vim
2020-08-05 17:14:30 -04:00
```shell
apt install -y vim
2020-08-11 07:16:17 -04:00
```
#### Configure Vim
```shell
2020-08-05 17:14:30 -04:00
cat < < "EOF" > ~/.vimrc
set encoding=UTF-8
set termencoding=UTF-8
set nocompatible
set backspace=indent,eol,start
set autoindent
set tabstop=2
set shiftwidth=2
set expandtab
set smarttab
set ruler
set paste
syntax on
EOF
```
2020-12-03 06:29:52 -05:00
### Step 14: set timezone (the following is for Montreal time)
2020-08-05 17:14:30 -04:00
2021-01-31 18:27:57 -05:00
See https://en.wikipedia.org/wiki/List_of_tz_database_time_zones for available timezones.
2020-08-05 17:14:30 -04:00
```shell
timedatectl set-timezone America/Montreal
```
2020-12-10 07:25:27 -05:00
### Step 15: save default network interface as environment variable
2020-08-10 19:36:08 -04:00
```console
$ ip -4 route | grep "default" | awk '{print "STRONGSWAN_INTERFACE="$5}' | tee -a ~/.bashrc
STRONGSWAN_INTERFACE=eth0
$ source ~/.bashrc
```
2020-12-10 07:25:27 -05:00
### Step 16: install cURL and Python, generate and save random IPv6 ULA as environment variable (if network is dual stack)
2020-12-09 07:18:44 -05:00
2020-12-10 07:25:27 -05:00
> Heads-up: only run the following if network is dual stack (IPv4 + IPv6).
2020-08-05 17:14:30 -04:00
2020-08-11 07:16:17 -04:00
#### Install cURL and Python
```shell
2020-12-10 07:24:56 -05:00
apt install -y curl python3
2020-08-11 07:16:17 -04:00
```
2020-12-10 07:25:27 -05:00
#### Generate and save random IPv6 ULA as environment variable
2020-08-11 07:16:17 -04:00
2020-08-05 17:14:30 -04:00
Shout out to [Andrew Ho ](https://gist.github.com/andrewlkho/31341da4f5953b8d977aab368e6280a8 ) for `ulagen.py` .
2020-09-26 07:20:10 -04:00
The following command downloads and runs [ulagen.py ](./ulagen.py ) ([PGP signature ](./ulagen.py.sig ), [PGP public key ](https://sunknudsen.com/sunknudsen.asc )).
2020-08-05 17:14:30 -04:00
```console
2020-12-10 07:24:56 -05:00
$ curl -s https://sunknudsen.com/static/media/privacy-guides/how-to-self-host-hardened-strongswan-ikev2-ipsec-vpn-server-for-ios-and-macos/ulagen.py | python3 | grep "First subnet" | awk '{print "STRONGSWAN_IPV6_ULA="$3}' | tee -a ~/.bashrc
STRONGSWAN_IPV6_ULA=fdba:8ce0:c301::/64
2020-08-10 19:36:08 -04:00
$ source ~/.bashrc
2020-08-05 17:14:30 -04:00
```
2020-12-03 06:29:52 -05:00
### Step 17: install iptables-persistent
2020-08-05 17:14:30 -04:00
When asked to save current IPv4 or IPv6 rules, answer `Yes` .
```shell
apt install -y iptables-persistent
```
2020-12-03 06:29:52 -05:00
### Step 18: configure iptables
2020-08-05 17:14:30 -04:00
```shell
2020-08-10 19:36:08 -04:00
iptables -N SSH_BRUTE_FORCE_MITIGATION
iptables -A SSH_BRUTE_FORCE_MITIGATION -m recent --name SSH --set
2020-10-25 09:14:10 -04:00
iptables -A SSH_BRUTE_FORCE_MITIGATION -m recent --name SSH --update --seconds 300 --hitcount 10 -m limit --limit 1/second --limit-burst 100 -j LOG --log-prefix "iptables[ssh-brute-force]: "
2020-08-11 07:16:17 -04:00
iptables -A SSH_BRUTE_FORCE_MITIGATION -m recent --name SSH --update --seconds 300 --hitcount 10 -j DROP
2020-08-10 19:36:08 -04:00
iptables -A SSH_BRUTE_FORCE_MITIGATION -j ACCEPT
2020-08-05 17:14:30 -04:00
iptables -A INPUT -i lo -j ACCEPT
2020-08-10 19:36:08 -04:00
iptables -A INPUT -p tcp --dport 22 --syn -m conntrack --ctstate NEW -j SSH_BRUTE_FORCE_MITIGATION
2020-08-05 17:14:30 -04:00
iptables -A INPUT -p udp --dport 500 -j ACCEPT
iptables -A INPUT -p udp --dport 4500 -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -s 10.0.2.0/24 -m policy --dir in --pol ipsec --proto esp -j ACCEPT
iptables -A FORWARD -d 10.0.2.0/24 -m policy --dir out --pol ipsec --proto esp -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A OUTPUT -p tcp --dport 53 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -p udp --dport 53 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
2020-08-05 19:34:55 -04:00
iptables -A OUTPUT -p udp --dport 123 -m state --state NEW -j ACCEPT
2020-08-05 17:14:30 -04:00
iptables -A OUTPUT -p tcp --dport 443 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
2020-08-10 19:36:08 -04:00
iptables -t nat -A POSTROUTING -s 10.0.2.0/24 -o $STRONGSWAN_INTERFACE -m policy --pol ipsec --dir out -j ACCEPT
iptables -t nat -A POSTROUTING -s 10.0.2.0/24 -o $STRONGSWAN_INTERFACE -j MASQUERADE
2020-08-05 17:14:30 -04:00
iptables -t mangle -A FORWARD -m policy --pol ipsec --dir in -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1280
iptables -t mangle -A FORWARD -m policy --pol ipsec --dir out -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1280
iptables -P FORWARD DROP
iptables -P INPUT DROP
iptables -P OUTPUT DROP
```
2020-12-10 07:25:27 -05:00
If network is IPv4-only, run:
2020-08-05 17:14:30 -04:00
```shell
ip6tables -P FORWARD DROP
ip6tables -P INPUT DROP
ip6tables -P OUTPUT DROP
```
2020-12-10 07:25:27 -05:00
If network is dual stack (IPv4 + IPv6) run:
2020-08-05 17:14:30 -04:00
```shell
ip6tables -A INPUT -i lo -j ACCEPT
2020-08-10 19:36:08 -04:00
ip6tables -A INPUT -p ipv6-icmp --icmpv6-type destination-unreachable -j ACCEPT
ip6tables -A INPUT -p ipv6-icmp --icmpv6-type packet-too-big -j ACCEPT
ip6tables -A INPUT -p ipv6-icmp --icmpv6-type time-exceeded -j ACCEPT
ip6tables -A INPUT -p ipv6-icmp --icmpv6-type parameter-problem -j ACCEPT
ip6tables -A INPUT -p ipv6-icmp --icmpv6-type router-advertisement -m hl --hl-eq 255 -j ACCEPT
ip6tables -A INPUT -p ipv6-icmp --icmpv6-type neighbor-solicitation -m hl --hl-eq 255 -j ACCEPT
ip6tables -A INPUT -p ipv6-icmp --icmpv6-type neighbor-advertisement -m hl --hl-eq 255 -j ACCEPT
ip6tables -A INPUT -p ipv6-icmp --icmpv6-type redirect -m hl --hl-eq 255 -j ACCEPT
2020-08-05 17:14:30 -04:00
ip6tables -A INPUT -p udp --dport 500 -j ACCEPT
ip6tables -A INPUT -p udp --dport 4500 -j ACCEPT
ip6tables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
2020-08-10 19:36:08 -04:00
ip6tables -A FORWARD -s $STRONGSWAN_IPV6_ULA -m policy --dir in --pol ipsec --proto esp -j ACCEPT
ip6tables -A FORWARD -d $STRONGSWAN_IPV6_ULA -m policy --dir out --pol ipsec --proto esp -j ACCEPT
2020-08-05 17:14:30 -04:00
ip6tables -A OUTPUT -o lo -j ACCEPT
2020-08-10 19:36:08 -04:00
ip6tables -A OUTPUT -p ipv6-icmp --icmpv6-type destination-unreachable -j ACCEPT
ip6tables -A OUTPUT -p ipv6-icmp --icmpv6-type packet-too-big -j ACCEPT
ip6tables -A OUTPUT -p ipv6-icmp --icmpv6-type time-exceeded -j ACCEPT
ip6tables -A OUTPUT -p ipv6-icmp --icmpv6-type parameter-problem -j ACCEPT
ip6tables -A OUTPUT -p ipv6-icmp --icmpv6-type router-solicitation -m hl --hl-eq 255 -j ACCEPT
ip6tables -A OUTPUT -p ipv6-icmp --icmpv6-type neighbour-solicitation -m hl --hl-eq 255 -j ACCEPT
ip6tables -A OUTPUT -p ipv6-icmp --icmpv6-type neighbour-advertisement -m hl --hl-eq 255 -j ACCEPT
2020-08-05 17:14:30 -04:00
ip6tables -A OUTPUT -p tcp --dport 53 -m state --state NEW -j ACCEPT
ip6tables -A OUTPUT -p udp --dport 53 -m state --state NEW -j ACCEPT
ip6tables -A OUTPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
2020-08-05 19:34:55 -04:00
ip6tables -A OUTPUT -p udp --dport 123 -m state --state NEW -j ACCEPT
2020-08-05 17:14:30 -04:00
ip6tables -A OUTPUT -p tcp --dport 443 -m state --state NEW -j ACCEPT
ip6tables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
2020-08-10 19:36:08 -04:00
ip6tables -t nat -A POSTROUTING -s $STRONGSWAN_IPV6_ULA -o $STRONGSWAN_INTERFACE -m policy --pol ipsec --dir out -j ACCEPT
ip6tables -t nat -A POSTROUTING -s $STRONGSWAN_IPV6_ULA -o $STRONGSWAN_INTERFACE -j MASQUERADE
2020-08-05 17:14:30 -04:00
ip6tables -t mangle -A FORWARD -m policy --pol ipsec --dir in -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1280
ip6tables -t mangle -A FORWARD -m policy --pol ipsec --dir out -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1280
ip6tables -P FORWARD DROP
ip6tables -P INPUT DROP
ip6tables -P OUTPUT DROP
```
2020-12-03 06:29:52 -05:00
### Step 19: log out and log in to confirm iptables didn’ t block SSH
2020-08-10 19:36:08 -04:00
#### Log out
2020-08-05 17:14:30 -04:00
```shell
exit
exit
```
2020-08-10 19:36:08 -04:00
#### Log in
2020-08-05 17:14:30 -04:00
2020-08-10 19:36:08 -04:00
Replace `185.193.126.203` with IP of server.
2020-08-05 17:14:30 -04:00
2021-03-02 12:58:46 -05:00
When asked for passphrase, enter passphrase from [step 1 ](#step-1-create-ssh-key-pair-on-mac ).
2020-12-05 10:14:29 -05:00
2020-08-05 17:14:30 -04:00
```shell
2020-08-17 16:44:39 -04:00
ssh vpn-server-admin@185 .193.126.203 -i ~/.ssh/vpn-server
2020-08-05 17:14:30 -04:00
```
2020-08-10 19:36:08 -04:00
#### Switch to root
2020-08-05 17:14:30 -04:00
2020-08-10 19:36:08 -04:00
When asked, enter root password.
2020-08-05 17:14:30 -04:00
```shell
2020-08-10 19:36:08 -04:00
su -
2020-08-05 17:14:30 -04:00
```
2020-12-03 06:29:52 -05:00
### Step 20: make iptables rules persistent
2020-08-05 17:14:30 -04:00
```shell
2020-08-10 19:36:08 -04:00
iptables-save > /etc/iptables/rules.v4
ip6tables-save > /etc/iptables/rules.v6
2020-08-05 17:14:30 -04:00
```
2020-12-03 06:29:52 -05:00
### Step 21: add and enable dummy network interface
2020-08-05 17:14:30 -04:00
2020-08-05 20:38:20 -04:00
If server is configured to use `/etc/network/interfaces` , run:
2020-08-05 17:14:30 -04:00
```shell
cp /etc/network/interfaces /etc/network/interfaces.backup
cat < < "EOF" >> /etc/network/interfaces
auto strongswan0
iface strongswan0 inet static
address 10.0.2.1/24
pre-up ip link add strongswan0 type dummy
EOF
2020-08-05 19:34:55 -04:00
ifup strongswan0
2020-08-05 17:14:30 -04:00
```
2020-08-05 20:38:20 -04:00
If server is configured to use systemd-networkd, run:
```shell
cat < < "EOF" >> /etc/systemd/network/10-strongswan0.netdev
[NetDev]
Name=strongswan0
Kind=dummy
EOF
cat < < "EOF" >> /etc/systemd/network/20-strongswan0.network
[Match]
Name=strongswan0
[Network]
Address=10.0.2.1/24
EOF
2020-08-10 19:36:08 -04:00
systemctl restart systemd-networkd
2020-08-05 20:38:20 -04:00
```
2020-12-03 06:29:52 -05:00
### Step 22: install, configure and restart dnsmasq
2020-08-10 19:36:08 -04:00
#### Install dnsmasq
2020-12-10 07:25:27 -05:00
> Heads-up: please ignore systemd address already in use error (if present).
2020-08-05 17:14:30 -04:00
```shell
apt install -y dnsmasq
```
2020-08-10 19:36:08 -04:00
#### Configure dnsmasq
2020-08-05 17:14:30 -04:00
```shell
cat < < "EOF" > /etc/dnsmasq.d/01-dhcp-strongswan.conf
interface=strongswan0
dhcp-range=10.0.2.10,10.0.2.254,255.255.255.0
port=0
EOF
```
2020-08-10 19:36:08 -04:00
#### Restart dnsmasq
2020-08-05 17:14:30 -04:00
```shell
systemctl restart dnsmasq
```
2020-12-03 06:29:52 -05:00
### Step 23: install strongSwan
2020-08-05 17:14:30 -04:00
2020-12-10 07:25:27 -05:00
> Heads-up: if you are shown an “Old runlevel management superseded” warning, answer “Ok”.
2020-08-05 17:14:30 -04:00
```shell
apt install -y strongswan libcharon-extra-plugins
```
2020-12-03 06:29:52 -05:00
### Step 24: configure strongSwan
2020-08-10 19:36:08 -04:00
2020-12-03 06:29:52 -05:00
#### Find server DNS nameserver(s)
2020-08-11 07:16:17 -04:00
2020-12-03 06:29:52 -05:00
Depending on server configuration, DNS nameserver(s) can be found using one of the following commands (ignore nameservers starting with `127` ).
2020-08-10 19:36:08 -04:00
2020-08-11 07:16:17 -04:00
Fist, run:
```console
$ cat /etc/resolv.conf | grep "nameserver" | awk '{print $2}'
93.95.224.28
93.95.224.29
```
If that doesn’ t output valid nameserver(s), run:
```console
$ cat /etc/network/interfaces | grep "dns-nameservers" | awk '{$1="";$0=$0;} NF=NF'
93.95.224.28 93.95.224.29
```
If that doesn’ t output valid nameserver(s), run:
```console
$ systemd-resolve --status | grep "DNS Servers" | awk '{print $3}'
95.215.19.53
```
2020-12-10 07:25:27 -05:00
#### Set DNS nameservers environment variable
2020-08-11 07:16:17 -04:00
2020-12-03 06:29:52 -05:00
Replace `95.215.19.53` with server DNS nameserver(s).
2020-08-11 07:16:17 -04:00
Separate nameservers using commas with no leading spaces (example: `93.95.224.28,93.95.224.29` ).
2020-08-05 17:14:30 -04:00
2020-08-10 19:36:08 -04:00
```shell
2020-08-11 07:16:17 -04:00
STRONGSWAN_DNS_NAMESERVERS=95.215.19.53
2020-08-10 19:36:08 -04:00
```
2020-08-05 17:14:30 -04:00
2021-04-19 14:55:32 -04:00
#### Backup and overwrite `/etc/ipsec.conf`
2020-08-05 17:14:30 -04:00
```shell
cp /etc/ipsec.conf /etc/ipsec.conf.backup
```
2020-12-10 07:25:27 -05:00
If network is IPv4-only, run:
2020-08-05 17:14:30 -04:00
```shell
2020-08-10 19:36:08 -04:00
cat < < EOF > /etc/ipsec.conf
2020-08-05 17:14:30 -04:00
config setup
charondebug="ike 1, knl 1, cfg 1"
conn ikev2
auto=add
compress=no
type=tunnel
keyexchange=ikev2
fragmentation=yes
forceencaps=yes
ike=aes256gcm16-prfsha512-ecp384!
esp=aes256gcm16-ecp384!
dpdaction=clear
dpddelay=300s
rekey=no
left=%any
leftid=vpn-server.com
2020-08-10 19:36:08 -04:00
leftcert=server.crt
2020-08-05 17:14:30 -04:00
leftsendcert=always
2020-08-05 19:51:52 -04:00
leftsubnet=0.0.0.0/0
2020-08-05 17:14:30 -04:00
right=%any
rightid=%any
rightauth=eap-tls
2020-08-11 07:16:17 -04:00
rightdns=$STRONGSWAN_DNS_NAMESERVERS
2020-08-05 17:14:30 -04:00
rightsourceip=%dhcp
rightsendcert=never
eap_identity=%identity
EOF
```
2020-12-10 07:25:27 -05:00
If network is dual stack (IPv4 + IPv6) run:
2020-08-05 17:14:30 -04:00
```shell
2020-08-10 19:36:08 -04:00
cat < < EOF > /etc/ipsec.conf
2020-08-05 17:14:30 -04:00
config setup
charondebug="ike 1, knl 1, cfg 1"
conn ikev2
auto=add
compress=no
type=tunnel
keyexchange=ikev2
fragmentation=yes
forceencaps=yes
ike=aes256gcm16-prfsha512-ecp384!
esp=aes256gcm16-ecp384!
dpdaction=clear
dpddelay=300s
rekey=no
left=%any
leftid=vpn-server.com
2020-08-10 19:36:08 -04:00
leftcert=server.crt
2020-08-05 17:14:30 -04:00
leftsendcert=always
2020-08-05 19:51:52 -04:00
leftsubnet=0.0.0.0/0,::/0
2020-08-05 17:14:30 -04:00
right=%any
rightid=%any
rightauth=eap-tls
2020-08-11 07:16:17 -04:00
rightdns=$STRONGSWAN_DNS_NAMESERVERS
2020-08-10 19:36:08 -04:00
rightsourceip=%dhcp,$STRONGSWAN_IPV6_ULA
2020-08-05 17:14:30 -04:00
rightsendcert=never
eap_identity=%identity
EOF
```
2021-04-19 14:55:32 -04:00
#### Backup and overwrite `/etc/ipsec.secrets`
2020-08-05 17:14:30 -04:00
```shell
cp /etc/ipsec.secrets /etc/ipsec.secrets.backup
cat < < "EOF" > /etc/ipsec.secrets
2020-08-10 19:36:08 -04:00
: RSA server.key
2020-08-05 17:14:30 -04:00
EOF
```
2021-04-19 14:55:32 -04:00
#### Backup and overwrite `/etc/strongswan.d/charon-logging.conf`
2020-08-05 17:14:30 -04:00
```shell
cp /etc/strongswan.d/charon-logging.conf /etc/strongswan.d/charon-logging.conf.backup
cat < < "EOF" > /etc/strongswan.d/charon-logging.conf
charon {
2020-12-10 07:25:27 -05:00
filelog {
charon {
default = 1
2020-08-05 17:14:30 -04:00
}
2020-12-10 07:25:27 -05:00
}
syslog {
auth {
default = 1
2020-08-05 17:14:30 -04:00
}
2020-12-10 07:25:27 -05:00
}
2020-08-05 17:14:30 -04:00
}
EOF
```
2021-04-19 14:55:32 -04:00
#### Backup and overwrite `/etc/strongswan.d/charon/dhcp.conf`
2020-08-05 17:14:30 -04:00
```shell
cp /etc/strongswan.d/charon/dhcp.conf /etc/strongswan.d/charon/dhcp.conf.backup
cat < < "EOF" > /etc/strongswan.d/charon/dhcp.conf
dhcp {
2020-12-10 07:25:27 -05:00
force_server_address = yes
identity_lease = yes
interface = strongswan0
load = yes
server = 10.0.2.1
2020-08-05 17:14:30 -04:00
}
EOF
```
2020-08-10 19:36:08 -04:00
#### Disable unused plugins
2020-08-05 17:14:30 -04:00
```shell
cd /etc/strongswan.d/charon
2020-08-05 20:38:20 -04:00
sed -i 's/load = yes/load = no/' ./*.conf
sed -i 's/load = no/load = yes/' ./eap-tls.conf ./aes.conf ./dhcp.conf ./farp.conf ./gcm.conf ./hmac.conf ./kernel-netlink.conf ./nonce.conf ./openssl.conf ./pem.conf ./pgp.conf ./pkcs12.conf ./pkcs7.conf ./pkcs8.conf ./pubkey.conf ./random.conf ./revocation.conf ./sha2.conf ./socket-default.conf ./stroke.conf ./x509.conf
2020-12-07 10:03:03 -05:00
cd -
2020-08-05 20:38:20 -04:00
```
2020-08-10 19:36:08 -04:00
#### Backup and edit `/lib/systemd/system/strongswan.service`
2020-08-05 20:38:20 -04:00
```shell
cp /lib/systemd/system/strongswan.service /lib/systemd/system/strongswan.service.backup
sed -i 's/After=network-online.target/After=dnsmasq.service/' /lib/systemd/system/strongswan.service
2020-08-10 19:36:08 -04:00
systemctl daemon-reload
```
2020-12-03 06:29:52 -05:00
### Step 25: create `strongswan-certs` folder
2020-08-10 19:36:08 -04:00
2020-12-10 07:25:27 -05:00
> Heads-up: for security reasons, steps 25 to 29 are done on Mac vs server.
2020-08-17 17:09:12 -04:00
2020-09-27 15:35:48 -04:00
> Heads-up: store `strongswan-certs` folder in a safe place if you wish to issue additional certificates in the future.
2020-08-10 19:36:08 -04:00
```shell
mkdir ~/Desktop/strongswan-certs
cd ~/Desktop/strongswan-certs
2020-08-05 17:14:30 -04:00
```
2020-12-03 06:29:52 -05:00
### Step 26: create OpenSSL config file
2020-08-10 19:36:08 -04:00
2020-12-10 07:25:27 -05:00
#### Set client name environment variable
2020-08-05 17:14:30 -04:00
```shell
2020-12-10 07:25:27 -05:00
STRONGSWAN_CLIENT_NAME=alice
2020-08-05 17:14:30 -04:00
```
2020-08-10 19:36:08 -04:00
#### Create OpenSSL config file
2020-08-05 17:14:30 -04:00
```shell
2020-08-10 19:36:08 -04:00
cat < < EOF > openssl.cnf
2020-08-05 17:14:30 -04:00
[ req ]
distinguished_name = req_distinguished_name
attributes = req_attributes
[ req_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_min = 2
countryName_max = 2
2020-08-10 19:36:08 -04:00
countryName_default = US
2020-08-05 17:14:30 -04:00
0.organizationName = Organization Name (eg, company)
2020-08-10 19:36:08 -04:00
0.organizationName_default = Self-hosted strongSwan VPN
2020-08-05 17:14:30 -04:00
commonName = Common Name (eg, fully qualified host name)
commonName_max = 64
[ req_attributes ]
challengePassword = A challenge password
challengePassword_min = 4
challengePassword_max = 20
[ ca ]
subjectKeyIdentifier = hash
basicConstraints = critical, CA:true
keyUsage = critical, cRLSign, keyCertSign
[ server ]
authorityKeyIdentifier = keyid
2020-08-10 19:36:08 -04:00
subjectAltName = DNS:vpn-server.com
2020-08-05 17:14:30 -04:00
extendedKeyUsage = serverAuth, 1.3.6.1.5.5.8.2.2
[ client ]
authorityKeyIdentifier = keyid
2020-12-10 07:25:27 -05:00
subjectAltName = email:$STRONGSWAN_CLIENT_NAME@vpn -server.com
2020-08-05 17:14:30 -04:00
extendedKeyUsage = serverAuth, 1.3.6.1.5.5.8.2.2
EOF
```
2020-12-10 07:25:27 -05:00
### Step 27: generate certificate authority key and cert
2020-08-05 17:14:30 -04:00
```console
$ openssl genrsa -out ca.key 4096
Generating RSA private key, 4096 bit long modulus
2020-12-10 07:25:27 -05:00
.........................................................................................++
........................................................................................................++
2020-08-05 17:14:30 -04:00
e is 65537 (0x10001)
2020-08-10 19:36:08 -04:00
$ openssl req -x509 -new -nodes -config openssl.cnf -extensions ca -key ca.key -subj "/C=US/O=Self-hosted strongSwan VPN/CN=vpn-server.com" -days 3650 -out ca.crt
2020-08-05 17:14:30 -04:00
```
2020-12-10 07:25:27 -05:00
### Step 28: generate server key, csr and cert
2020-08-05 17:14:30 -04:00
```console
2020-08-10 19:36:08 -04:00
$ openssl genrsa -out server.key 4096
2020-08-05 17:14:30 -04:00
Generating RSA private key, 4096 bit long modulus
2020-12-10 07:25:27 -05:00
.............................................................................................................................................................................................++
....................................................................................................................................++
2020-08-05 17:14:30 -04:00
e is 65537 (0x10001)
2020-08-10 19:36:08 -04:00
$ openssl req -new -config openssl.cnf -extensions server -key server.key -subj "/C=US/O=Self-hosted strongSwan VPN/CN=vpn-server.com" -out server.csr
$ openssl x509 -req -extfile openssl.cnf -extensions server -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 3650 -out server.crt
2020-08-05 17:14:30 -04:00
Signature ok
2020-08-10 19:36:08 -04:00
subject=/C=US/O=Self-hosted strongSwan VPN/CN=vpn-server.com
2020-08-05 17:14:30 -04:00
Getting CA Private Key
```
2020-12-10 07:25:27 -05:00
### Step 29: generate client key, csr, cert and pkcs12
2020-08-05 17:14:30 -04:00
2020-08-10 19:36:08 -04:00
When asked for export password, use output from `openssl rand -base64 24` (and store password in password manager).
2020-08-05 17:14:30 -04:00
```console
2020-12-10 07:25:27 -05:00
$ openssl genrsa -out $STRONGSWAN_CLIENT_NAME.key 4096
2020-08-05 17:14:30 -04:00
Generating RSA private key, 4096 bit long modulus
2020-12-10 07:25:27 -05:00
....................................................................................................................................................................................++
...........................................++
2020-08-05 17:14:30 -04:00
e is 65537 (0x10001)
2020-12-10 07:25:27 -05:00
$ openssl req -new -config openssl.cnf -extensions client -key $STRONGSWAN_CLIENT_NAME.key -subj "/C=US/O=Self-hosted strongSwan VPN/CN=$STRONGSWAN_CLIENT_NAME@vpn -server.com" -out $STRONGSWAN_CLIENT_NAME.csr
2020-08-10 19:36:08 -04:00
2020-12-10 07:25:27 -05:00
$ openssl x509 -req -extfile openssl.cnf -extensions client -in $STRONGSWAN_CLIENT_NAME.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 3650 -out $STRONGSWAN_CLIENT_NAME.crt
2020-08-05 17:14:30 -04:00
Signature ok
2020-12-07 10:03:03 -05:00
subject=/C=US/O=Self-hosted strongSwan VPN/CN=alice@vpn -server.com
2020-08-05 17:14:30 -04:00
Getting CA Private Key
2020-12-10 07:25:27 -05:00
$ openssl pkcs12 -in $STRONGSWAN_CLIENT_NAME.crt -inkey $STRONGSWAN_CLIENT_NAME.key -certfile ca.crt -export -out $STRONGSWAN_CLIENT_NAME.p12
2020-08-05 17:14:30 -04:00
Enter Export Password:
Verifying - Enter Export Password:
```
2020-12-10 07:25:27 -05:00
### Step 30: copy certs and key to server and make private folder root-only.
2020-08-05 17:14:30 -04:00
2020-12-10 07:25:27 -05:00
On Mac, run:
2020-08-05 17:14:30 -04:00
2020-12-10 07:25:27 -05:00
```shell
cat < < EOF
cat < < "_EOF" > /etc/ipsec.d/cacerts/ca.crt
$(cat ca.crt)
_EOF
EOF
```
2020-08-05 17:14:30 -04:00
2020-12-10 07:25:27 -05:00
On server, run output from previous command:
```shell
cat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ty
JW0CAwEAAaNCMEAwHQYDVR0OBBYEFEPDTIrkb2mgz3vsioEMyb4s4GFOMA8GA1Ud
EwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgEGMA0GCSqGSIb3DQEBCwUAA4ICAQA1
bqXeP2ngjGlki4TomYCAKXJJdOoHDIeXzFrPpaDpfza+JP5/VNVuTH0gM774oWgO
JqpD/2ZIVfBdwLjsAGB0vx5of19Y+HpCoTfC4onoG40LJS+2w6OWg14dgiyxOpU8
PhTGqY7Josfo9RwmNeVot0AYqrOIQLERmWm04uJ8ojYherH7qGX0pSU9rHptnG8J
kagP6V9/auH6MXqdgEbstKa66yZKKI84mgDMIu5ExPTC7EXmjCkiAsRu+gJAxCi2
B01xcCT7fYVGSgC+abziFhDJIuaGGgiWPdMiZ+709Pm1XeXAjyTj0JCU8+HsS8oS
olK9ZInvY6qKqA0pnzgohp5NGkmIRZahgZLu40va7wZBhu52cGTfOa2A8nMoltJ+
uxtTS8SaLI1eXdNl/IegN6uy+1BwHhCa1uTwG8tt0ld+UI9S1c6YVum7o5m3dRyj
+UJFTltK9rKMXHLEzMKE/rZEKailY8lADpur4sEsp9vVPmUqUr7Yd5P2p4yDNb+d
ECJH7z7TGc0jK3RteSjEWpZ/rc1yrnJaNrVgwxe6nni58kxd7awdmMGVyL14HGWh
HePRFQM4zSD3SiazdYVPtVX0XyQKL/9rGN2tR2sGilTKC0yBkQ/gHIYOBIJGyzrh
wuMHH9d46B2ks8UzUf7w304bk6RaOqU6794WXoEE0Q==
-----END CERTIFICATE-----
_EOF
```
On Mac, run:
```shell
cat < < EOF
cat < < "_EOF" > /etc/ipsec.d/private/server.key
$(cat server.key)
_EOF
EOF
```
2020-08-05 17:14:30 -04:00
2020-12-10 07:25:27 -05:00
On server, run output from previous command:
```shell
cat < < "_EOF" > /etc/ipsec.d/private/server.key
-----BEGIN RSA PRIVATE KEY-----
MIIJKAIBAAKCAgEAviBI5I0qkJfLYiXS8cMjAx7LxkHnygkXSs4jCZ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.crt)
_EOF
EOF
```
2020-08-05 17:14:30 -04:00
2020-12-10 07:25:27 -05:00
On server, run output from previous command:
```shell
cat < < "_EOF" > /etc/ipsec.d/certs/server.crt
-----BEGIN CERTIFICATE-----
MIIFdjCCA16gAwIBAgIJANL3OkwHc0s+MA0GCSqGSIb3DQEBBQUAMEsxCzAJBgNV
BAYTAlVTMSMwIQYDVQQKDBpTZWxmLWhvc3RlZCBzdHJvbmdTd2FuIFZQTjEXMBUG
A1UEAwwOdnBuLXNlcnZlci5jb20wHhcNMjAxMjA4MjIwMDAyWhcNMzAxMjA2MjIw
MDAyWjBLMQswCQYDVQQGEwJVUzEjMCEGA1UECgwaU2VsZi1ob3N0ZWQgc3Ryb25n
U3dhbiBWUE4xFzAVBgNVBAMMDnZwbi1zZXJ2ZXIuY29tMIICIjANBgkqhkiG9w0B
AQEFAAOCAg8AMIICCgKCAgEAviBI5I0qkJfLYiXS8cMjAx7LxkHnygkXSs4jCZVj
wPB+eoBr4lGrKLe6pwcS4+0xADeVwfR1VGJcpjlGs7epoUxcCGobMJWP87evb93U
/A/3S7latv+BjC/Dl6PumDgan7GrBaWwUWit5COjKm5BcTkR4aav9WWKuuzrG969
Onw8ZaMeQPvqqGHOjKAntCE6X+BGJN7nPnn7d3JXQWbOVrIgMduZsPX1ikzxLb2G
s0QHOdnnlWrZrbJZ5z5rgDZQ5FYChnBNS2i63ptO6eggyjX4fb/wd0hOPVwlqIIP
xt1qDyWAV5CzlH7cAUFfoq8GP1b3ZPmLiCAYwccZEMldpqWOz0xaGDYEsT26q2cb
dgNV2QgkvmLO7AGhf1Cr+XuKwgCESzN0T3gZXdtRm3urxilQCPl9Hbr4EubqOIQa
JUQbauaXadUkqHPhwpI5AiW1qpbQIw9Yq1hLrjuN4iYgPqJ7D0LJ0R+2qQMbkTW8
sCXPcWSu7gA7g+D9/764GV/nAvBxaFyVnNqW8nJM4pCS+26A+gXcPVUCZYfBaih8
xdeYlRiJBSxyQiw1AR1gN9/L//P2kM6YterUg28MiDlyDCF3lvTrqwPzaHsxPkQd
iErHje8MgTJxXoOMk/1WBptzfWngvrM87nXziV7hG8vijWJ4rXFJduYFfRuUK15k
uzMCAwEAAaNdMFswHwYDVR0jBBgwFoAUQ8NMiuRvaaDPe+yKgQzJvizgYU4wGQYD
VR0RBBIwEIIOdnBuLXNlcnZlci5jb20wHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsG
AQUFCAICMA0GCSqGSIb3DQEBBQUAA4ICAQCKzJgEz7WM+Wg8AKk/q3gT45in50yG
YF9i1jbbtgq2QKA261BI6Y8hUSNoRHNNPD8fll3hDbDTxboKFM3zP3BHBTm89uY5
b+bE2mLwvNTYcStw/rhi3I1eYNhIa05LIz+EEcl7EG5lzsOD1jSF7SwN3qGJhOCg
2B71rmyCqLYMELtFLXq46/MNuqg/gGV27Wijg84RsltDIqFK8DrmrnZEa56gPOIs
3zpLs2rxg5ufh8L2eX39gxT7ocNHHlSfbGGoIIvYywY95GuO6WYmmBdbVA7JZlQj
QMYaYs2HQTcYDesqXgdJEN2OPadxwlo9xDezy8UUaqvbXa8emsRCL/BOI6O7yHT/
6Yso0iVlAjSPWB1WYxKfwDJR4G5yvgOk8hdOEo82X5Pkt4BP8fXDuIzysityl0FG
nHgiRCl4AsXC+1502FuwvqBIjPwBtTBS1CvxeH3CBSMwuCqMly+AUkwUaOw8yTIS
UYsVvM7cE3t1EYj70tcGAOZjy4QcodgHE5jXKiKzEw8cZ3tPxd3pOiGEm0l8q2oJ
uWt8HPc++++4v1feUx3Qf2roTTDnK0KNY16wDx/UbHkSNitr62aT+Z+EnvthJ70V
XPO3OQ496jhylWgBapWIQGD8nZgdPgVp3PdrIqzpCF1+NYQl4as8AmJwCKDpu230
uEErfEZ7mAYftQ==
-----END CERTIFICATE-----
_EOF
```
On server, run `chmod -R 600 /etc/ipsec.d/private`
2020-08-05 17:14:30 -04:00
2020-12-03 06:29:52 -05:00
### Step 31: restart strongSwan
2020-08-05 17:14:30 -04:00
```shell
systemctl restart strongswan
```
2020-12-03 06:29:52 -05:00
### Step 32: configure sysctl
2020-08-05 17:14:30 -04:00
2020-12-03 06:29:52 -05:00
#### Backup and edit `/etc/sysctl.conf`
2020-08-05 17:14:30 -04:00
```shell
cp /etc/sysctl.conf /etc/sysctl.conf.backup
2020-08-05 19:34:55 -04:00
sed -i -E 's/#net .ipv4.ip_forward=1/net.ipv4.ip_forward=1/' /etc/sysctl.conf
sed -i -E 's/#net .ipv4.conf.all.accept_redirects = 0/net.ipv4.conf.all.accept_redirects = 0/' /etc/sysctl.conf
sed -i -E 's/#net .ipv4.conf.all.send_redirects = 0/net.ipv4.conf.all.send_redirects = 0/' /etc/sysctl.conf
2020-08-05 17:14:30 -04:00
```
2020-12-10 07:25:27 -05:00
If network is IPv4-only, run:
2020-08-05 17:14:30 -04:00
```shell
cat < < "EOF" >> /etc/sysctl.conf
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
EOF
```
2020-12-10 07:25:27 -05:00
If network is dual stack (IPv4 + IPv6) run:
2020-08-05 17:14:30 -04:00
```shell
sed -i -E 's/#net .ipv6.conf.all.forwarding=1/net.ipv6.conf.all.forwarding=1/' /etc/sysctl.conf
```
2020-08-10 19:36:08 -04:00
#### Reload sysctl
2020-08-05 17:14:30 -04:00
```shell
sysctl -p
```
2020-12-03 06:29:52 -05:00
### Step 33: create VPN profile for iOS and macOS using [Apple Configurator 2](https://support.apple.com/apple-configurator)
2020-08-10 19:36:08 -04:00
2020-09-27 15:35:48 -04:00
> Heads-up: when configuring strongSwan using certs and dnsmasq, two devices cannot use the same provisioning profile simultaneously.
2020-08-05 17:14:30 -04:00
2020-10-10 10:43:57 -04:00
Open “Apple Configurator 2”, then click “File”, then “New Profile”.
2020-08-05 17:14:30 -04:00
2020-10-10 10:43:57 -04:00
In “General”, enter “Self-hosted strongSwan VPN” in “Name”.
2020-08-05 17:14:30 -04:00

2020-12-10 07:25:27 -05:00
In “Certificates”, click “Configure” and select “ca.crt”. Then click “+” and select “alice.p12”. The password is the one from [step 29 ](#step-29-generate-client-key-csr-cert-and-pkcs12 ).
2020-08-05 17:14:30 -04:00

2020-10-10 10:43:57 -04:00
In “VPN”, click “Configure” and enter the settings from the following screenshot (replace `185.193.126.203` with IP of server).
2020-08-05 19:34:55 -04:00
2020-10-10 10:43:57 -04:00
The “Child SA Params” are the same as “IKE SA Params”.
2020-08-05 17:14:30 -04:00

2020-12-07 10:03:03 -05:00
Finally, click “File”, then “Save”, and save file as “alice.mobileconfig”.
2020-08-05 17:14:30 -04:00
2020-12-03 06:29:52 -05:00
### Step 34: add VPN profile to iPhone using Apple Configurator 2
2020-08-05 17:14:30 -04:00
2020-08-05 17:22:20 -04:00
Unlock iPhone, connect it to Mac using USB cable and open Apple Configurator 2.
2020-08-05 17:14:30 -04:00
2020-10-10 10:43:57 -04:00
In “All Devices”, double-click on iPhone, then “Add”, and finally “Profiles”.
2020-08-05 17:14:30 -04:00
2020-12-07 10:03:03 -05:00
Select “alice.mobileconfig” and follow instructions.
2020-08-05 17:14:30 -04:00
2020-10-10 10:43:57 -04:00
On iPhone, open “Settings”, then “Profile Downloaded” and tap “Install”.
2020-09-27 15:35:48 -04:00
**If this steps fails (a recent update to Apple Configurator 2 has introduced a bug), please run the following and try again.**
```shell
2020-12-07 10:03:03 -05:00
sed -i '' '/< key > DNS< \/key > /,/< \/dict > /d' ~/Desktop/strongswan-certs/alice.mobileconfig
2020-09-27 15:35:48 -04:00
```
2020-08-05 17:14:30 -04:00
2020-12-03 06:29:52 -05:00
### Step 35: add VPN profile to Mac
2020-08-10 19:36:08 -04:00
2020-12-07 10:03:03 -05:00
This step is super simple, simply double-click “alice.mobileconfig” and follow instructions.
2020-08-10 19:36:08 -04:00
2020-09-27 15:35:48 -04:00
**If this steps fails (a recent update to Apple Configurator 2 has introduced a bug), please run the following and try again.**
```shell
2020-12-07 10:03:03 -05:00
sed -i '' '/< key > DNS< \/key > /,/< \/dict > /d' ~/Desktop/strongswan-certs/alice.mobileconfig
2020-09-27 15:35:48 -04:00
```
2020-12-03 06:29:52 -05:00
### Step 36: connect to VPN on iPhone or Mac
2020-08-10 19:36:08 -04:00
2020-10-10 10:43:57 -04:00
On iPhone, open “Settings”, then enable “VPN”.
2020-08-10 19:36:08 -04:00
2020-10-10 10:43:57 -04:00
On Mac, open “System Preferences”, click “Network”, then “Self-hosted strongSwan VPN” and finally “Connect” and enable “Show VPN status in menu bar”.
2020-08-10 19:36:08 -04:00
2020-12-03 06:29:52 -05:00
### Step 37: test for leaks
2020-08-11 07:16:17 -04:00
2021-01-31 18:27:57 -05:00
Open Firefox and go to https://ipleak.net/.
2020-08-11 07:16:17 -04:00
2020-12-10 07:25:27 -05:00
Make sure listed IPv4, IPv6 (if network is dual stack) and DNS servers do not match the ones provided by ISP.
2020-08-11 07:16:17 -04:00
2020-12-03 06:29:52 -05:00
### Step 38: create additional provisioning profiles
2020-12-10 07:25:27 -05:00
Repeat steps [26 ](#step-26-create-openssl-config-file ), [29 ](#step-29-generate-client-key-csr-cert-and-pkcs12 ) and [33 ](#step-33-create-vpn-profile-for-ios-and-macos-using-apple-configurator-2 ).
2020-08-10 19:36:08 -04:00
2020-12-03 06:29:52 -05:00
👍