首页 > 解决方案 > 如何访问 VMWare Fusion VM 内的 VPN

问题描述

我在 MacOS BigSur 中有一个 VPN 连接,但我无法在 VMWare Fusion V12.1.2 下运行的 Linux VM 中访问它。

标签: virtual-machinevmwarevpnnat

解决方案


该问题已在 V12.2.0 VMWare Fusion 12.2.0 发行说明中得到修复

解决方案是手动创建 VPN 隧道并将其链接到 VM,因为涉及多个命令并且 IP 地址可以更改我创建了以下脚本来执行所需的命令。

#!/bin/bash

function ask_yes_or_no() {
    read -p "$1 ([y]es or [N]o): "
    case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
        y|yes) echo "yes" ;;
        *)     echo "no" ;;
    esac
}

currNatRules=$(sudo pfctl -a com.apple.internet-sharing/shared_v4 -s nat 2>/dev/null)
if test -z "$currNatRules" 
then
    echo -e "\nThere are currently no NAT rules loaded\n"
    exit 0
fi

utunCheck=$(echo $currNatRules | grep utun)
if test -n "$utunCheck"
then
    echo -e "\nIt looks like the VPN tunnel utun2 has already been created"
    echo -e "\n$currNatRules\n"

    if [[ "no" == $(ask_yes_or_no "Do you want to continue?") ]]
    then
        echo -e "\nExiting\n"
        exit 0
    fi
fi


natCIDR=$(echo $currNatRules | grep en | grep nat | cut -d\  -f 6)
if test -z "$natCIDR" 
then
    echo -e "\nCannot extract the NAT CIDR from:"
    echo -e "\n$currNatRules\n"
    exit 0
fi

interface=$(route get 10/8 | grep interface | cut -d\  -f 4)

echo -e "\nNAT CIDR=$natCIDR Interface=$interface\n"

newRule="nat on ${interface} inet from ${natCIDR} to any -> (${interface}) extfilter ei"

echo -e "\nAdding new rule: $newRule\n"

configFile="fixnat_rules.conf"

[[ -d $configFile ]] && rm $configFile
echo "$currNatRules" > $configFile
echo "$newRule" >> $configFile

sudo pfctl -a com.apple.internet-sharing/shared_v4 -N -f  ${configFile} 2>/dev/null

echo -e "\nConfig update applied\n"

sudo pfctl -a com.apple.internet-sharing/shared_v4 -s nat 2>/dev/null

echo -e "\n"

exit 0

推荐阅读