はじめに
GlobalProtect を使ってWSL2のUbuntuからオープンネットワークにアクセスが失敗した。その解決方法を記載する、
環境
- Windows10(ホストOS)
- WSL2
- Ubuntu 22.04.3 LTS
設定
オープンネットワークに接続できない理由として、以下が上げられる。
この解決する設定について後述する。
DNSサーバのIPアドレスの確認
コマンドプロンプトを開く。
次に、PANGP Virtual Ethernet Adapter Secureという、GlobalProtect VPNクライアントがシステムにインストールする仮想ネットワークアダプターを確認する。
>ipconfig /all イーサネット アダプター イーサネット 2: 接続固有の DNS サフィックス . . . . .: 説明. . . . . . . . . . . . . . . . .: PANGP Virtual Ethernet Adapter Secure (中略) DNS サーバー. . . . . . . . . . . . .: X.X.X.X
メトリックの設定
次に、メトリックを変更する。 PowerShellを管理者権限で実行する。 aquasoftware.net からメトリックを設定するソースコードを流用して、Powershell上で実行する。
$targetIfName = 'PANGP Virtual Ethernet Adapter'; # define function function Get-NetworkAddress { param([Parameter(Mandatory, ValueFromPipelineByPropertyName)][string]$IPAddress, [Parameter(Mandatory, ValueFromPipelineByPropertyName)][int]$PrefixLength); process { $bitNwAddr = [ipaddress]::Parse($IPAddress).Address -band [uint64][BitConverter]::ToUInt32([System.Linq.Enumerable]::Reverse([BitConverter]::GetBytes([uint32](0xFFFFFFFFL -shl (32 - $PrefixLength) -band 0xFFFFFFFFL))), 0); [pscustomobject]@{ Addr = $IPAddress; Prfx = $PrefixLength; NwAddr = [ipaddress]::new($bitNwAddr).IPAddressToString + '/' + $PrefixLength; }; } } # extend route metric $targetAddresses = Get-NetAdapter -IncludeHidden | Where-Object InterfaceDescription -Match 'Hyper-V Virtual Ethernet Adapter' | Get-NetIPAddress -AddressFamily IPv4 | Get-NetworkAddress; $targetIfs = Get-NetAdapter -IncludeHidden | Where-Object InterfaceDescription -Match $targetIfName; $targetIfs | Set-NetIPInterface -InterfaceMetric 2; $targetIfs | Get-NetRoute -AddressFamily IPv4 | Select-Object -PipelineVariable rt | Where-Object { $targetAddresses | Where-Object { $_.NwAddr -eq (Get-NetworkAddress $rt.DestinationPrefix.Split('/')[0] $_.Prfx).NwAddr } } | Set-NetRoute -RouteMetric 6000;
GlobalProtect 以外の場合は、 上記コード一行目の 'PANGP Virtual Ethernet Adapter'のインターフェス名を適宜に変更する。
設定された結果を確認する。
> route print IPv4 ルート テーブル =========================================================================== アクティブ ルート: ネットワーク宛先 ネットマスク ゲートウェイ インターフェイス メトリック X.X.X.X 255.255.255.255 リンク上 10.8.206.178 2 172.19.192.0 255.255.240.0 リンク上 10.8.206.178 6002 172.19.207.255 255.255.255.255 リンク上 10.8.206.178 6002
nameserverの設定
WSL上でnameserverにGlobalProtectのDNSのIPアドレスを設定する。
$ sudo vim /etc/resolv.conf nameserver X.X.X.X
これで、オープンネットワークに接続される。
試しに、apt updateコマンドを実行し、接続されることを確認した。
$ sudo apt update Hit:1 http://security.ubuntu.com/ubuntu jammy-security InRelease Hit:2 http://archive.ubuntu.com/ubuntu jammy InRelease Hit:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease (省略)