LoginSignup
1
1

More than 3 years have passed since last update.

Ubuntu on WSL2でVPNに接続する

Last updated at Posted at 2020-06-27

はじめに

企業や団体などで使われることの多いCisco AnyConnectですが,すべての通信をVPN側に送ってしまいます
(もしかしたら設定で変更できるかもしれませんが)
なので.VPN先のファイアウォール設定によっては必要な通信ができなくなることがあります
そこで,WSL2にAnyConnectと互換性のあるOpenconnectを入れることでVPNと通常のネットワークを共存させます

Step by step

Windowsのバージョンを確認する

WSL2を使うので,Windows 10 May 2020 Updateが必要です
バージョンが2004以降であることを確認してください
設定 -> システム -> バージョン情報 で見れます

WSL2を有効にする

Microsoftがインストールガイドを公開しているので、従って下さい

Ubuntuを入れる

Microsoft StoreでUbuntuと検索すれば出てくるのでインストールしてください

Openconnectを入れる

UbuntuにOpenconnectを導入する方法はここにあります
要するに$ sudo apt-get install openconnectです

VPNにつなぐ

$ sudo openconnect -u ユーザ名 --passwd-on-stdin ホスト名 &
でつながります

スクリプトで楽をする

毎回コマンドを打つのは面倒なので,スクリプトを書きます

VPNHOST=ホスト名
VPNUSER=メールアドレス
sudo openconnect -u $VPNUSER --passwd-on-stdin $VPNHOST &

もっと楽をする

パスワードを打つのも面倒です
自動化しましょう

まずは,sudoのためのパスワードをいらなくします
$ sudo visudoします

インストール直後ならこうなっているはずなので,

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

こうします

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# Allow members to execute sudo command without password 書き足しています!
ユーザ名 ALL=NOPASSWD: /usr/sbin/openconnect             書き足しています!

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

あとは,スクリプトをこうすれば完了です

VPNHOST=ホスト名
VPNUSER=メールアドレス
VPNPASS=パスワード
echo $VPNPASS |  sudo openconnect -u $VPNUSER --passwd-on-stdin $VPNHOST &
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1