LoginSignup
4
7

More than 5 years have passed since last update.

VPSの容量をVPNで増やす

Last updated at Posted at 2018-10-14

はじめに

借りているVPSの容量が不足してきました。もちろん契約を変更しもっと大きなHDDに増やせばよいのですが、大部分のデーターがめったにアクセスしない、かと言って消すわけにもいかないものの場合「遅くていいからローコストで」容量増加を図りたい場合の方法について記します。

手段

  1. 自宅NaniPI NASからVPSにOpenVPN Tunnelを接続する。
  2. NASのDiskをnfs expotする。
  3. VPSにnfs mountして公開する。

OpenVPNをInstallする

OpenVPN Server

VPS (さくらのVPS)にOpenVPSを実装します。

apt update
apt upgrade
apt install easy-rsa openvpn

鍵を作ります。

cd /usr/share/easy-rsa
vi vars
/usr/share/easr-rsa/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="`pwd`"

#
# 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=2048

# 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="SAITAMA"
export KEY_CITY="Kawagoe"
export KEY_ORG="Agri-hitech"
export KEY_EMAIL="mac@line.to"
export KEY_OU="R&D"

# X509 Subject Field
export KEY_NAME="EasyRSA"

# PKCS11 Smart Card
# export PKCS11_MODULE_PATH="/usr/lib/changeme.so"
# export PKCS11_PIN=1234

# If you'd like to sign all keys with the same Common Name, uncomment the KEY_CN export below
# You will also need to make sure your OpenVPN server config has the duplicate-cn option set
# export KEY_CN="CommonName"
. ./vars
./clear-all
./build-ca
./build-key-server server
./build-dh
openvpn --genkey --secret ta.key
./build-key c1
./build-key c2
./build-key c3

鍵を配置します。

cd keys
cp ca.* dh2048.pem server.* /etc/openvpn
cd /etc/openvpn
chmod 600 *.key

OpenVPN Serverの設定

/etc/openvpn/openvpn.conf
port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key  # This file should be kept secret
dh dh2048.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
keepalive 10 120
tls-auth ta.key 0 # This file is secret
persist-key
persist-tun
status openvpn-status.log
verb 3
/etc/init.d/openvpn
#!/bin/sh -e

### BEGIN INIT INFO
# Provides:          openvpn
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Should-Start:      network-manager
# Should-Stop:       network-manager
# X-Start-Before:    $x-display-manager gdm kdm xdm wdm ldm sdm nodm
# X-Interactive:     true
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Openvpn VPN service
# Description: This script will start OpenVPN tunnels as specified
#              in /etc/default/openvpn and /etc/openvpn/*.conf
### END INIT INFO
. /lib/lsb/init-functions

test $DEBIAN_SCRIPT_DEBUG && set -v -x

DAEMON=/usr/local/sbin/openvpn
DESC="virtual private network daemon"
CONFIG_DIR=/etc/openvpn

test -x $DAEMON || exit 0
test -d $CONFIG_DIR || exit 0

case "$1" in
start)
  log_daemon_msg "Starting $DESC"
  (cd /etc/openvpn; /usr/local/sbin/openvpn /etc/openvpn/openvpn.conf) 2>&1 > /var/log/openvpn.log &
  log_end_msg ${STATUS:-0}

  ;;
stop)
  log_daemon_msg "Stopping $DESC"
  killall -9 openvpn && exit 0
  log_end_msg 0
  ;;
# Only 'reload' running VPNs. New ones will only start with 'start' or 'restart'.
reload|restart)
 log_daemon_msg "Reloading $DESC"
 /etc/init.d/openvpn stop
 /etc/init.d/openvpn start
    log_progress_msg "$NAME"
  log_end_msg 0
  ;;
*)
  echo "Usage: $0 {start|stop|restarts}" >&2
  exit 1
  ;;
esac

exit 0

実行フラグを立て、

chmod +x /etc/init.d/openvpn

起動します。

/etc/init.d/openvpn start

自動起動にします。

update-rc.d enable openvpn

OpenVPN Clientの設定

自宅のNanoPI NASにOpenVPN Clientの設定をします。

apt install openvpn

鍵を配置します。先にServerで作った鍵をscp, USBメモリなど安全な方法で輸送します。

mount /dev/<usbmem> /usb
cd /usr/share/easy-rsa/vars
cp ca.crt ta.key c1.* /usb/
cd /
umount /usb

輸送後

mount /dev/<usbmem> /usb
cd /etc/openvpn
mv c1.key client.key
mv c1.crt client.crt
mv c1.csr client.csr
mv ta.key .
mv ca.crt .
chmod 600 *.key

設定ファイルの制作

/etc/openvpn/openvpn.conf
client
dev tun
proto udp
remote line.to 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert client.crt
key client.key
remote-cert-tls server
tls-auth ta.key 1
cipher AES-256-CBC
verb 3

起動ファイル

Serverと同じ

OpenVPN Client起動

/etc/init.d/openvpn start

自動起動にします。

update-rc.d enable openvpn

試験します。

ping 10.8.0.1

Server側からも

ping 10.8.0.10

ここまで来ればLANで繋がっているのと同様です。

NFSの設定

OpenVPNのclient側にnfs server、OpenVPNのserver側にnfs clientを設置します。

NFS Server側

apt install nfs-server
/etc/exports
/       10.8.0.1/24(ro,sync,no_subtree_check)

起動します。

exportfs -a

NFSのClient側

apt install nfs-client

試験します。

mkdir /nfs
mount -t nfs 10.8.0.10:/ /nfs
umount /nfs

fstabを編集して恒久的にmountします。
最終行に以下追加。

/etc/fatab
10.8.0.10:/ /nfs nfs defaults 0 0

mountする。

mount -a

終わりに

OpenVPNでtunnelが掘れれば、NFSだけでなくSMB, ftp, telnetもセキュアに透過します。
またClientを複数置いてClient同士で通信することもルーティング次第で可能になります。
長くなりましたので、これらは別稿としましょう。

4
7
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
4
7