大网数据广告活动
服务器运维问答

How to configure a failsafe solution for OpenVPN in Linux?

· 52CE 我爱测

I'm running a linux debian x64. OpenVPN is running and configured. Now I want to disable the internet connection whenever my vpn isn't running or the connection broke up. You know what I mean? VPN working -> internet connection V…

问题原文

原帖链接 · vP3nguin · CC BY-SA 3.0

I'm running a linux debian x64. OpenVPN is running and configured.

Now I want to disable the internet connection whenever my vpn isn't running or the connection broke up.

You know what I mean?

VPN working -> internet connection

VPN not working -> no internet connection

Is it possible to solve this within iptables or what should be the best way?

已采纳解答

原帖链接 · MadHatter · CC BY-SA 3.0

You normally run all your internet traffic over OpenVPN when it's up, and presumably wish not to have this leak over the plaintext internet when OpenVPN falls over. As such, this is fairly easy: simply forbid any traffic to leave your regular ethernet interface (which I assume here is eth0) unless it's either OpenVPN traffic (which I assume here is UDP/1194, modify appropriately for TCP OpenVPN), or infrastructurally necessary (DNS, ICMP):

iptables -A OUTPUT -o eth0 -p udp --dport 1194 -j ACCEPT
iptables -A OUTPUT -o eth0 -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -o eth0 -p icmp  -j ACCEPT
iptables -A OUTPUT -o eth0 -j DROP

I know that not all ICMP is needed, but I don't personally find harm in allowing PING requests and PONG responses out; modify the rules above if you do. iptables rules are order-sensitive, so if you already have rules in your OUTPUT chain, then getting these in the right place is something you will need to have a care to do.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-04-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。