OpenVPN:Raspberry Pi Auto Connect: Difference between revisions
Added troubleshooting guide: Raspberry Pi OpenVPN Auto-Connect Setup |
Major update - troubleshooting guide: Raspberry Pi OpenVPN Auto-Connect Setup (44 sections) |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
This guide walks through setting up a Raspberry Pi to automatically connect to the OpenVPN server when it boots. This assumes the Raspberry Pi is being set up from scratch with only the OS installed. | This guide walks through setting up a Raspberry Pi to automatically connect to the OpenVPN server when it boots. This assumes the Raspberry Pi is being set up from scratch with only the OS installed. | ||
| Line 6: | Line 4: | ||
* Raspberry Pi with Raspberry Pi OS installed (Raspberry Pi OS Lite or Desktop) | * Raspberry Pi with Raspberry Pi OS installed (Raspberry Pi OS Lite or Desktop) | ||
* SSH access to the Raspberry Pi (or physical access with keyboard/monitor) | * SSH access to the Raspberry Pi (or physical access with keyboard/monitor) | ||
* OpenVPN client configuration file (<code>.ovpn</code>) from the server administrator | * OpenVPN client configuration file (<code>.ovpn</code>) from the server administrator | ||
* Network connectivity on the Raspberry Pi | * Network connectivity on the Raspberry Pi | ||
| Line 36: | Line 37: | ||
* Client certificate | * Client certificate | ||
* Client private key | * Client private key | ||
* CA certificate | * CA certificate | ||
* TLS-Crypt key | * TLS-Crypt key | ||
* Server connection details | * Server connection details | ||
| Line 137: | Line 142: | ||
<pre> | <pre> | ||
= Auto-reconnect settings = | == Auto-reconnect settings == | ||
keepalive 10 120 | keepalive 10 120 | ||
persist-key | persist-key | ||
| Line 146: | Line 151: | ||
'''Explanation''': | '''Explanation''': | ||
* <code>keepalive 10 120</code>: Sends a ping every 10 seconds, restarts if no response for 120 seconds | * <code>keepalive 10 120</code>: Sends a ping every 10 seconds, restarts if no response for 120 seconds | ||
* <code>persist-key</code>: Keeps trying to read key files if they're temporarily unavailable | * <code>persist-key</code>: Keeps trying to read key files if they're temporarily unavailable | ||
* <code>persist-tun</code>: Keeps the TUN/TAP interface open across restarts | * <code>persist-tun</code>: Keeps the TUN/TAP interface open across restarts | ||
* <code>resolv-retry infinite</code>: Keeps trying to resolve the server hostname if DNS fails | * <code>resolv-retry infinite</code>: Keeps trying to resolve the server hostname if DNS fails | ||
| Line 187: | Line 195: | ||
<pre class="lang-bash"> | <pre class="lang-bash"> | ||
= Ping the VPN server (adjust IP based on your VPN subnet) = | ==== Ping the VPN server (adjust IP based on your VPN subnet) ==== | ||
ping -c 4 10.8.0.1 | ping -c 4 10.8.0.1 | ||
</pre> | </pre> | ||
| Line 216: | Line 224: | ||
<pre class="lang-bash"> | <pre class="lang-bash"> | ||
= Check service status = | ==== Check service status ==== | ||
sudo systemctl status openvpn-client@client.service | sudo systemctl status openvpn-client@client.service | ||
= Check VPN interface = | == Check VPN interface == | ||
ip addr show tun0 | ip addr show tun0 | ||
= Check routing = | == Check routing == | ||
ip route show | ip route show | ||
</pre> | </pre> | ||
| Line 245: | Line 253: | ||
<pre class="lang-bash"> | <pre class="lang-bash"> | ||
sudo systemctl enable NetworkManager-wait-online.service | sudo systemctl enable NetworkManager-wait-online.service | ||
=== Or for systemd-networkd: === | |||
sudo systemctl enable systemd-networkd-wait-online.service | sudo systemctl enable systemd-networkd-wait-online.service | ||
</pre> | </pre> | ||
| Line 354: | Line 362: | ||
<pre> | <pre> | ||
= Redirect all traffic through VPN (remove or comment this line) = | ==== Redirect all traffic through VPN (remove or comment this line) ==== | ||
= redirect-gateway def1 = | == redirect-gateway def1 == | ||
</pre> | </pre> | ||
| Line 382: | Line 390: | ||
* Automatically connect to the OpenVPN server on boot | * Automatically connect to the OpenVPN server on boot | ||
* Automatically reconnect if the connection drops | * Automatically reconnect if the connection drops | ||
* Maintain the VPN connection as long as the device is powered on | * Maintain the VPN connection as long as the device is powered on | ||
'''Key Files''': | '''Key Files''': | ||
* Configuration: <code>/etc/openvpn/client/client.conf</code> | * Configuration: <code>/etc/openvpn/client/client.conf</code> | ||
* Service: <code>openvpn-client@client.service</code> | * Service: <code>openvpn-client@client.service</code> | ||
* Logs: <code>journalctl -u openvpn-client@client.service</code> | * Logs: <code>journalctl -u openvpn-client@client.service</code> | ||
'''Useful Commands''': | '''Useful Commands''': | ||
* Start VPN: <code>sudo systemctl start openvpn-client@client.service</code> | * Start VPN: <code>sudo systemctl start openvpn-client@client.service</code> | ||
* Stop VPN: <code>sudo systemctl stop openvpn-client@client.service</code> | * Stop VPN: <code>sudo systemctl stop openvpn-client@client.service</code> | ||
* Restart VPN: <code>sudo systemctl restart openvpn-client@client.service</code> | * Restart VPN: <code>sudo systemctl restart openvpn-client@client.service</code> | ||
* Check Status: <code>sudo systemctl status openvpn-client@client.service</code> | * Check Status: <code>sudo systemctl status openvpn-client@client.service</code> | ||
* View Logs: <code>sudo journalctl -u openvpn-client@client.service -f</code> | * View Logs: <code>sudo journalctl -u openvpn-client@client.service -f</code> | ||
Latest revision as of 13:44, 1 January 2026
This guide walks through setting up a Raspberry Pi to automatically connect to the OpenVPN server when it boots. This assumes the Raspberry Pi is being set up from scratch with only the OS installed.
Prerequisites
[edit]- Raspberry Pi with Raspberry Pi OS installed (Raspberry Pi OS Lite or Desktop)
- SSH access to the Raspberry Pi (or physical access with keyboard/monitor)
- OpenVPN client configuration file (
.ovpn) from the server administrator
- Network connectivity on the Raspberry Pi
Step 1: Initial System Setup
[edit]1.1 Update System Packages
[edit]First, ensure your Raspberry Pi is up to date:
sudo apt update sudo apt upgrade -y
1.2 Install Required Packages
[edit]Install OpenVPN and other necessary tools:
sudo apt install -y openvpn network-manager-openvpn resolvconf
Note: The network-manager-openvpn package is optional but can be useful for GUI-based management. The resolvconf package helps manage DNS resolution when connected to the VPN.
Step 2: Obtain OpenVPN Configuration File
[edit]You need to obtain the .ovpn configuration file for your Raspberry Pi from the server administrator. This file contains:
- Client certificate
- Client private key
- CA certificate
- TLS-Crypt key
- Server connection details
Common file locations on the server: /root/<client-name>.ovpn
2.1 Transfer Configuration File to Raspberry Pi
[edit]You can transfer the file using one of these methods:
Method 1: Using SCP (from your local machine)
scp <username>@<raspberry-pi-ip>:/path/to/client.ovpn ~/client.ovpn
Method 2: Using SFTP
sftp <username>@<raspberry-pi-ip> put /path/to/client.ovpn ~/client.ovpn exit
Method 3: Copy and paste (if you have the file contents)
Create the file manually:
nano ~/client.ovpn
Paste the contents and save (Ctrl+X, then Y, then Enter).
Step 3: Install Configuration File
[edit]3.1 Copy Configuration to System Directory
[edit]Copy the .ovpn file to /etc/openvpn/client/:
sudo cp ~/client.ovpn /etc/openvpn/client/client.conf
Note: OpenVPN looks for .conf files in /etc/openvpn/client/, so we rename it to client.conf. If you have multiple VPN configurations, you can use descriptive names like raspberry-pi.conf.
3.2 Set Proper Permissions
[edit]Ensure the configuration file has the correct permissions:
sudo chmod 600 /etc/openvpn/client/client.conf sudo chown root:root /etc/openvpn/client/client.conf
Step 4: Configure Auto-Start on Boot
[edit]4.1 Enable OpenVPN Service
[edit]Enable the OpenVPN client service to start automatically on boot:
sudo systemctl enable openvpn-client@client.service
Note: The service name format is openvpn-client@<config-name>.service, where <config-name> is the name of your .conf file without the extension. Since we named it client.conf, the service is openvpn-client@client.service.
4.2 Start the Service
[edit]Start the OpenVPN service immediately (without rebooting):
sudo systemctl start openvpn-client@client.service
4.3 Verify Service Status
[edit]Check that the service is running:
sudo systemctl status openvpn-client@client.service
You should see output indicating the service is active and running.
Step 5: Configure Auto-Reconnect
[edit]OpenVPN should automatically reconnect if the connection drops, but we can enhance this by modifying the configuration file.
5.1 Add Auto-Reconnect Options
[edit]Edit the configuration file:
sudo nano /etc/openvpn/client/client.conf
Add these lines at the end of the file (if they're not already present):
== Auto-reconnect settings == keepalive 10 120 persist-key persist-tun resolv-retry infinite
Explanation:
keepalive 10 120: Sends a ping every 10 seconds, restarts if no response for 120 seconds
persist-key: Keeps trying to read key files if they're temporarily unavailable
persist-tun: Keeps the TUN/TAP interface open across restarts
resolv-retry infinite: Keeps trying to resolve the server hostname if DNS fails
Save and exit (Ctrl+X, then Y, then Enter).
5.2 Restart the Service
[edit]Apply the changes:
sudo systemctl restart openvpn-client@client.service
Step 6: Verify Connection
[edit]6.1 Check VPN Interface
[edit]Verify that the VPN interface (typically tun0) is up:
ip addr show tun0
You should see output showing the VPN interface with an IP address in the VPN subnet (e.g., 10.8.0.x).
6.2 Check Routing
[edit]Verify that traffic is being routed through the VPN:
ip route show
You should see routes indicating traffic is going through the tun0 interface.
6.3 Test Connectivity
[edit]Test connectivity to the VPN server:
==== Ping the VPN server (adjust IP based on your VPN subnet) ==== ping -c 4 10.8.0.1
6.4 Check OpenVPN Logs
[edit]View OpenVPN logs to ensure everything is working:
sudo journalctl -u openvpn-client@client.service -f
Press Ctrl+C to exit the log viewer.
Step 7: Test Auto-Start on Boot
[edit]7.1 Reboot the Raspberry Pi
[edit]Reboot to verify the VPN connects automatically:
sudo reboot
7.2 Verify After Reboot
[edit]After the Raspberry Pi reboots, SSH back in and verify:
==== Check service status ==== sudo systemctl status openvpn-client@client.service == Check VPN interface == ip addr show tun0 == Check routing == ip route show
Troubleshooting
[edit]VPN Not Connecting on Boot
[edit]If the VPN doesn't connect automatically on boot, check:
- Service Status:
sudo systemctl status openvpn-client@client.service
- Service Logs:
sudo journalctl -u openvpn-client@client.service -n 50
- Network Timing: The VPN service might be starting before the network is ready. Check if
network-online.targetis enabled:
sudo systemctl enable NetworkManager-wait-online.service === Or for systemd-networkd: === sudo systemctl enable systemd-networkd-wait-online.service
VPN Interface Not Appearing
[edit]If tun0 doesn't appear:
- Check if OpenVPN is running:
ps aux | grep openvpn
- Check configuration file syntax:
sudo openvpn --config /etc/openvpn/client/client.conf --verb 4
- Verify TUN/TAP module is loaded:
lsmod | grep tun
If not loaded, load it:
sudo modprobe tun
DNS Resolution Issues
[edit]If DNS isn't working after connecting:
- Check DNS settings:
cat /etc/resolv.conf
- Install resolvconf if not already installed:
sudo apt install resolvconf
- Restart the OpenVPN service:
sudo systemctl restart openvpn-client@client.service
Connection Drops Frequently
[edit]If the connection drops frequently:
- Check network stability:
ping -c 10 <vpn-server-ip>
- Review keepalive settings in the configuration file
- Check firewall rules that might be blocking OpenVPN traffic
- Review server logs on the VPN server for any issues
Permission Denied Errors
[edit]If you see permission errors:
- Verify file permissions:
ls -l /etc/openvpn/client/client.conf
Should show-rw-------(600) and owned byroot:root
- Check directory permissions:
ls -ld /etc/openvpn/client/
Advanced Configuration
[edit]Multiple VPN Configurations
[edit]If you need multiple VPN configurations:
- Copy additional
.ovpnfiles to/etc/openvpn/client/with different names:
sudo cp ~/vpn2.ovpn /etc/openvpn/client/vpn2.conf
- Enable the additional service:
sudo systemctl enable openvpn-client@vpn2.service sudo systemctl start openvpn-client@vpn2.service
Custom DNS Servers
[edit]To use custom DNS servers when connected to the VPN, add to your configuration file:
dhcp-option DNS 8.8.8.8 dhcp-option DNS 8.8.4.4
Route Specific Traffic Through VPN
[edit]To route only specific traffic through the VPN (split tunneling), modify the configuration file to remove or comment out:
==== Redirect all traffic through VPN (remove or comment this line) ==== == redirect-gateway def1 ==
Then add specific routes:
route 192.168.1.0 255.255.255.0
Security Considerations
[edit]- Protect Configuration Files: The
.ovpnfile contains private keys. Ensure it has restrictive permissions (600) and is owned by root.
- Regular Updates: Keep your Raspberry Pi OS and OpenVPN client updated:
sudo apt update && sudo apt upgrade -y
- Firewall: Consider configuring a firewall (ufw) to allow only necessary traffic.
- Monitor Logs: Regularly check OpenVPN logs for any suspicious activity.
Summary
[edit]After completing these steps, your Raspberry Pi will:
- Automatically connect to the OpenVPN server on boot
- Automatically reconnect if the connection drops
- Maintain the VPN connection as long as the device is powered on
Key Files:
- Configuration:
/etc/openvpn/client/client.conf
- Service:
openvpn-client@client.service
- Logs:
journalctl -u openvpn-client@client.service
Useful Commands:
- Start VPN:
sudo systemctl start openvpn-client@client.service
- Stop VPN:
sudo systemctl stop openvpn-client@client.service
- Restart VPN:
sudo systemctl restart openvpn-client@client.service
- Check Status:
sudo systemctl status openvpn-client@client.service
- View Logs:
sudo journalctl -u openvpn-client@client.service -f