クラウド上UbuntuにPostgreSQLをインストールしVPNアクセス

Linux

本ページは広告が含まれています。気になる広告をクリック頂けますと、サーバ運営費になります(^^

クラウドに配置したUbuntuサーバにPostgreSQLをインストールし、PacketixVPNでPostgreSQLサーバにアクセスします。

PostgreSQLのインストール準備をします

apt-get update

PostgreSQLバージョン10が入るようです。

# apt-cache show postgresql
Package: postgresql
Architecture: all
Version: 10+190
Priority: optional
Section: database
Source: postgresql-common (190)
Origin: Ubuntu
Maintainer: Ubuntu Developers 
Original-Maintainer: Debian PostgreSQL Maintainers 
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 62
Depends: postgresql-10
Suggests: postgresql-doc
Filename: pool/main/p/postgresql-common/postgresql_10+190_all.deb
Size: 5784
MD5sum: 2fb6f13d95e354c47a1df602f68f7131
SHA1: 45e8e55044259941faba82282243f11194baf66c
SHA256: 02baa3ea401deb634a4307a8d0e25378613ffbf3b3ddbda9cc03d5277a5ea7a4
Description-en: object-relational SQL database (supported version)
 This metapackage always depends on the currently supported PostgreSQL
 database server version.
 .
 PostgreSQL is a fully featured object-relational database management
 system.  It supports a large part of the SQL standard and is designed
 to be extensible by users in many aspects.  Some of the features are:
 ACID transactions, foreign keys, views, sequences, subqueries,
 triggers, user-defined types and functions, outer joins, multiversion
 concurrency control.  Graphical user interfaces and bindings for many
 programming languages are available as well.
Description-md5: bdff2d6e5b2a1dd00e72b3ed8729d9ac
Task: postgresql-server
Supported: 5y

postgresqlをインストールします。

# apt-get install postgresql
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libpq5 libsensors4 postgresql-10 postgresql-client-10 postgresql-client-common postgresql-common sysstat
Suggested packages:
  lm-sensors postgresql-doc locales-all postgresql-doc-10 libjson-perl isag
The following NEW packages will be installed:
  libpq5 libsensors4 postgresql postgresql-10 postgresql-client-10 postgresql-client-common postgresql-common sysstat
0 upgraded, 8 newly installed, 0 to remove and 8 not upgraded.
Need to get 5,310 kB of archives.
After this operation, 20.9 MB of additional disk space will be used.
Do you want to continue? [Y/n]

dbを作成する上で、分かりやすいようにシェルログインユーザも同じユーザ名・パスワードで作成しておきます。

# passwd postgres
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@Cloudn01:/var/log/postgresql# su - postgres
postgres@Cloudn01:~$ psql
psql (10.6 (Ubuntu 10.6-0ubuntu0.18.04.1))
Type "help" for help.

postgres=# alter role postgres with password '********';
ALTER ROLE
\q
# vi /etc/postgresql/10/main/pg_hba.conf
# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
host    all             all             172.16.4.0/24            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            md5

私の環境でのポイントはこちらで

host all all 172.16.4.0/24 md5

PacketixVPNで割り振られるIPアドレスにしてあります。

/etc/postgresql/10/main# vi postgresql.conf
listen_addresses = '*'          # what IP address(es) to listen on;
# /etc/init.d/postgresql restart
[ ok ] Restarting postgresql (via systemctl): postgresql.service.
https://www.techlive.tokyo/archives/6165

PacketixVPNサーバをインストールしたホスト(サーバ)自体にアクセスしたい場合はSecureNATだけでは利用できません。SecureNAT機能では、DHCPサーバをしているホストが、vpnseverをインストールしたホストのように感じますが、単なるDHCPサーバとして機能しているだけで内部にはアクセスできません。

SecureNATを利用する方法もありますが、一工夫必要です。

そこでtapデバイスを作成して、アクセスします。SecureNAT機能は無効化しておきます。

vpnserver自体にtapデバイスを作成する機能があります。とっても便利。新しいtapデバイスを作成してそのtapデバイスとローカルブリッジ接続します。Linuxカーネルの問題で、vpn接続したサーバに対してnicがないとローカルサーバとの通信はできない仕様になっているとの事ですので、tapデバイス、もしくはローカルの物理NICを必ず割り当てる必要があります。

賢者(とても詳しい方)の発言がこの辺りに掲載されています。

vi /etc/network/interfaces
# The VPN network interface
iface tap_vpn01 inet static
   address 10.4.1.1/24

PacketixVPN上で作成したtapデバイスにubuntu上でIPアドレスを割り当てます。

ifup tap_vpn01

tapデバイスを有効にし、ちゃんとIPアドレスが割り当てられているか確認します。

# ifconfig
tap_vpn01: flags=4163  mtu 1500
        inet 172.16.4.1  netmask 255.255.255.0  broadcast 10.4.1.255
        inet6 fe80::5ce2:1dff:fea3:b030  prefixlen 64  scopeid 0x20
        ether 5e:e2:1d:a3:b0:30  txqueuelen 1000  (Ethernet)
        RX packets 5115  bytes 651340 (651.3 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3259  bytes 336454 (336.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
タイトルとURLをコピーしました