DebianにOpenVPNサーバを立てる †OpenVPNのインストール †# apt-get update # apt-get install openvpn openssl これだけで必要なパッケージを全部インストールしてくれます 鍵の生成 †認証局(CA)の作成 †openvpnのパッケージにはeasy_rsaというopenvpn用の鍵生成ツールがあります # cd /usr/share/doc/openvpn/examples/easy-rsa/2.0 最初にすることは、このCAの環境を編集することです。varsというファイルがあるのでこれを編集します export KEY_COUNTRY="US" export KEY_PROVINCE="CA" export KEY_CITY="SanFrancisco" export KEY_ORG="Fort-Funston" export KEY_EMAIL="me@myhost.mydomain" KEY_COUNTRYは国(JP)、KEY_PROVINCEは都道府県(Tokyoなど)、KEY_CITYは市区町村、KEY_ORGは所属組織(自分の会社など)、KEY_EMAILはCAの管理者のメールアドレスを設定します。 # . ./vars # ./build-ca Generating a 1024 bit RSA private key ...............................................++++++ ....................................................++++++ writing new private key to 'ca.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [JP]: State or Province Name (full name) [Tokyo]: Locality Name (eg, city) [Hachiohji]: Organization Name (eg, company) [MyCompany]: Organizational Unit Name (eg, section) []: Common Name (eg, your name or your server's hostname) []: MyHost-CA Email Address [foo@example.com]: Common NameはこのCAの名前となります。以降このCAで作る鍵のCommonName?がユニークになるようにしなければなりません。 サーバ用証明書の発行 †次にopenvpnサーバ用の鍵を生成し、証明書を発行します # ./build-key-server OpenVPN-server Generating a 1024 bit RSA private key ...++++++ ..........................................................++++++ writing new private key to 'OpenVPN-server-test3.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [JP]: State or Province Name (full name) [Tokyo]: Locality Name (eg, city) [Hachiohji]: Organization Name (eg, company) [MyCompany]: Organizational Unit Name (eg, section) []: Common Name (eg, your name or your server's hostname) [OpenVPN-server]: Email Address [foo@example.com]: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: Using configuration from /usr/share/doc/openvpn/examples/easy-rsa/2.0/openssl.cnf Check that the request matches the signature Signature ok The Subject's Distinguished Name is as follows countryName :PRINTABLE:'JP' stateOrProvinceName :PRINTABLE:'Tokyo' localityName :PRINTABLE:'Hachiohji' organizationName :PRINTABLE:'MyCompany' commonName :PRINTABLE:'OpenVPN-server' emailAddress :IA5STRING:'foo@example.com' Certificate is to be certified until Mar 16 02:14:34 2019 GMT (3650 days) Sign the certificate? [y/n]:y 1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated 上で説明したとおり、CommonName?には一意の名前を付けます。 次にDiffieHellman?のパラメータを生成します # ./build-dh SSL/TLSの鍵交換で必要なファイルです。 クライアント用証明書の発行 †サーバ用と同様にクライアントにも鍵と証明書を発行する必要があります # ./build-key myClient サーバ証明書と同様にCommonName?の入力、Sign the certificate?、commit?と進みます。 サーバ設定ファイルの編集 †上で生成した以下のファイルを/etc/openvpnにコピーします ca.crt OpenVPN-server.crt OpenVPN-server.key dh1024.pem 次にサンプルの設定ファイルをコピーして編集のベースにします # cd /etc/openvpn # cp /usr/share/doc/openvpn/exaples/sample-config-files/server.conf.gz . # gunzip server.conf.gz # vi server.conf 編集すべき点は以下です port 1194 openvpnが待ち受けるポート番号
proto udp openvpnが使用するプロトコル(udp/tcp)
dev tap0 openvpnが使用する仮想インターフェースデバイス(tap/tun)
ca ca.crt 認証局の証明書ファイルパス
cert OpenVPN-server.crt openvpnサーバの証明書ファイル
key OpenVPN-server.key openvpnサーバの鍵ファイルパス
dh dh1024.pem DHパラメータファイル
server 192.168.1.0 255.255.255.0 動作モードの指定。serverはルーティングモード
パラメータ2つはopenvpnが使用するネットワークアドレスとネットマスク
user nobody openvpnの実行ユーザー
group nogroup openvpnの実行グループ
編集が終わったら起動してみます。 # openvpn server.conf Initialization Sequence Completedが表示されたら正しく起動しています。 # /etc/init.d/openvpn start でdaemonとして起動させておきましょう またルーティングを有効にする必要があります # vi /etc/sysctl.conf net.ipv4.ip_forward=1 としましょう。再起動すると有効になります。 # echo "1" > /proc/sys/net/ipv4/ip_forward とします。 ポートフォワードの設定 †外部ネットワークに直接接続されていない場合はルータ等でポートフォワードを設定する必要があります。 クライアントの設定 †Windowsクライアントの場合、CUIクライアントとGUIクライアントがあります。 次にネットワークのプロパティを開いて、TAP-Win32 Adapter V8のデバイスで生成されたローカルエリア接続を探します。 サーバで生成した以下のファイルをクライアントにコピーします。 myClient.crt myClient.key ca.crt できる限り安全な経路でコピーするようにしましょう。 c:\program files\openvpn\sample-config\client.ovpnをconfigディレクトリにコピーし、テキストエディタで編集します。 client クライアントモードを指定します pull 設定をサーバから取得することを意味します float IPアドレスが動的であることを示します dev tap0 サーバのdev指定とあわせます dev-node OpenVPN 上で変更したTAP-Win32デバイスの接続名を指定 proto udp サーバのproto指定とあわせます ca ca.crt 認証局の証明書ファイルパス cert myClient.crt クライアントの証明書ファイル key myClient.key クライアントの鍵ファイル remote vpn.server 1194 openvpnサーバのアドレスとポート番号を指定します OpenVPN GUIのタスクトレイアイコンを右クリック→client→connectで接続に行きます。 ブリッジモードの設定 †上の設定ではopenvpnサーバはルーティングモードになっており、openvpnサーバが所属するサブネットとは別のサブネットがopenvpnクライアントに設定されます。 そこでブリッジモードを使います。 ブリッジの作成 †まずはopenvpnが生成する仮想コネクションと物理ネットワークインターフェース間のブリッジを作成します # apt-get install bridge-utils # vi /etc/network/interfaces 通常、interfacesは以下のようになっていると思います。 # This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo eth0
iface lo inet loopback
# The primary network interface
allow-hotplug eth0
iface eth0 inet static
address 192.168.1.250
netmask 255.255.255.0
gateway 192.168.1.254
これを以下のように編集します auto lo
iface lo inet loopback
# The primary network interface
allow-hotplug eth0
iface eth0 inet static
auto br0
iface br0 inet static
address 192.168.1.250
netmask 255.255.255.0
gateway 192.168.1.254
network 192.168.213.0
broadcast 192.168.1.255
bridge_ports eth0 tap0
pre-up openvpn --mktun --dev tap0
pre-up /sbin/ifconfig tap0 0.0.0.0 promisc up
pre-up /sbin/ifconfig eth0 0.0.0.0 promisc up
pre-up /usr/sbin/brctl addbr br0
pre-up /usr/sbin/brctl addif br0 eth0
pre-up /usr/sbin/brctl addif br0 tap0
post-down /usr/sbin/brctl delif br0 eth0
post-down /usr/sbin/brctl delif br0 tap0
post-down /usr/sbin/brctl delbr br0
post-down /usr/sbin/openvpn --rmtun --dev tap0
post-down /sbin/ifconfig eth0 down
今までeth0にアサインされていたipの設定をbr0に移動、eth0の設定を空にします openvpnのブリッジモード指定 †次にserver.confを変更します。 # vi /etc/openvpn/server.conf ; server 192.168.1.0 255.255.255.0 server-bridge 192.168.1.250 255.255.255.0 192.168.1.60 192.168.1.128 ;server-bridge push "dhcp-option DNS 192.168.1.245" push "dhcp-option WINS 192.168.1.245" server-bridgeでブリッジモードを指定します。パラメータはブリッジIFのIPアドレス、ネットマスク、クライアント用IPプール範囲開始、範囲終了です。 これで、一度再起動しましょう。 VMware ESXi上でopenvpnをブリッジモードで稼動させる †プロミスキャスモードの指定 †VMware ESXiでは標準ではゲストOSのネットワークがプロミスキャスモードになることを許可していないため、ブリッジモードがうまく動作しません。 VICを起動します VMware ESXiのVMwareTools?をインストールする †VMwareTools?に必要なkernel headerをインストール †# uname -a Linux host 2.6.26-1-686 #1 SMP Sat Jan 10 18:29:31 UTC 2009 i686 GNU/Linux # apt-get install linux-headers-2.6.26-1-686 Open Virtual Machine Toolsをダウンロード †http://open-vm-tools.sourceforge.net/から最新版のopen-vmtoolsをダウンロードしておく VMwareTools?のCDイメージをマウント †VICから対象VMを右クリック>Install/Upgrade VMware Toolsを選ぶ # mount /cdrom VMwareTools?のインストール †以下のようにしてまずインストールする。(この作業はVMwareコンソールから行うこと。途中でネットワークがきれるため) # cp /cdrom/VMwareTools-3.5.x-nnnnnn.tar.gz /tmp # cd /tmp # tar -zxf VMwareTools-3.5.x-nnnnnn.tar.gz # cd vmware-tools-distrib # export CC=gcc-4.1 # ./vmware-install.pl すべてデフォルトで答えてかまわない。コンパイルエラーが出るが現時点では無視する。 Open Virtual Machine Toolsのインストール †上でダウンロードしておいたmergetools.shを実行する # export CC=gcc-4.1 # sh ./mergetools.sh 先ほどと同様にすべてデフォルトでかまわない # lsmod |