Raspberry Pi B+ (Raspbian)にOpenVPNを入れる

  • 投稿日:
  • by
  • カテゴリ:

色々なサイトで紹介されているが、openvpn.conf の書き方で詰まったのでメモ。

(1)まず鍵を作る

pi@pi ~ $ sudo su
# apt-get update
# apt-get upgrade
# apt-get install openvpn openssl
# cd /etc/openvpn
# cp -r /usr/share/doc/openvpn/examples/easy-rsa/2.0 ./easy-rsa
# vi easy-rsa/vars

以下varsの内容:

root@pi:/etc/openvpn/easy-rsa# cat vars
# easy-rsa parameter settings

# NOTE: If you installed from an RPM,
# don't edit this file in place in
# /usr/share/openvpn/easy-rsa --
# instead, you should copy the whole
# easy-rsa directory to another location
# (such as /etc/openvpn) so that your
# edits will not be wiped out by a future
# OpenVPN package upgrade.

# This variable should point to
# the top level of the easy-rsa
# tree.
export EASY_RSA="/etc/openvpn/easy-rsa"

#
# This variable should point to
# the requested executables
#
export OPENSSL="openssl"
export PKCS11TOOL="pkcs11-tool"
export GREP="grep"


# This variable should point to
# the openssl.cnf file included
# with easy-rsa.
export KEY_CONFIG=`$EASY_RSA/whichopensslcnf $EASY_RSA`

# Edit this variable to point to
# your soon-to-be-created key
# directory.
#
# WARNING: clean-all will do
# a rm -rf on this directory
# so make sure you define
# it correctly!
export KEY_DIR="$EASY_RSA/keys"

# Issue rm -rf warning
echo NOTE: If you run ./clean-all, I will be doing a rm -rf on $KEY_DIR

# PKCS11 fixes
export PKCS11_MODULE_PATH="dummy"
export PKCS11_PIN="dummy"

# Increase this to 2048 if you
# are paranoid. This will slow
# down TLS negotiation performance
# as well as the one-time DH parms
# generation process.
export KEY_SIZE=1024

# In how many days should the root CA key expire?
export CA_EXPIRE=3650

# In how many days should certificates expire?
export KEY_EXPIRE=3650

# These are the default values for fields
# which will be placed in the certificate.
# Don't leave any of these fields blank.
export KEY_COUNTRY="JP"
export KEY_PROVINCE="Ibaraki"
export KEY_CITY="somewhere"
export KEY_ORG="hogehoge"
export KEY_EMAIL="xx@xx.xx"
export KEY_EMAIL=mail@host.domain
export KEY_CN=changeme
export KEY_NAME=changeme
export KEY_OU=changeme
export PKCS11_MODULE_PATH=changeme
export PKCS11_PIN=1234

(2)その後、「サーバ鍵」と「クライアント鍵」を作成する。

# . ./easy-rsa/vars
# ./easy-rsa/clean-all
# cd easy-rsa
# ln -s openssl-1.0.0.cnf openssl.cnf
# cd ..
# ./easy-rsa/build-ca OpenVPN
# ./easy-rsa/build-key-server server
# ./easy-rsa/build-key client1
# ./easy-rsa/build-dh

そして、/etc/server.confを作成。

# cat server.conf
##############################################
#     OpenVPNサーバ設定ファイル   #
##############################################

#tls-authを有効にする
#tls-auth tls-auth.key 0

#256bit AESを有効にする
#cipher AES-256-CBC

#通信ポートの設定
#(1194番ポートをすでに使用している人は以下を書き換えてください)
port 1194

#UDPを使用
#(UDPの使用が推奨されますが、どうしてもTCPを使用したい場合は以下を書き換えてください。)
proto udp

#ルーティングIPトンネルを生成
dev tun

ca /etc/openvpn/easy-rsa/keys/ca.crt # generated keys
cert /etc/openvpn/easy-rsa/keys/server.crt
key /etc/openvpn/easy-rsa/keys/server.key # keep secret
dh /etc/openvpn/easy-rsa/keys/dh1024.pem

#max-clients 100

#クライアント同士が通信できるようにする場合は
#以下の行の#を外してください。
#client-to-client

以下、server.conf
#以下は特に書き換える必要なし
#必要な人のみ書き換えるようにしてください。
server 192.168.203.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "dhcp-option DNS 192.168.1.20"
push "dhcp-option DNS 8.8.8.8"
#push "dhcp-option WINS 192.168.0.2"
keepalive 10 120
comp-lzo
persist-key
persist-tun
status openvpn-status.log
verb 3

(3)Pi内部でIP転送できるようにする。

# echo 1 > /proc/sys/net/ipv4/ip_forward

さらに
 /etc の中で sysctl.conf を開き,# net.ipv4.ip_forward = 1 の「#」を消してその行を有効(アンコメント)にする。([Ctrl]+ O, [Enter], [Ctrl]+ x で保存)

(3)/etc/rc.localの末尾に次の2行を追加

iptables -t nat -A INPUT -i eth0 -p udp -m udp -dport 1194 -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.203.0/16 -o eth0 -j SNAT --to-source 192.168.1.6

(4)起動することを確認

# /etc/init.d/openvpn start

(5)起動時にopenvpnが起動できるようにする

# update-rc.d openvpn