Paladinz2k

Enable VPN port forwarding

Aug 2nd, 2026 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. sudo iptables -t nat -A PREROUTING -d 217.154.59.19 -p tcp --dport PORT:PORT -j DNAT --to-destination 10.8.0.2:PORT-PORT
  2. sudo iptables -A FORWARD -p tcp -d 10.8.0.2 --dport PORT:PORT -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
  3. sudo iptables -t nat -A POSTROUTING -d 10.8.0.2 -p tcp --dport PORT:PORT -j MASQUERADE
  4. sudo apt update
  5. sudo apt install iptables-persistent
  6. sudo iptables-save | sudo tee /etc/iptables/rules.v4
  7.  
  8. ## Prerequisites
  9. Replace the following placeholders with your actual values:
  10. * **`1.2.3.4`**: Your VPS public IP address.
  11. * **`10.8.0.2`**: Your home server's internal VPN IP address.
  12. * **`6000:7000`**: The range of ports you want to forward.
  13.  
  14. ---
  15.  
  16. ## Step 1: Enable IP Forwarding Permanently
  17. Open the configuration file with a text editor:
  18. ```bash
  19. sudo nano /etc/sysctl.conf
  20. ```
  21.  
  22. Find the line `#net.ipv4.ip_forward=1` and remove the hashtag (`#`) to uncomment it. If the line is missing entirely, add it to the bottom of the file:
  23. ```text
  24. net.ipv4.ip_forward=1
  25. ```
  26.  
  27. Save and exit the file (`Ctrl+O`, `Enter`, `Ctrl+X`). Apply the changes immediately without rebooting:
  28. ```bash
  29. sudo sysctl -p
  30. ```
  31.  
  32. ---
  33.  
  34. ## Step 2: Apply the TCP iptables Rules
  35. Run these three commands on your VPS to handle the routing, firewall allowance, and NAT masquerading:
  36.  
  37. ```bash
  38. # 1. Forward the incoming TCP port range to your home server's VPN IP
  39. sudo iptables -t nat -A PREROUTING -d 1.2.3.4 -p tcp --dport 6000:7000 -j DNAT --to-destination 10.8.0.2:6000-7000
  40.  
  41. # 2. Allow the TCP traffic through the FORWARD chain
  42. sudo iptables -A FORWARD -p tcp -d 10.8.0.2 --dport 6000:7000 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
  43.  
  44. # 3. Masquerade the traffic so the home server forces replies back through the VPN tunnel
  45. sudo iptables -t nat -A POSTROUTING -d 10.8.0.2 -p tcp --dport 6000:7000 -j MASQUERADE
  46. ```
  47.  
  48. > **Note on Syntax:**
  49. > * In the `--dport` match flag, the port range uses a colon (`6000:7000`).
  50. > * In the `--to-destination` target flag, the port range uses a dash (`6000-7000`).
  51.  
  52. ---
  53.  
  54. ## Step 3: Save the Rules Permanently
  55. Debian clears `iptables` rules upon reboot. Use `iptables-persistent` to make them stick:
  56.  
  57. 1. Install the persistent utility package:
  58. ```bash
  59. sudo apt update
  60. sudo apt install iptables-persistent
  61. ```
  62. *During the installation prompt, select **Yes** to save the current IPv4 rules.*
  63.  
  64. 2. If you modify or add rules in the future, save the new configuration manually by running:
  65. ```bash
  66. sudo iptables-save | sudo tee /etc/iptables/rules.v4
  67. ```
  68.  
Advertisement
Add Comment
Please, Sign In to add comment