LoginSignup
26

More than 5 years have passed since last update.

RaspberryPiにOpenVPNサーバを立てる

Last updated at Posted at 2016-09-21

概要

スタバやホテルなどフリーWifiスポットで、OpenVPNを利用してローカル(自宅)ネットワークに VPN 接続する
OSにRaspbianがインストールされていることが条件(Lite版以外)

OpenVPNサーバの構築

すべてスーパーユーザ(root)で操作を行なうこと
viエディタの使い方については以下のURLを参照
http://net-newbie.com/linux/commands/vi.html

OpenVPNのインストール

# apt-get update
# apt-get upgrade
# apt-get install zip
# apt-get install openssl
# apt-get install openvpn

zipはクライアントファイルの転送に使用するためインストールする

IPフォワードを有効に設定

# echo 1 > /proc/sys/net/ipv4/ip_forward
# vi /etc/sysctl.conf

以下を編集する

/etc/sysctl.conf
# Uncomment the next line to enable packet forwarding for IPv4
#net.ipv4.ip_forward=1 ← #をコメントアウトする

再起動する

# reboot

証明書ファイルを作成

# sudo su
# cd /usr/share/doc/openvpn/examples/easy-rsa/2.0
# vi vars

以下の部分を探して任意に変更する。

KEY_COUNTRY=
KEY_PROVINCE=
KEY_CITY=
KEY_ORG=
KEY_EMAIL=
/usr/share/doc/openvpn/examples/easy-rsa/2.0/vars
KEY_COUNTRY="JP"
KEY_PROVINCE="IBARAKI"
KEY_CITY="TSUKUBA"
KEY_ORG="VPNSERVER"
KEY_EMAIL="me@myhost.mydomain"
# . ./vars
# ./clean-all

マスタCA証明書を作成

# ./build-ca
Common Name (eg, your name or your server's hostname) []:

Common Nameに任意の文字列を入れる以外は空白で良い。
私の場合にはcaと入力しました。

サーバ証明書を作成

# ./build-key-server server
Common Name (eg, your name or your server's hostname) []:

Common Nameに任意の文字列を入れる以外は空白で良い。
私の場合にはserverと入力しました。

さらに

Sign the certificate? [y/n]:
1 out of 1 certificate requests certified, commit? [y/n]

この2つを聞いてきますので、両方ともyで。

各クライアントに配布する証明書を作成

# ./build-key client1
Common Name (eg, your name or your server's hostname) []:

Common Nameに任意の文字列を入れる以外は空白で良い。
私の場合にはclient1と入力しました。

さらに

Sign the certificate? [y/n]:
1 out of 1 certificate requests certified, commit? [y/n]

この2つを聞いてきますので、両方ともyで。

クライアントを増やしたければ次のようにつくる。

#./build-key client2
#./build-key client3
#./build-key client4

DH (Diffie Hellman) パラメータを作成

# ./build-dh

TLS鍵を作成

# cd keys
# openvpn --genkey --secret vpn_ta.key

作成した証明書ファイルを移動

# cp vpn_ta.key server.crt server.key dh1024.pem ca.crt /etc/openvpn

必要なクライアントを圧縮

# zip vpnclient.zip ca.crt vpn_ta.key client1.crt client1.key

zipファイルを移動

ファイル転送出来るように/homeへ移動する
方法は、USBメモリやメールなどで転送する

# cp vpnclient.zip /home

confファイルでOpenVPNサーバーの設定

# cd /etc/openvpn
# cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz .
# gunzip server.conf.gz
# vi server.conf

xxx.xxx.xxx.xxxはプロバイダのDNSサーバの環境に合わせて変更する
DNSサーバの確認方法は以下のURLを参照
http://www.akakagemaru.info/port/dns-address.html

/etc/openvpn/server.conf
;local a.b.c.d

port 1194

;proto tcp
proto udp

;dev tap0
dev tun

;dev-node MyTap

ca /etc/openvpn/ca.crt
cert /etc/openvpn/server.crt
key /etc/openvpn/server.key

dh /etc/openvpn/dh1024.pem

server 10.8.0.0 255.255.255.0

ifconfig-pool-persist ipp.txt

;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100

;server-bridge

push "route 10.8.0.0 255.255.255.0"
;push "route 192.168.20.0 255.255.255.0"

;client-config-dir ccd
;route 192.168.40.128 255.255.255.248

;client-config-dir ccd
;route 10.9.0.0 255.255.255.252

;learn-address ./script

push "redirect-gateway def1"

push "dhcp-option DNS xxx.xxx.xxx.xxx"
push "dhcp-option DNS xxx.xxx.xxx.xxx"

;client-to-client

;duplicate-cn

keepalive 10 120

tls-auth /etc/openvpn/vpn_ta.key 0 

;cipher BF-CBC        
;cipher AES-128-CBC   
;cipher DES-EDE3-CBC  

;comp-lzo


;max-clients 100

user nobody
group nogroup

persist-key
persist-tun

status /etc/openvpn/openvpn-status.log

log         /etc/openvpn/openvpn.log
log-append  /etc/openvpn/openvpn.log

verb 3

;mute 20

設定したOpenVPNを再起動

# /etc/init.d/openvpn restart
Stopping virtual private network daemon:.
Starting virtual private network daemon: server.

[OK]と表示できたら起動成功!

RasPi起動時にOpenVPNを自動起動設定

# update-rc.d openvpn defaults

RasPiのポート開放の設定

ルーターのUDP1194ポート開放も忘れずに設定すること

# vi /etc/rc.local

以下を iptables~~~ の2行を追記する。

  : 省略
iptables -A INPUT -p udp -m state --state NEW -m udp --dport 1194 -j ACCEPT
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

exit 0

クライアントの設定

圧縮したzipファイルのkeyファイルまたはcrtファイルをメモ帳で開き、< > </ >内にコピーする。
xxx.xxx.xxx.xxxはルータの固定IPアドレスを記入する

client.ovpn
client
dev tun
proto udp
key-direction 1
remote xxx.xxx.xxx.xxx 1194
resolv-retry infinite
nobind
persist-key
persist-tun
verb 3
<ca>
-----BEGIN CERTIFICATE-----

-----END CERTIFICATE-----
</ca>
<cert>
-----BEGIN CERTIFICATE-----

-----END CERTIFICATE-----
</cert>
<key>
-----BEGIN PRIVATE KEY-----

-----END PRIVATE KEY-----
</key>
<tls-auth>
-----BEGIN OpenVPN Static key V1-----

-----END OpenVPN Static key V1-----
</tls-auth>

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
26